Magento2: Fatal error on product-compare page when attributes not configured

Created on 11 Jan 2019  路  16Comments  路  Source: magento/magento2

Preconditions

  1. Magento 2.3.x

Steps to reproduce

  1. Add products to compare where at least one product has no value set for at least one of its comparable attributes (e.g. "Description" is empty).
  2. Go to the compare page (/catalog/product_compare/index)

Expected result

  1. Compare page should load normally, where any unconfigured attributes show "N/A" or "No".

Actual result

  1. "Fatal error: Uncaught TypeError: preg_match() expects parameter 2 to be string, object given in [site_root]/vendor/magento/module-catalog/Helper/Output.php on line 229"

The error occurs in the isDirectivesExists() method, which expects a string param. This method is called by productAttribute(), which passes $attributeHtml, which it receives as a param that is also expected to be a string.
The issue is that in app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml:119, where productAttribute() is called, the 2nd param passed is a value from \Magento\Catalog\Block\Product\Compare\ListCompare::getProductAttributeValue, which returns a \Magento\Framework\Phrase object when the value of an attribute is undefined. Therefore, an error is thrown when this object is passed through to preg_match().
The issue was triggered in commit #bdcfbc8, where a call to isDirectivesExists() is added to line 167 of app/code/Magento/Catalog/Helper/Output.php. However, this is reasonable, since a string is expected. Therefore I recommend simply modifying line 119 of list.phtml to cast the result of getProductAttributeValue() as a string, which would be appropriate, since that's what productAttribute() is expecting.

Catalog Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.3.x good first issue

Most helpful comment

Hello,
This bug is still present at version 2.3.5

If you add 1 or more products in the Compare List, and these Products have no Short Description or Description, then when you visit the Compare page you get the following Fatal Error:

Fatal error: Uncaught TypeError: preg_match() expects parameter 2 to be string, object given in /app/web/vendor/magento/module-catalog/Helper/Output.php:245 Stack trace: #0 /app/web/vendor/magento/module-catalog/Helper/Output.php(245): preg_match('/{{([a-z]{0,10}...', Object(Magento\Framework\Phrase)) #1 /app/web/vendor/magento/module-catalog/Helper/Output.php(185): Magento\Catalog\Helper\Output->isDirectivesExists(Object(Magento\Framework\Phrase)) #2 /app/web/vendor/magento/module-catalog/view/frontend/templates/product/compare/list.phtml(134): Magento\Catalog\Helper\Output->productAttribute(Object(Magento\Catalog\Model\Product\Interceptor), Object(Magento\Framework\Phrase), 'short_descripti...') #3 /app/web/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/app/web/vendor...') #4 /app/web/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\Compare\ListCompare\Interceptor), '/app/web/vendor...', Array) #5 /app/w in /app/web/vendor/magento/module-catalog/Helper/Output.php on line 245

Update:
Since this BugReport is closed and nobody answered anything so far, I made a new Bug Report here: https://github.com/magento/magento2/issues/28254

All 16 comments

Hi @Brengineer. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [x] Summary of the issue
  • [x] Information on your environment
  • [x] Steps to reproduce
  • [x] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me $VERSION instance

where $VERSION is version tags (starting from 2.2.0+) or develop branches (for example: 2.3-develop).
For more details, please, review the Magento Contributor Assistant documentation.

@Brengineer do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [x] yes
  • [ ] no

Hi @irajneeshgupta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [x] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [x] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [x] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [x] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 6. Add label Issue: Confirmed once verification is complete.

  • [x] 7. Make sure that automatic system confirms that report has been added to the backlog.

@irajneeshgupta Thank you for verifying the issue. Based on the provided information internal tickets MAGETWO-97584 were created

Hi @saphaljha. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

I have a project on 2.3.0 that is experiencing the same issue. We've reported the issue to Magento Partner Support and I'll be working with our developer team later this evening to implement the fix committed in the updated list.phtml file and verify if it resolves our reported issue as well.

Hi @Brengineer. Thank you for your report.
The issue has been fixed in magento/magento2#20231 by @vivekkumarcedcoss in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.1 release.

@magento-engcom-team give me 2.3.0 instance

Hi @0-Sony. Thank you for your request. I'm working on Magento 2.3.0 instance for you

Hi @0-Sony, here is your Magento instance.
Admin access: https://i-20229-2-3-0.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

the bug still not fixed in 2.3

the bug still not fixed in 2.3

2.3.0 or 2.3.1? The fix should be available in 2.3.1

Adding the is_string check in app/code/Magento/Catalog/view/frontend/templates/product/compare/list.phtml now causes some attribute values to not render at all.

For example, in the case of boolean attributes, \Magento\Catalog\Block\Product\Compare\ListCompare::getProductAttributeValue returns a \Magento\Framework\Phrase object, with the values of either "Yes" or "No". These values are now no longer rendered.

Yes, I agree. I did not like the given fix either. This is why I suggested in the first place to "cast the result... as a string". It is quite simple, and this way it is always rendered, but it will not cause any errors:

<?= /* @escapeNotVerified */ $helper->productAttribute($item, (string) $block->getProductAttributeValue($item, $attribute), $attribute->getAttributeCode()) ?>

Of course, to make the code a little cleaner, you could first set a variable:

<?php $attributeValueString = (string) $block->getProductAttributeValue($item, $attribute); ?>
<?= /* @escapeNotVerified */ $helper->productAttribute($item, $attributeValueString, $attribute->getAttributeCode()) ?>

Hello,
This bug is still present at version 2.3.5

If you add 1 or more products in the Compare List, and these Products have no Short Description or Description, then when you visit the Compare page you get the following Fatal Error:

Fatal error: Uncaught TypeError: preg_match() expects parameter 2 to be string, object given in /app/web/vendor/magento/module-catalog/Helper/Output.php:245 Stack trace: #0 /app/web/vendor/magento/module-catalog/Helper/Output.php(245): preg_match('/{{([a-z]{0,10}...', Object(Magento\Framework\Phrase)) #1 /app/web/vendor/magento/module-catalog/Helper/Output.php(185): Magento\Catalog\Helper\Output->isDirectivesExists(Object(Magento\Framework\Phrase)) #2 /app/web/vendor/magento/module-catalog/view/frontend/templates/product/compare/list.phtml(134): Magento\Catalog\Helper\Output->productAttribute(Object(Magento\Catalog\Model\Product\Interceptor), Object(Magento\Framework\Phrase), 'short_descripti...') #3 /app/web/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/app/web/vendor...') #4 /app/web/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\Compare\ListCompare\Interceptor), '/app/web/vendor...', Array) #5 /app/w in /app/web/vendor/magento/module-catalog/Helper/Output.php on line 245

Update:
Since this BugReport is closed and nobody answered anything so far, I made a new Bug Report here: https://github.com/magento/magento2/issues/28254

it is mg 2.3.5 p1 but I am still facing this issue

/var/www/magento/vendor/magento/module-catalog/Helper/Output.php is failing because when the description isn't present, it's a Phrase Object and not a string. Casting to (string) fixes it.

Magento\Framework\Phrase Object
(
[text:Magento\Framework\Phrase:private] => N/A
[arguments:Magento\Framework\Phrase:private] => Array
(
)
)

Here's my patchfile for 2.3.5-p1
patchfile.txt

Looking at the current source I do see this is fixed in latest versions, and #28254 referenced above also indicates this.

P.S. (Not sure how I unassigned @irajneeshgupta , sorry about that if it's an issue)

Was this page helpful?
0 / 5 - 0 ratings