Problem/Motivation
Monolingual site just installed, enable experimental admin_theme, go to /admin/people, a warning is issued:
Deprecated function: Using null as an array offset is deprecated, use an empty string instead in Drupal\Core\Entity\ContentEntityBase->hasTranslation() (line 984 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
Steps to reproduce
- Install Standard profile from
mainin English - Log in.
- Enable the experimental
admin_theme. - Set it as your administration theme.
- Go to
/admin/people
You're welcomed with Deprecated function: Using null as an array offset is deprecated, use an empty string instead in Drupal\Core\Entity\ContentEntityBase->hasTranslation() (line 984 of core/lib/Drupal/Core/Entity/ContentEntityBase.php).
No language aside of English is present in the system, but
core/themes/admin/templates/views/views-view-field--status.html.twig uses:
{% set entity = row._entity %}
{% if entity.hasTranslation(row.node_field_data_langcode) %}
{% set entity = row._entity.getTranslation(row.node_field_data_langcode) %}
{% endif %}
which ends up being hasTranslation(langcode: NULL)
Proposed resolution
I'm not against hardening getTranslation(), but the actual problem is that the presentation layer shouldn't mangle with entity translations. row._entity will already have the entity in the negotiated language. And if that's not the case, is not a twig template who should fix that.
So: we should just delete
{% if entity.hasTranslation(row.node_field_data_langcode) %}
{% set entity = row._entity.getTranslation(row.node_field_data_langcode) %}
{% endif %}
from core/themes/admin/templates/views/views-view-field--status.html.twig.
Verified this is the only template doing something like that.
Remaining tasks
- MR.
- Does this need tests? I don't think so.
User interface changes
Ideally none. No warnings are issued.
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
AI disclosure
None. Zero. Nada.
Issue fork drupal-3580733
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
penyaskitoVerified this is the only template doing something like that.
Comment #3
penyaskitoComment #5
penyaskitoComment #6
penyaskitoComment #7
smustgrave commentedSeems this came over from the gin move over to core? Either way the error does go away with the change.
Comment #8
penyaskitoComment #9
alexpottThis was added to fix a bug - see #3502789: The "Unpublished" badge for an unpublished translation is green - yes it is causing problems elsewhere and perhaps is not the correct fix but we should cause this.
Comment #10
alexpottSee also #3576477: Langcode not correctly detected in field--status which leads to PHP8.5 deprecation issue which is the same issue just for gin.
Comment #11
alexpottI think we need a different solution to the original problem in #3502789: The "Unpublished" badge for an unpublished translation is green
Comment #12
berdirMy suggestion would be that the theme shouldn't work what is apparently bug/inconsistency between views/entity translations. The whole template seems a bit problematic. It comes with a hardcoded assumption that the published status field is called "status" and that the entity for it does have an isPublished() method.
I'd suggest creating a new bug report in core about that, investigate what exactly is going on and work on a better resolution of this?
Comment #13
alexpott@berdir I agree - I think we should remove core/themes/default_admin/templates/views/views-view-field--status.html.twig entirely and open an issue to add it back fixing the multilingual issue in views. The entity should be in the language that the row used but it might be tricky to change. And definitely we should not be working around this in the theme layer.
Comment #14
berdirI was also thinking that removing this entirely is something to consider. We would lose the formatting of the status completely though? I'm personally fine with that. removing a feature in default_admin that existed in gin in contrib isn't a regression, but I don't know who exactly needs to make that decision.
Views already has formatting options to display this as Yes/No, a checkbox, or something else, a "Badge" could be another option there?
Comment #15
berdirAlso, we have this "entity status badge" thing as a component in the navigation module for the top bar. This is essentially the same, could be reused if we push it up somewhere (where though...)
Comment #16
jatingupta40 commentedIn addition to the Twig template fix, I'd suggest also hardening `hasTranslation()` in `ContentEntityBase` to guard against null langcodes:
In `/core/lib/Drupal/Core/Entity/ContentEntityBase.php`, add a null coalescing assignment at the start of `hasTranslation()`:
$langcode ??= '';This would make the method more defensive and prevent the deprecation from surfacing even if a null langcode is passed from other places in the future — complementing whatever fix is applied at the template or views layer.
Comment #17
poker10 commentedFWIW, the same warning is also displayed on
/admin/content/filespage, but will disappear on a second page load (until caches are cleared again).