FieldItemBase category cannot be translatable

Created on 27 September 2024, 5 months ago

Problem/Motivation

In FieldItemBase classes, the 'category' in the @FieldType cannot be translatable, nor capitalized.

Capitalization throws the following error:

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

Trying to translate it throws this error:

TypeError: Cannot access offset of type Drupal\Core\StringTranslation\TranslatableMarkup in isset or empty in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 45 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).

Steps to reproduce

In Drupal 11.0.4, with version 2.1.3 of this module, add a field to an existing content type.

Proposed resolution

Change the existing

 * @FieldType(
 *   category= @Translation("General"),

to

 * @FieldType(
 *   category= "general",
πŸ› Bug report
Status

Active

Version

2.1

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States srjosh

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

Merge Requests

Comments & Activities

  • Issue created by @srjosh
  • πŸ‡ΊπŸ‡ΈUnited States srjosh
  • Pipeline finished with Success
    5 months ago
    Total: 169s
    #295042
  • πŸ‡§πŸ‡ͺBelgium BramDriesen Belgium πŸ‡§πŸ‡ͺ

    I think this actually requires a .yml change like the error indicates rather as a change to the FieldType.

    Been digging a bit in how core does this and I see no difference in the definition.

    Here is a similar issue for a different contrib module: https://www.drupal.org/project/geolocation/issues/3449270 πŸ› AssertionError: "Spatial fields" must be defined in MODULE_NAME.field_type_categories.yml Fixed

  • πŸ‡«πŸ‡·France lazzyvn paris

    Please change it is very urgent on drupal 11
    change annotation Plugin/Field/FieldType/TimeRangeType.php

     * @FieldType(
     *   category= @Translation("General"),
     *   id = "time_range",
     *   label = @Translation("Time Range"),
     *   description = @Translation("Time range field"),
     *   default_widget = "time_range_widget",
     *   default_formatter = "time_range_formatter"
     * )
    

    to php attributes

    #[FieldType(
      id: "time_range",
      label: new TranslatableMarkup("Time Range"),
      description: [
        new TranslatableMarkup("Time range field"),
      ],
      category: "date_time",
      default_widget: "time_range_widget",
      default_formatter: "time_range_formatter",
    )]
    
    

    and Plugin/Field/FieldType/TimeType.php

    @FieldType(
     *   category= @Translation("General"),
     *   id = "time",
     *   label = @Translation("Time"),
     *   description = @Translation("Time field"),
     *   default_widget = "time_widget",
     *   default_formatter = "time_formatter",
     *   list_class = "\Drupal\time_field\Plugin\Field\FieldType\TimeFieldItemList"
     * )

    to php attributes

    #[FieldType(
      id: "time",
      label: new TranslatableMarkup("Time"),
      description: [
        new TranslatableMarkup("Time field"),
      ],
      category: "date_time",
      default_widget: "time_widget",
      default_formatter: "time_formatter",
      list_class: TimeFieldItemList::class,
    )]
    
    
  • Status changed to Needs work 2 months ago
  • πŸ‡ΉπŸ‡³Tunisia mehdib4
  • πŸ‡­πŸ‡ΊHungary nagy.balint

    Thank you!

    The patch in #6 solved the error.

    It is indeed critical as it breaks the add field UI in Drupal 11.

  • πŸ‡§πŸ‡ͺBelgium BramDriesen Belgium πŸ‡§πŸ‡ͺ

    Needs an issue fork.

    Is this backwards compatible? As the current release supports 8.8 9 10 and 11

  • πŸ‡§πŸ‡ͺBelgium BramDriesen Belgium πŸ‡§πŸ‡ͺ
  • πŸ‡§πŸ‡ͺBelgium BramDriesen Belgium πŸ‡§πŸ‡ͺ

    I guess it's ok to drop 8.8 and 9 support as they are both EOL. Plus a bump to 10.2 is also ok I guess.

  • The patch in #6 adds `list_class: TimeFieldItemList::class,` which doesn't exists in the current codebase. While it fixes the initial issue it introduces a new bug.

    I'm attaching the patch from the merge request that only updates the existing annotations.

    Just wanted to mention the change records β†’ for the cause of this issue. It explains a way to add backward compatibility in "BC layer" section, but switching to attributes will break it anyway.

Production build 0.71.5 2024