0

I would like to add a reference field to my custom entity to a stock entity (eg. user) using the Field UI and the stock entity reference field (not my own custom field). The custom entity is perfectly functional. However, when I try to add it to another entity, I get an error on the Field UI page, from Drupal\Component\Utility\Html::escape() which leads back to Drupal\Core\StringTranslation\TranslatableMarkup.php:195. Basically, what happens is that the placeholder function (Drupal/Component/Render/FormattableMarkup.php:187) receives a

['@label' => NULL]

item while it tries to assemble the current entities for the display of the defaults. I do have a label entry in my entity_keys that points to the field I use as a name. I also have common_reference_target set to TRUE.

Is there something else I miss to be able to use my entity with the stock reference field?

Not that I would condone such behavior but if I happen to temporarily change FormattableMarkup.php:187 to

$args[$key] = static::placeholderEscape($value ?? '');

the reference field can be added and it will keep working, even if this modification in core is reverted later.

Update:

 * @ContentEntityType(
 *   id = "country",
 *   label = @Translation("Country"),
 *   label_collection = @Translation("Countries"),
 *   label_singular = @Translation("country"),
 *   label_plural = @Translation("countries"),
 *   label_count = @PluralTranslation(
 *     singular = "@count country",
 *     plural = "@count countries",
 *   ),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "list_builder" = "Drupal\xxx\Controller\CountryListBuilder",
 *     "form" = {
 *       "add" = "Drupal\xxx\Form\Country\CountryForm",
 *       "edit" = "Drupal\xxx\Form\Country\CountryForm",
 *       "delete" = "Drupal\xxx\Form\Country\CountryDeleteForm",
 *     },
 *     "translation" = "Drupal\content_translation\ContentTranslationHandler",
 *     "access" = "Drupal\Core\Entity\EntityAccessControlHandler",
 *   },
 *   base_table = "xxx_countries",
 *   data_table = "xxx_countries_field_data",
 *   admin_permission = "edit country",
 *   fieldable = FALSE,
 *   translatable = TRUE,
 *   internal = TRUE,
 *   entity_keys = {
 *     "id" = "country_id",
 *     "label" = "name",
 *     "langcode" = "langcode",
 *   },
 *   links = {
 *     "canonical" = "/admin/config/xxx/countries/{country}",
 *     "edit-form" = "/admin/config/xxx/countries/{country}/edit",
 *     "delete-form" = "/admin/config/xxx/countries/{country}/delete",
 *     "collection" = "/admin/config/xxx/countries/list",
 *   },
 *   common_reference_target = TRUE,
 * )
6
  • It could be that this has nothing to do with the label/name field. There is another label in the entity type definition, usually the second item after id in the entity type annotations. Commented Mar 11 at 22:04
  • Yes, there it is. But the third one, the second one is langcode. You don't say order is important... :-) I map it there to my actual field name. Commented Mar 11 at 22:07
  • You are still looking in the entity keys field mapping. I mean at the very top of the entity type annotations * @ContentEntityType( * id = "node", * label = @Translation("Content"), Commented Mar 11 at 22:09
  • I have all of that. Actually, my annotation has about all the same items as User does, I double checked. Commented Mar 11 at 22:11
  • To find out which label this is, couldn't you trace it back? Either in a stack trace or by searching for the main string of the FormattableMarkup. Commented Mar 11 at 22:21

1 Answer 1

1

There is only one hit searching t('@label':

/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/DefaultSelection.php:238

  $fields[$field_name] = $this->t('@label', ['@label' => $field_definition->getLabel()]);

So this is a missing label in the field definition when building the config form to configure the reference field selection.

1
  • That was a very good idea, thanks. A field that I treated as internal, only filled by code, not available from the UI ever, so I didn't provide a label for it. Commented Mar 11 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.