TypeError: addslashes(): Argument #1 ($string) must be of type string, null given in vendor/magento/framework/Escaper.php:440
4 Answers
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'
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);
}
}
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 );
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!
null
is passed to the escaper.