Skip to content
Snippets Groups Projects
Commit 5c954755 authored by Erik Stielstra's avatar Erik Stielstra Committed by Erik Stielstra
Browse files

Issue #2875632 by Gertjan.k, Sutharsan: Paragraph fields always display the default language

parent 1b60e4a8
No related branches found
No related tags found
No related merge requests found
...@@ -8,12 +8,14 @@ use Drupal\Core\Entity\ContentEntityInterface; ...@@ -8,12 +8,14 @@ use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterInterface; use Drupal\Core\Field\FormatterInterface;
use Drupal\Core\Field\FormatterPluginManager; use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormHelper; use Drupal\Core\Form\FormHelper;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Form\SubformStateInterface; use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
...@@ -59,6 +61,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -59,6 +61,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
*/ */
protected $routeMatch; protected $routeMatch;
/**
* The language manager
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
private $languageManager;
/** /**
* The entity to be used when displaying the block. * The entity to be used when displaying the block.
* *
...@@ -84,12 +93,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -84,12 +93,13 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match. * The current route match.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, FormatterPluginManager $formatter_plugin_manager, RouteMatchInterface $route_match) { public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, EntityFieldManagerInterface $entityFieldManager, FormatterPluginManager $formatter_plugin_manager, RouteMatchInterface $route_match, LanguageManagerInterface $languageManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entityTypeManager; $this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entityFieldManager; $this->entityFieldManager = $entityFieldManager;
$this->formatterPluginManager = $formatter_plugin_manager; $this->formatterPluginManager = $formatter_plugin_manager;
$this->routeMatch = $route_match; $this->routeMatch = $route_match;
$this->languageManager = $languageManager;
} }
/** /**
...@@ -102,7 +112,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -102,7 +112,8 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$container->get('entity_type.manager'), $container->get('entity_type.manager'),
$container->get('entity_field.manager'), $container->get('entity_field.manager'),
$container->get('plugin.manager.field.formatter'), $container->get('plugin.manager.field.formatter'),
$container->get('current_route_match')); $container->get('current_route_match'),
$container->get('language_manager'));
} }
/** /**
...@@ -370,7 +381,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -370,7 +381,7 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
$entity = $this->getEntity(); $entity = $this->getEntity();
if ($entity) { if ($entity) {
$build['field'] = $entity->get($this->configuration['field_name'])->view([ $build['field'] = $this->getTranslatedFieldFromEntity($entity)->view([
'label' => 'hidden', 'label' => 'hidden',
'type' => $this->configuration['formatter_id'], 'type' => $this->configuration['formatter_id'],
'settings' => $this->configuration['formatter_settings'], 'settings' => $this->configuration['formatter_settings'],
...@@ -383,6 +394,26 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface { ...@@ -383,6 +394,26 @@ class FieldBlock extends BlockBase implements ContainerFactoryPluginInterface {
return $build; return $build;
} }
/**
* Ensure that the field gets correctly translated into the current language
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @return \Drupal\Core\Field\FieldItemListInterface
*/
private function getTranslatedFieldFromEntity(ContentEntityInterface $entity) {
$language = $this->languageManager->getCurrentLanguage()->getId();
$field = $entity->get($this->configuration['field_name']);
if ($entity->hasTranslation($language)) {
$translatedEntity = $entity->getTranslation($language);
$adapter = EntityAdapter::createFromEntity($translatedEntity);
$field->setContext($this->configuration['field_name'], $adapter);
}
return $field;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment