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,
* )
labelin the entity type definition, usually the second item afteridin the entity type annotations.name.labelthis is, couldn't you trace it back? Either in a stack trace or by searching for the main string of the FormattableMarkup.