Hi there,

I just wanted to report, that this module breaks Drupal with this error:

Cannot access offset of type Drupal\\Core\\StringTranslation\\TranslatableMarkup

Drupal Version: 11.3.6

Steps to reproduce:

- Install the module
- In any content type trying to add a new field results in this error

The same issue has been reported numerous times in other modules, e.g.: https://www.drupal.org/project/butils/issues/3486979

I don't really need this module, just wanted to briefly test it. But anyway, you might want to look into this.

Comments

marcushenningsen created an issue. See original summary.

kamiviolet’s picture

It looks like to me distantly related to this issue:
https://www.drupal.org/project/drupal/issues/3495587

In general, although annotation-based plugins are backward compatible, it seems translation only works with attributes-based and it is buggy with annotations-based for the current Drupal version.

Using annotations work ONLY if taking down all @Translation() out of the definitions.

Proposed solution

Use attributes to register a plugin

#[FieldFormatter(
  id: "simple_time_formatter_12h_lower",
  label: new TranslatableMarkup("Time — 12-hour lowercase am/pm"),
  field_types: [
    "simple_time_type"
  ]
)]

Instead of using annotations

 * @FieldFormatter(
 *   id = "simple_time_formatter_12h_lower",
 *   label = @Translation("Time — 12-hour lowercase am/pm"),
 *   field_types = {
 *     "simple_time_type"
 *   }
 * )

Attributes-based plugin:
https://www.drupal.org/docs/drupal-apis/plugin-api/attribute-based-plugins

Annotations-based plugin:
https://www.drupal.org/docs/drupal-apis/plugin-api/annotations-based-plugins

sujan shrestha’s picture

Version: 1.3.0 » 1.4.x-dev
Status: Active » Fixed

Fixed in 1.4.x and released in 1.4.0:
https://git.drupalcode.org/project/simple_time_field/-/commit/6cc259ff24...
https://www.drupal.org/project/simple_time_field/releases/1.4.0

I could reproduce this on 1.3.0 with Drupal 11.4: the "Add field" page failed with exactly this error. The cause was the field type's category, which was a translated label (@Translation("General")) instead of the category ID, so Drupal used a TranslatableMarkup object as an array key. See also #3576503.

As @kamiviolet suggested, all plugins now use PHP attributes instead of annotations, and the category is 'general'. A functional test now opens the "Add field" page, so this is covered from now on.

Thanks @marcushenningsen for the report and @kamiviolet for the investigation.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.