6

When running Magento 2.4.4 with PHP 8.1, if I use a block object's getTemplateFile method AND the block had no template set, I end up with an error that looks like this

1 exception(s):
Exception #0 (Exception): Deprecated Functionality: explode(): Passing null to parameter #2 ($string) of type string is deprecated in /usr/local/var/www/virtual-hosts/agento-2-4-4.localhost/project-community-edition/vendor/magento/framework/View/FileSystem.php on line 174

Exception #0 (Exception): Deprecated Functionality: explode(): Passing null to parameter #2 ($string) of type string is deprecated in /usr/local/var/www/virtual-hosts/magento-2-4-4.localhost/project-community-edition/vendor/magento/framework/View/FileSystem.php on line 174
<pre>#1 explode() called at [vendor/magento/framework/View/FileSystem.php:174]
#2 Magento\Framework\View\FileSystem::normalizePath() called at [vendor/magento/framework/View/FileSystem.php:119]
#3 Magento\Framework\View\FileSystem->getTemplateFileName() called at [vendor/magento/framework/View/Element/Template/File/Resolver.php:60]
#4 Magento\Framework\View\Element\Template\File\Resolver->getTemplateFileName() called at [vendor/magento/framework/View/Element/Template.php:214]

If I follow the stack trace to the code in question, I get down to here

    #File: vendor/magento/framework/View/FileSystem.php 
    public static function normalizePath($path)
    {
        $parts = explode('/', $path);
        $result = [];

where is appears Magento is not property guarding/defaulting for the new, more strict, PHP 8.1 expectations around explode.

Is there a safer method to call to get a block's template? Or is getTemplateFile something we should just expect to throw now and wrap calls in a try/catch.

1 Answer 1

0

I don't know a reason to use getTemplateFile method to the block before check is template is set to block or doesn't

I've reviewed a logic of this part in Magento and looks like it's a design bug with missed check and related exception

You can pass Magento style template path which you are want to receive to \Magento\Framework\View\Element\Template::getTemplateFile or the method will try to use defined template.

Make sense to create bug report or improvement to Magento and specify this case. I think Magento should throw exception in \Magento\Framework\View\Element\Template\File\Resolver::getTemplateFileName when empty template passed.

Is there a safer method to call to get a block's template? Or is getTemplateFile something we should just expect to throw now and wrap calls in a try/catch.

I think a best solution of this will be check is template set before run getTemplateFileName like:

if ($block->getTemplate()) {
    // your logic with $block->getTemplateFileName();
} else {
    // block doesn't have defined template
}

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.