diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php index 4957688..c0fe36d 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceInlineSettingsFormatter.php @@ -3,6 +3,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldFormatter; use Drupal\Core\Entity\Entity\EntityViewDisplay; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; @@ -50,6 +51,13 @@ class EntityReferenceInlineSettingsFormatter extends EntityReferenceFormatterBas protected $formatterPluginManager; /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -62,7 +70,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['view_mode'], $configuration['third_party_settings'], $container->get('entity_field.manager'), - $container->get('plugin.manager.field.formatter') + $container->get('plugin.manager.field.formatter'), + $container->get('entity_type.manager') ); } @@ -87,11 +96,14 @@ public static function create(ContainerInterface $container, array $configuratio * The entity field manager. * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager * The formatter plugin manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityFieldManager $entity_field_manager, FormatterPluginManager $formatter_plugin_manager) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityFieldManager $entity_field_manager, FormatterPluginManager $formatter_plugin_manager, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->entityFieldManager = $entity_field_manager; $this->formatterPluginManager = $formatter_plugin_manager; + $this->entityTypeManager = $entity_type_manager; } /** @@ -146,8 +158,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { protected function getAvailableFieldNames(FormStateInterface $form_state) { $entity_type_id = $this->fieldDefinition->getSetting('target_type'); // We always show the entity label as an option to be selected. - // @TODO How to retrieve this dynamically? - $field_names = $entity_type_id === 'media' ? ['name' => $this->t('Name')] : ['title' => $this->t('Title')]; + $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); + $label_key = $entity_type->getKey('label'); + $field_names = [$label_key => $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id)[$label_key]->getLabel()]; /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info */ $bundle_info = \Drupal::service('entity_type.bundle.info'); $target_bundles = $this->fieldDefinition->getSetting('handler_settings')['target_bundles'] === NULL ? array_keys($bundle_info->getBundleInfo($entity_type_id)) : $this->fieldDefinition->getSetting('handler_settings')['target_bundles']; @@ -159,7 +172,7 @@ function (FieldDefinitionInterface $field_definition) use ($show_basefields) { return $field_definition->getLabel(); } }, - \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $value) + $this->entityFieldManager->getFieldDefinitions($entity_type_id, $value) ); $field_names = array_merge($field_names, array_filter($bundle_field_names)); }