0

I'm trying to get url of the front-end logo inside a custom helper method in the backend, but I get stuck on getting a file using theme model. All I have is: store id, theme id, theme model and a filename:

/**
 * Retrieve default store logo image URL from design config or theme
 *
 * @param null $storeId
 * @return string
 */
protected function getDefaultStoreLogoUrl($storeId = null)
{
    $folderName = \Magento\Config\Model\Config\Backend\Image\Logo::UPLOAD_DIR;
    $storeLogoPath = $this->scopeConfig->getValue(
        'design/header/logo_src',
        ScopeInterface::SCOPE_STORE,
        $storeId
    );

    $path = $folderName . '/' . $storeLogoPath;
    $logoUrl = $this->_urlBuilder
            ->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) . $path;

    if ($storeLogoPath !== null && $this->isFile($path)) {
        $url = $logoUrl;
    } else {
        $themeId = (int)$this->scopeConfig->getValue(
            'design/theme/theme_id',
            ScopeInterface::SCOPE_STORE,
            $storeId
        );
        /** @var \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection */
        $themeCollection = $this->themeFactory->create();
        /** @var \Magento\Theme\Model\Theme $theme */
        $theme = $themeCollection->getItemById($themeId);

        // ..... I'm stuck here
    }

    return $url;
}

I need asset for the theme frontend, but I don't know how to access it. It's should be something similar to the Block/Logo's code:

$url = $this->assetRepository->getUrlWithParams(
    'images/logo.svg',
    [
        '_secure' => $this->_request->isSecure()
    ]
);

But inside the backend $this->assetRepository returns a backend asset :( and result url looks like:

http://baseurl.com/pub/static/adminhtml/Magento/backend/en_US/images/logo.svg

when I expect to get url like:

http://baseurl.com/pub/static/frontend/Magento/luma/en_US/images/logo.svg

The question is: how to get the frontend asset for specific theme in the backend?

1 Answer 1

2

Add to custom helper this dependency

\Magento\Store\Model\App\Emulation $emulation,
...
$this->emulation = $emulation;

and then in code before fetching logo run this

$this->emulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);

Once you finish what you wanted just restore original environment

$this->emulation->stopEnvironmentEmulation();
2
  • Thank you, it works great. Please, update your answer with this line: $this->emulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true), becouse the "force" parameter is set as false by-default (in this case the method does not work for the default store: if ($storeId == $this->_storeManager->getStore()->getStoreId() && !$force) { return; } gyazo.com/882f14048dbe4455acf824de241ccbb3 ). Commented Mar 15, 2017 at 10:44
  • @SiarheyUchukhlebau done
    – rafal-kos
    Commented Mar 15, 2017 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.