1

I want to override the addDefaultAttributes function in /vendor/magento/framework/View/Page/Config/Renderer.php to remove the type="text/javascript" it inserts into the js meta tags. I have tried to do this using preference in a custom module but the changes do not apply after running upgrade/deploy. The original class uses implements RendererInterface. I have tried extending as well as keeping the implement class and both seem to not work. How do I account for this?

what I have:

/app/code/ME/framework/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
            \Magento\Framework\Component\ComponentRegistrar::MODULE,
            'ME_framework',
            __DIR__
        );

/app/code/ME/framework/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <preference for="magento\framework\View\Page\Config\Renderer" type="ME\framework\View\Page\Config\Renderer" />

</config>  

/app/code/ME/framework/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="ME_framework" setup_version="2.1.0" active="true" />
</config>

/app/code/ME/framework/View/Page/Config/Renderer.php

<?php

namespace ME\framework\View\Page\Config;

class Renderer implements \magento\framework\View\Page\Config\RendererInterface
{

    protected function addDefaultAttributes($contentType, $attributes)
    {
        switch ($contentType) {
            case 'css':
                $attributes = ' rel="stylesheet" type="text/css" ' . ($attributes ?: ' media="all"');
                break;
        }
        return $attributes;
    }
}

UPDATE: working code below using the public fuction renderHeadContent. I also had to change

<preference for="magento\framework\View\Page\Config\Renderer" type="ME\framework\View\Page\Config\Renderer" />

in di.xml to:

<preference for="Magento\Framework\View\Page\Config\Renderer" type="ME\framework\View\Page\Config\Renderer" />

Even though the directory structure had them lowercase.

Updated /app/code/ME/framework/View/Page/Config/Renderer.php

<?php

namespace ME\framework\View\Page\Config;

class Renderer extends \Magento\Framework\View\Page\Config\Renderer
{
    public function renderHeadContent()
    {
        $result = '';
        $result .= $this->renderMetadata();
        $result .= $this->renderTitle();
        $this->prepareFavicon();
        $result .= preg_replace('/  type\=\"text\/javascript\" /', '', $this->renderAssets($this->getAvailableResultGroups()));
        $result .= $this->pageConfig->getIncludes();
        return $result; 
    }
}
0

Actually protected function can not override by preference neither we can make plugin for it.

so what we can do it we can override public function where our protected function is called.

like for

addDefaultAttributes called in renderAssetHtml

renderAssetHtml called in renderAssetGroup

renderAssetGroup called in renderAssets

renderAssets is public function so what we can do it

using preference custommethod for all protected function and call in public method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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