Poll as a reference field does not get the right language

Created on 6 April 2022, about 3 years ago
Updated 8 April 2025, 5 days ago

Problem/Motivation

I have created a block type and added a poll as reference field, when I create the block using layout builder I don't get the translated version of the poll

Steps to reproduce

1- Add new poll
2- Translate the poll
3- Create Block type
4- Add poll reference field
5- Create a poll block using layout builder and fill the poll reference field with the created poll
6- Switch between the languages

Proposed resolution

Poll.module

function template_preprocess_poll_vote(&$variables) {
  $lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $form = $variables['form'];
  $variables['question'] = \Drupal::service('entity.repository')->getTranslationFromContext($form['#entity'], $lang_code)->label();
  $variables['show_question'] = !empty($form['#show_question']);
}

poll.php

  public function getOptions() {
    $lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $options = array();
    if (count($this->choice)) {
      foreach ($this->choice as $choice_item) {
        $options[$choice_item->target_id] = \Drupal::service('entity.repository')->getTranslationFromContext($choice_item->entity, $lang_code)->label();
      }
    }
    return $options;
  }
πŸ› Bug report
Status

Postponed: needs info

Version

1.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ahmadhalah

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024