Problem/Motivation
Hi
Originally saw this on slack but now i have experienced in my dev setup
In #3372097, 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
Issue fork drupal-3409413
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
gorkagr commentedComment #7
gorkagr commentedThnks @lauriii
That one also works
Comment #8
longwaveCoding standards checker doesn't like it though:
I think there is also no guarantee that someone has used
@Translationin an annotation, I think we should add a test with:or similar
Comment #9
gorkagr commentedHi @longwave
Having only
description = "text"in the annotation causes:so yes, there is an issue with that part too
Comment #10
gorkagr commentedSomething such as
can cover the empty case, the string only and the use of @translation in the annotation.
Comment #11
longwaveI don't think this is the right solution either, as we shouldn't pass arbitrary strings to TranslatableMarkup (coding standards checker may also complain about this). I think a better solution might be to relax the types in places so we accept either TranslatableMarkup or string.
Comment #12
chebbro commentedI have this problem when i enable commerce_tax module
Comment #13
chebbro commentedOh, thanks @lauriii! It's work
Comment #14
lauriiiChanged the approach so that it doesn't require an empty translatable string
Comment #15
gorkagr commentedHi @lauriii
Applied the last patch. It works with and without the @traslation in the description and also with plugins without the description
Thnks for the patch :)
Comment #16
gorkagr commentedComment #17
freddy rodriguez+1 works perfect.
In this case, this bug limits the addition of new fields of any content type in D10.2. Should be considered critical?
Comment #20
longwaveAs per the priority criteria this is still major and not critical, I classify this as "causes a significant admin- or developer-facing bug with no workaround" but there is no data loss and the site is still usable apart from this.
Committed and pushed 19f5bdd1e3 to 11.x and 9dddb68024 to 10.2.x. Thanks!
Comment #21
keiserjb commentedThanks for getting this fix up on here. There are lots of modules that don't include a description in the annotations. This patch does fix things. I couldn't add fields on multiple sites and now I can.
Comment #22
damienmckennaThank you for this fix!