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",
in both TimeRangeType.php and TimeType.php
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | time_field-fielditembase-category-cannot-be-translatable.patch | 1.47 KB | murat_kekic |
| #6 | time_field-fielditembase-category-cannot-be-translatable.patch | 2.38 KB | mehdib4 |
Issue fork time_field-3477472
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
srjoshComment #4
bramdriesenI 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
Comment #5
lazzyvn commentedPlease change it is very urgent on drupal 11
change annotation Plugin/Field/FieldType/TimeRangeType.php
to php attributes
and Plugin/Field/FieldType/TimeType.php
to php attributes
Comment #6
mehdib4 commentedComment #7
nagy.balint commentedThank you!
The patch in #6 solved the error.
It is indeed critical as it breaks the add field UI in Drupal 11.
Comment #8
bramdriesenNeeds an issue fork.
Is this backwards compatible? As the current release supports
8.8910and11Comment #9
bramdriesenComment #10
nagy.balint commentedI think only 10.2+ since it uses php attributes.
https://www.drupal.org/docs/drupal-apis/plugin-api/attribute-based-plugins
Comment #11
bramdriesenI 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.
Comment #12
murat_kekic commentedThe 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.
Comment #13
flyke commentedI applied the MR19 on time_field 2.1.3 on Drupal 11.1.4 and the error is fixed.
Comment #14
rajmataj commentedPatch #12 confirmed working on stable version 2.1.3 / Drupal 11.1.5
Tested environment:
- Drupal 11.1.5 (Composer-based install)
- PHP 8.3 (via DDEV)
- MariaDB 10.11
- cweagans/composer-patches ^1.7.3
This was tested on a virgin, new install of Drupal without any other modules. I initially tried patch #6, which updates the plugin definitions to use PHP 8 attributes (e.g.,
#[FieldType(...)]). That patch introduces a reference to aTimeFieldItemListclass:list_class: TimeFieldItemList::classHowever, that class is missing from the module, which caused a fatal error when attempting to add a Time field:
I then applied patch #12, which only changes the category value in the annotation from:
to:
This patch applied cleanly and resolved the following error when adding a Time field:
After applying patch #12, I was able to:
- Add a Time field to the default Article and Basic Page content types
- Create and save nodes using the field
- Rebuild caches with no errors
Note: This issue affects the current stable release, 2.1.3, which is listed as Drupal 11-compatible on the project page. That version fails on Drupal 11 unless patch #12 is applied. Recommending the patch be committed and/or the compatibility info updated.
Thanks to everyone involved — this patch works well.
Comment #15
saganakat commentedI applied the MR19 on time_field 2.1.3 on Drupal 11.1.6 and I confirm the error is fixed.
Comment #16
andras_szilagyi commentedThe fix worked for me aswell
Comment #17
bramdriesenThis was fixed in #3523013: AssertionError: "General" must be defined in MODULE_NAME.field_type_categories.yml in assert() (line 183 of core/lib/Drupal/Core/Field/FieldTypePluginManager.php).. Assigning credits for the work done here.
Comment #18
bramdriesen