0

If admin uses a custom domain, different from the default website, the WYSIWYG image-selector will return malformed media directives, causing broken links to images.

Expected directive: {{media url="wysiwyg/image.jpg"}}

Returned directive: {{media url="http://backend.dev/media/wysiwyg/image.jpg"}}

This is known Magento 2 bug https://github.com/magento/magento2/issues/16427, but maybe there are some workarounds until Magento 2.3 will be released?

1 Answer 1

0

I've done the following workaround that works for me.

vendor/magento/module-cms/Helper/Wysiwyg/Images.php

...
protected $scopeConfig;
...
public function __construct(
    ...
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ...
) {
    ...
    $this->scopeConfig = $scopeConfig;
}
...
public function getImageHtmlDeclaration($filename, $renderAsTag = false)
{
    $fileurl = $this->getCurrentUrl() . $filename;
/**** WORKAROUND START ****/
    $adminUrl = $this->scopeConfig->getValue('admin/url/custom');
    $baseUrl = $this->scopeConfig->getValue('web/secure/base_url');
    $fileurl = str_replace($adminUrl, $baseUrl, $fileurl);
/**** WORKAROUND END ****/
    $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $mediaPath = str_replace($mediaUrl, '', $fileurl);
    $directive = sprintf('{{media url="%s"}}', $mediaPath);
    if ($renderAsTag) {
        $html = sprintf('<img src="%s" alt="" />', $this->isUsingStaticUrlsAllowed() ? $fileurl : $directive);
    } else {
        if ($this->isUsingStaticUrlsAllowed()) {
            $html = $fileurl; // $mediaPath;
        } else {
            $directive = $this->urlEncoder->encode($directive);

            $html = $this->_backendData->getUrl(
                'cms/wysiwyg/directive',
                [
                    '___directive' => $directive,
                    '_escape_params' => false,
                ]
            );
        }
    }
    return $html;
}

It is not a good idea to modify vendor files, so I've added this into separate extension https://github.com/troublediehard/wysiwyg-custom-admin-workaround

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.