1

TypeError: addslashes(): Argument #1 ($string) must be of type string, null given in vendor/magento/framework/Escaper.php:440

2
  • Could you please provide a bit more information. When does this error occur? What's the stack trace? It appears that null is passed to the escaper.
    – Daniel
    Commented May 17, 2023 at 7:45
  • @Daniel This error occurs when we had run the website after the upgrade. I have also checked not to show any third-party module error. Commented May 17, 2023 at 8:19

4 Answers 4

7

Without modify core code - simple update the core_config_data table with this sql statement:

UPDATE core_config_data SET value = 'welcome' WHERE path ='design/header/welcome'
2

Solution: Add the below line in the di.xml file.

<preference for="Magento\Theme\Block\Html\Header" type="Vendor\Packagename\Block\Html\Header" />

Create a new Header.php file under the Vendor\Packagename\Block\Html folder.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Packagename\Block\Html;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Escaper;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\ScopeInterface;

/**
 * Html page header block
 *
 * @api
 * @since 100.0.2
 */
class Header extends \Magento\Theme\Block\Html\Header
{
    /**
     * @var Escaper
     */
    private $escaper;

    /**
     * @param Context $context
     * @param array $data
     * @param Escaper|null $escaper
     */
    public function __construct(
        Context $context,
        array $data = [],
        Escaper $escaper = null
    ) {
        parent::__construct($context, $data, $escaper);
        $this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
    }

    /**
     * Current template name
     *
     * @var string
     */
    protected $_template = 'Magento_Theme::html/header.phtml';

    /**
     * Retrieve welcome text
     *
     * @return string
     */
    public function getWelcome()
    {
        if (empty($this->_data['welcome'])) {
            $this->_data['welcome'] = $this->_scopeConfig->getValue(
                'design/header/welcome',
                ScopeInterface::SCOPE_STORE
            );
        }
        return $this->escaper->escapeQuote(__($this->_data['welcome'])->render(), true);
    }
}
1

File : vendor/magento/module-theme/Block/Html/Header.php

Replace from

$this->_data['welcome'] = $this->_scopeConfig->getValue('design/header/welcome', ScopeInterface::SCOPE_STORE );

To

$this->_data['welcome'] = (string)$this->_scopeConfig->getValue( 'design/header/welcome', ScopeInterface::SCOPE_STORE );

https://github.com/magento/magento2/issues/37208

0

The code in question is designed to escape quotes, and it uses the addslashes() function to add backslashes before characters that need to be escaped. If $addSlashes is set to true, the addslashes() function is called on $data.

To avoid the TypeError, you could add a conditional check to ensure $data is not null before calling addslashes():

Go to file /public_html/vendor/magento/framework/Escaper.php > go to line 437 and replace this code

public function escapeQuote($data, $addSlashes = false) { if ($addSlashes === true) { $data = addslashes($data); } return htmlspecialchars($data, $this->htmlSpecialCharsFlag, null, false); }

With this code

public function escapeQuote($data, $addSlashes = false) { if ($data !== null) { if ($addSlashes === true) { $data = addslashes($data); } } else { // Optionally, handle null values here, or log them for debugging $data = ''; } return htmlspecialchars($data, $this->htmlSpecialCharsFlag, null, false); }

Save file php bin/magento cache:clean php bin/magento cache:flush php bin/magento setup:di:compile

Issue Resolved!

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.