magento2: Exception: Warning: Trying to access array offset in... -> Calendar.php since upgrade to ICU 74.1 (PHP Intl)
Preconditions and environment
Magento version 2.4.6-P3. PHP v8.1.25. ICU 74.1 (required by PHP Intl) FreeBSD 13.2-RELEASE-p5
Steps to reproduce
Install the above versions and visit either the backend or the frontend.
Expected result
No errors in var/log/exception.log
Actual result
Errors on each store visit in exception.log:
main.CRITICAL: Exception: Warning: Trying to access array offset on value of type null in /vendor/magento/framework/View/Element/Html/Calendar.php on line 114 in /vendor/magento/framework/App/ErrorHandler.php:62
Additional information
Magento is using ‘Swedish’ as locale, and doesn’t use am or pm (24 hrs). Set on Scope -> Default Config Stores -> Configurations -> General -> Locale Options -> Locale Magento is set up with 8 web sites, stores and views.
Line 114 in Calendar.php:
$this->assign('am', $this->encoder->encode($localeData['calendar']['gregorian']['AmPmMarkers']['0']));
Triage and priority
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
About this issue
- Original URL
- State: open
- Created 7 months ago
- Reactions: 6
- Comments: 25 (15 by maintainers)
We’re also seeing this over here. Error triggers on product detail pages,
$localeData['calendar']['gregorian']['AmPmMarkers']isnullin our case.Using:
This quickfix seems to work (no idea if this is the correct way to solve it though):
@webcreative24: apply changes from https://github.com/magento/magento2/pull/38364, that works for us.
@engcom-Hotel: it definitely is reproducible.
Are you 100% sure your PHP is using ICU version 74.x? You may need to recompile the php-intl extension. Can you try my dirty test script from this comment: https://github.com/magento/magento2/issues/38214#issuecomment-1898634163 It will output the ICU version used at the top.
Or alternatively, you can run
php -i | grep 'ICU version'to view the version used.Hello @Munktells Thank you for your collaboration
Hello @hostep Thank you to your contribution here I\ll review your PR
I just wrote this (dirty) script to be put in
pub/test.phpand then openend viahttps://base-url/test.php, it goes over all locale’s supported by Magento and outputs a table to see which locale supports which amPmMarker key:Results with ICU 74.1:
Results with ICU 69.1:
If somebody wants to test with other versions of ICU, feel free to do so and post the results!
It seems like a lot of data disappeared in the latest version of ICU, no idea why, maybe this is a bug on their end?
Anyway, based on the results from ICU 74.1, it looks like no key is supported by each locale, the key that has the most support is
AmPmMarkersAbbrat the moment, so if we decide to keep these 2 lines of code, I vote we use that one. But add a fallback tonullin case it doesn’t exist.So then I would propose we make this change to the core code:
Update: I’ve changed my mind again, see my new proposal in #38364
Same issue here with Magento 2.4.7 … 😃
Create file patches/composer/github-issue-38364.diff
Add to composer.json
Many thanks to @hostep! 💯
Yes @hostep,
I compiled the PHP with ICU version 74.1, and after compilation, running the below command was showing ICU version as 74.1:
`php -i | grep 'ICU version``
Hello @Munktells @hostep,
We have tried to reproduce the issue with the mentioned prerequisites and the issue is reproducible for us with the below-mentioned steps:
Manual testing scenarios (*)
app/code/Magento/Theme/view/frontend/templates/js/calendar.phtmland add this line at the very bottom:<ul><li>am: <?= $am; ?></li><li>pm: <?= $pm; ?></li></ul>bin/magento config:set general/locale/code lv_LVPlease refer to the below screenshot for reference:
Hence confirming the PR.
Thanks
Same issue here on php 8.1 and magento 2.4.6-p3
FYI this also happens on Alpine 3.19 with the following versions of ICU:
Locale tested:
fr. Page tested: login page of admin area.After some more looking into and seeing how the ICU data changed between versions 73.2 and 74.1 (this is the commit), it looks like we should use
AmPmMarkersAbbrinstead ofAmPmMarkers.Also, using the index as a string (
['0']) never ever worked, it should have been used as a int ([0])So I would suggest this change now:
However, this code in core Magento has existed for over 9 years (was introduced here), and never worked, also the datetime picker widget only shows dates and not times, so I’m not sure if we even need that
am/pmnotation at all. So maybe we should just remove those two lines and be done with it?Hi, Any updates on this error? I am recently getting this error on M2.4.5-p4 and php 8.1