Magento2.3 Upgrade Error – Fix Page Title Could Not Be Converted to String

magento-upgrademagento2.3renderer

We just recently tested upgrading from M2.2.4 to M2.3.7 and have ironed out most of the issues, however when trying to load a page, we get the following error:

Fatal error: Uncaught Error: Object of class
Magento\Framework\View\Page\Title could not be converted to string in
/app/code/CleverSoft/Base/Model/Renderer.php:141
Stack trace: #0
/app/code/CleverSoft/Base/Model/Renderer.php(141):
str_replace(Array, Array, '<meta name="%na…') #1
/app/code/CleverSoft/Base/Model/Renderer.php(112):
CleverSoft\Base\Model\Renderer->renderMetadata() #2
/vendor/magento/framework/View/Result/Page.php(250):
CleverSoft\Base\Model\Renderer->renderHeadContent() #3
/vendor/magento/framework/View/Result/Layout.php(171):
Magento\Framework\View\Result\Page->render(Object(Magento\Framework\App\Response\Http\Interceptor))
#4 /vendor/magento/framework/Interception/Interceptor.php(58):
Ma in
/app/code/CleverSoft/Base/Model/Renderer.php
on line 141

The function it is referring to within Renderer.php is:

public function renderMetadata()
    {
        $result = '';
        foreach ($this->pageConfig->getMetadata() as $name => $content) {
            $metadataTemplate = $this->getMetadataTemplate($name);
          if (!$metadataTemplate) {
                continue;
            }
            $content = $this->processMetadataContent($name, $content);
            if ($content) {
               $result .= str_replace(['%name', '%content'], [$name, $content], $metadataTemplate);
            }
        }
        return $result;
    }

With the mentioned line 141 being $result .= str_replace(['%name', '%content'], [$name, $content], $metadataTemplate);

Just playing with it and changing the code to:

public function renderMetadata()
    {
        $result = '';
        return $result;
    }

Allows the pages to load but, some elements end up broken like the navigation menus and whatnot. The above is not a solution, so anyone has any ideas. What did I do to allow for the object to be converted to a string?

Thanks!

Best Answer

You need to try this code.

protected $emailfilter;

public function __construct(
        .....
        \Magento\Email\Model\Template\Filter $filter,
        .....)
    {
        $this->emailfilter = $filter;
    }

public function renderMetadata()
    {

        $result = '';
        foreach ($this->pageConfig->getMetadata() as $name => $content) 
        {
            $metadataTemplate = $this->getMetadataTemplate($name);
            if(!$metadataTemplate) 
            {
               continue;
            }
            $content = $this->processMetadataContent($name, $content);
            if ($content) 
            {
               $this->emailfilter->setVariables(['%name' => $name,'%content' => $content]);
               $result = $this->emailfilter->filter($metadataTemplate);
            }
         }
    return $result;
    }
Related Topic