Deleted keywords can cause fatal PHP errors

Created on 8 December 2022, over 1 year ago
Updated 29 May 2023, about 1 year ago

Problem/Motivation

If a reference has a reference to a Keyword entity that has been deleted, rendering that publication as a citation causes a fatal error:

Error: Call to a member function label() on null in Drupal\bibcite_entity\Normalizer\ReferenceNormalizerBase->extractKeywords() (line 284 of modules/custom/bibcite/modules/bibcite_entity/src/Normalizer/ReferenceNormalizerBase.php).

This is due to the extractKeywords method of ReferenceNormalizerBase, which loops over the keywords reference field to extract all the keyword labels:

  protected function extractKeywords(FieldItemListInterface $field_item_list) {
    $keywords = [];

    foreach ($field_item_list as $field) {
      $keywords[] = $field->entity->label();
    }

    return $keywords;
  }

If a keyword entity is deleted, the reference to it is not automatically deleted by Drupal core (see πŸ› Remove orphaned (dangling) entity references when an entity is deleted Active ).

Steps to reproduce

  1. Configure publication detail page to use the Citation view mode for display
  2. Add a keyword and add a reference. Add the keyword to the reference
  3. Delete the keyword
  4. Attempt to view the reference detail page

Proposed resolution

Check that the keyword entity exists before attempting to call the label method on it.

Remaining tasks

User interface changes

N/A

API changes

N/A

Data model changes

N/A

πŸ› Bug report
Status

Closed: duplicate

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024