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 main in 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

Command icon 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

penyaskito created an issue. See original summary.

penyaskito’s picture

Issue summary: View changes

Verified this is the only template doing something like that.

penyaskito’s picture

Issue summary: View changes

penyaskito’s picture

Status: Active » Needs review
penyaskito’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems this came over from the gin move over to core? Either way the error does go away with the change.

penyaskito’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#3502789: The "Unpublished" badge for an unpublished translation is green

This 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.

alexpott’s picture

I think we need a different solution to the original problem in #3502789: The "Unpublished" badge for an unpublished translation is green

berdir’s picture

My 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?

alexpott’s picture

@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.

berdir’s picture

I 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?

berdir’s picture

Also, 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...)

jatingupta40’s picture

In 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.

poker10’s picture

FWIW, the same warning is also displayed on /admin/content/files page, but will disappear on a second page load (until caches are cleared again).