0

Inside a custom module I get the translated nodes like this:

$ids = Drupal::entityQuery('node')
    ->condition('type', 'event')
    ->condition('status', 1)
    ->execute();

$nodes = Drupal::entityTypeManager()->getStorage('node')->loadMultiple($ids);

$events = array_map(function ($node) {
    return Drupal::entityManager()->getTranslationFromContext($node);
}, $nodes);

and inside the template:

<div class="events">
  {% for event in events %}
    <div>
      <h4>{{ event.title.value }}</h4>
      <div class="event-type">
        {{ event.field_event_type.entity.name.value }}
      </div>
    </div>
  {% endfor %}
</div>

The event_type entity which is a taxonomy is displayed in the original language always. How can I get the translated value for the entity too?

0

You would need to prepare the taxonomy entities the same way like the nodes. But normally it's not your job to do either of this.

Use viewMultiple() instead of array_map() in the first php example:

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($ids);
$build = \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple($nodes, $view_mode);
$variables['events'] = $build;

In Twig:

{{ events }}

Customize the twig of the view mode specific event content type and other templates like the taxonomy field template if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.