Warnings thrown on entities without SH support in shorthand_metatags_alter

Created on 7 August 2024, 9 months ago
Updated 13 September 2024, 7 months ago

Problem/Motivation

If you view an entity that has Metatag support but no shorthand fields a warning will be thrown. This is flooding the logs on one of our sites.

The root cause of this comes from this line

$fields = \Drupal::service(EntityFieldManagerInterface::class)->getFieldMapByFieldType('shorthand_local')[$entity->getEntityTypeId()];

If you have the SH field on a node and you visit another entity that has metatag support but no shorthand support, then $fields will be NULL. Passing NULL to the foreach on the next line generates a PHP warning.

Steps to reproduce

This is being thrown with custom entities on our site, but I'm pretty sure this could be replicated with Taxonomy terms

  • Add SH module
  • Add SH to a node bundle
  • Create a new Vocab
  • Add Metatag support to the vocab
  • View a term in that vocab, the warnings should be thown.

Proposed resolution

Check if shorthand is enabled on the entity type
Return early if not.

Remaining tasks

Review MR
Merge

User interface changes

None

API changes

None

Data model changes

🐛 Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

🇳🇿New Zealand danielveza Brisbane, AU

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024