I've got nodes with hero images. The hero images are a paragraph field with unlimited cardinality, each with an image and a translatable text. For design reasons I need to print the hero images in page.html.twig
(using Twig Tweak for simplicity)
{{ drupal_field('field_banner_slide', 'node', node.id) }}
The hero slider has some options like timer, size,... which are other fields on the same host node, and pulled in during preprocess
function MYTHEME_base_preprocess_field(array &$variables, $hook) {
$parentEntity = $element['#object'];
if ($parentEntity && $variables['field_name'] == 'field_banner_slide') {
// attach JS library
// add HTML data attributes on this field based on other field values of same host node
// ...and so on...
}
}
All worked fine until my site went to the translator. My slider always returned the default lang. Next I tried setting the language in page.html.twig
{{ drupal_field('field_banner_slide', 'node', node.id, 'default', language.getId()) }}
This works for every other field - except Paragraphs. I've also tried the alternative
{{ node.field_banner_slide|view('default', language.getId()) }}
but still the same language problem.
I've already checked the source code from Twig Tweak, it seems to correctly set the language
public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL, $check_access = TRUE) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $id
? \Drupal::entityTypeManager()->getStorage($entity_type)->load($id)
: \Drupal::routeMatch()->getParameter($entity_type);
if ($entity && (!$check_access || $entity->access('view'))) {
$entity = \Drupal::service('entity.repository')
->getTranslationFromContext($entity, $langcode);
if (isset($entity->{$field_name})) {
return $entity->{$field_name}->view($view_mode);
}
}
}
I've been really digging here, but I can't find any single error. How do I get my correctly translated paragraphs?
PS: I don't want to use {{ drupal_entity('paragraph', my_field_banner_slide_target_ids, 'default', my_langcode) }}
, because I also need the preprocessing from the host field.