D10.2.0: public function getDescription(): TranslatableMarkup error

Created on 18 December 2023, 12 months ago
Updated 19 December 2023, 12 months ago

Problem/Motivation

In Drupal 10.2.0 I am getting an error 500 when I try to add a new field

Uncaught PHP Exception TypeError: "Drupal\Core\Field\FieldTypeCategory::getDescription():
Return value must be of type Drupal\Core\StringTranslation\TranslatableMarkup, string returned"
at core/lib/Drupal/Core/Field/FieldTypeCategory.php line 26

The function that errors is public function getDescription(): TranslatableMarkup. As far as I could debug the description returned a empty string.

Steps to reproduce

Update to Drupal 10.2.0 and try to add a field.

Proposed resolution

As a workaround I patched Drupal Core:

diff --git a/core/lib/Drupal/Core/Field/FieldTypeCategory.php b/core/lib/Drupal/Core/Field/FieldTypeCategory.php
index 168616102..435ab579d 100644
--- a/core/lib/Drupal/Core/Field/FieldTypeCategory.php
+++ b/core/lib/Drupal/Core/Field/FieldTypeCategory.php
@@ -23,7 +23,9 @@ public function getLabel(): TranslatableMarkup {
    * {@inheritdoc}
    */
   public function getDescription(): TranslatableMarkup {
-    return $this->pluginDefinition['description'];
+    $description = $this->pluginDefinition['description'];
+    if (gettype($description) == "string") return $this->pluginDefinition['label'];
+    return $description;
   }
 
   /**

Nasty solution: when $this->pluginDefinition['description'] is a string return $this->pluginDefinition['label'] which is of the required type TranslatableMarkup.

🐛 Bug report
Status

Closed: duplicate

Version

10.2

Component
Field 

Last updated 1 day ago

Created by

🇳🇱Netherlands Jan-E

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

Comments & Activities

Production build 0.71.5 2024