Problem/Motivation

In the process of updating a Drupal 10 install to 11 I noticed I couldn't add fields anymore to any entity and would get the following errors:

AssertionError: "Reference" must be defined in MODULE_NAME.field_type_categories.yml in assert() (line 183 of /srv/... /core/lib/Drupal/Core/Field/FieldTypePluginManager.php).

I jumped on slack and received the following help:

This error occurs in Drupal 11+ because field categories must use machine names rather than translated labels in @FieldType annotations, or require a module_name.field_type_categories.yml file. Resolution: Change category = @Translation("Reference") to category = "reference" in the plugin annotation or define the category in a new YML file.

Basically a custom field type must define the category property in the annotation/attribute as a machine name instead of a Translatable string, and then define the {module_name}.field_type_categories.yml file and simply add the same machine name as in the Field type annotation.

#[FieldType(
  id: 'assignment_statistics',
  label: new TranslatableMarkup('Assignment statistics'),
  category: 'statistics',
{module_name}.field_type_categories.yml 
statistics:
  label: 'Statistics'
  description: 'A field that display a list of statistics.'

The file that needed adjusting was BricksTreeItem.php.

Proposed resolution

On line 16 of BricksTreeItem.php change @Translation("Reference") to category = "reference"

Comments

birchy82 created an issue. See original summary.

birchy82’s picture

Issue summary: View changes
veronicaseveryn’s picture

Status: Active » Needs review
StatusFileSize
new2.82 KB

I think the root cause was two-fold:

1. Missing .field_type_categories.yml files — Drupal 11 requires every field type category to be registered (previous fix)
2. @Translation() in category annotations — In Drupal 11, FieldStorageAddController passes $field_type['category'] directly to createInstance(), which uses it as an array key. @Translation() creates a TranslatableMarkup object, and PHP 8.1+ throws a TypeError when using objects in isset(). Core's field types use plain strings for categories (e.g., category: "plain_text").

Attached a patch