Error TypeError: Drupal\Core\Field\FieldTypeCategory::getDescription() if a FieldType has 'description' missing in its annotation

Created on 18 December 2023, 11 months ago
Updated 18 January 2024, 10 months ago

Problem/Motivation

Hi

Originally saw this on slack but now i have experienced in my dev setup

In #3372097 ๐Ÿ“Œ Consider replacing hook_field_type_category_info with YML based plugin discovery Fixed , the file core/lib/Drupal/Core/Field/FieldTypeCategory.php was introduced for 10.2.x.

After the update to that version, if there is any custom FieldItem located in any module (custom or contrib) that does not have description = @Translation("..."), in its annotation (as it happened on my case), when going to any Content Type and adding a new field to it, the site will return the following error:

TypeError: Drupal\Core\Field\FieldTypeCategory::getDescription(): Return value must be of type Drupal\Core\StringTranslation\TranslatableMarkup, string returned in Drupal\Core\Field\FieldTypeCategory->getDescription() (line 26 of core/lib/Drupal/Core/Field/FieldTypeCategory.php).

Steps to reproduce

1. Have any custom field in Plugin/Field/FieldItem without description in the annotation
2. CR
3. Go to admin/structure/types/manage/<bundle>/fields
4. Site crashes

Proposed resolution

There should be a warning to indicate the description is missing in the annotation, so in case you are in a contrib module, the developer/maintainer adds it.
Meanwhile, in order to avoid the error, if the function is changed to something like:

  /**
   * {@inheritdoc}
   */
  public function getDescription(): TranslatableMarkup {
    return ($this->pluginDefinition['description'] instanceof TranslatableMarkup ? $this->pluginDefinition['description'] : new TranslatableMarkup($this->pluginDefinition['description']));
  }

it will avoid the error, as if the description is missing in the annotation, $this->pluginDefinition['description'] will return '' while the function expects a TranslatableMarkup object

Best

๐Ÿ› Bug report
Status

Fixed

Version

10.2 โœจ

Component
Fieldย  โ†’

Last updated about 2 hours ago

Created by

๐Ÿ‡ง๐Ÿ‡ชBelgium gorkagr

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