diff --git a/src/Plugin/Field/FieldFormatter/FieldLink.php b/src/Plugin/Field/FieldFormatter/FieldLink.php index eaf2d8a..b9b228b 100644 --- a/src/Plugin/Field/FieldFormatter/FieldLink.php +++ b/src/Plugin/Field/FieldFormatter/FieldLink.php @@ -2,6 +2,7 @@ namespace Drupal\field_formatter\Plugin\Field\FieldFormatter; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; @@ -52,9 +53,11 @@ class FieldLink extends FieldWrapperBase { * The field_type plugin manager. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager object for retrieving the correct language code. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormatterPluginManager $formatter_plugin_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, LanguageManagerInterface $language_manager) { - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $formatter_plugin_manager, $field_type_plugin_manager); + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormatterPluginManager $formatter_plugin_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, LanguageManagerInterface $language_manager, EntityRepositoryInterface $entity_repository) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $formatter_plugin_manager, $field_type_plugin_manager, $entity_repository); $this->languageManager = $language_manager; } @@ -72,7 +75,8 @@ class FieldLink extends FieldWrapperBase { $configuration['third_party_settings'], $container->get('plugin.manager.field.formatter'), $container->get('plugin.manager.field.field_type'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('entity.repository') ); } diff --git a/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php b/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php index 9484c1f..4ef49ed 100644 --- a/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php +++ b/src/Plugin/Field/FieldFormatter/FieldWrapperBase.php @@ -3,6 +3,7 @@ namespace Drupal\field_formatter\Plugin\Field\FieldFormatter; use Drupal\Core\Entity\Entity\EntityViewDisplay; +use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -49,6 +50,13 @@ abstract class FieldWrapperBase extends FormatterBase implements ContainerFactor */ protected $fieldTypePluginManager; + /** + * The entity repository. + * + * @var \Drupal\Core\Entity\EntityRepositoryInterface + */ + protected $entityRepository; + /** * Constructs a FieldFormatterWithInlineSettings object. * @@ -70,11 +78,14 @@ abstract class FieldWrapperBase extends FormatterBase implements ContainerFactor * The formatter plugin manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_plugin_manager * The field_type plugin manager. + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository + * The entity repository. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormatterPluginManager $formatter_plugin_manager, FieldTypePluginManagerInterface $field_type_plugin_manager) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FormatterPluginManager $formatter_plugin_manager, FieldTypePluginManagerInterface $field_type_plugin_manager, EntityRepositoryInterface $entity_repository) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->formatterPluginManager = $formatter_plugin_manager; $this->fieldTypePluginManager = $field_type_plugin_manager; + $this->entityRepository = $entity_repository; } /** @@ -90,7 +101,8 @@ abstract class FieldWrapperBase extends FormatterBase implements ContainerFactor $configuration['view_mode'], $configuration['third_party_settings'], $container->get('plugin.manager.field.formatter'), - $container->get('plugin.manager.field.field_type') + $container->get('plugin.manager.field.field_type'), + $container->get('entity.repository') ); } @@ -288,7 +300,7 @@ abstract class FieldWrapperBase extends FormatterBase implements ContainerFactor */ protected function getFieldOutput(FieldItemListInterface $items, $langcode) { /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ - $entity = $items->getEntity(); + $entity = $this->entityRepository->getTranslationFromContext($items->getEntity(), $langcode); $build = $this->getViewDisplay($entity->bundle())->build($entity); return $build[$this->fieldDefinition->getName()] ?? [];