Score:0

How to render the image or several images in the table render element?

fi flag

I have a form. In the last step of the form, I display in the table what was saved in the fields in the previous steps.

Where there is a comment in the code, I need to render the all images from the $taxonomy_pms_color that was saved in the previous steps.

$quote_printing_pms_colors = $quote_print_location->get('field_pms_colors')->getValue();
$pms_colors = '';

foreach ($quote_printing_pms_colors as $pms_color_value) {
  $taxonomy_pms_color_target_id = $pms_color_value['target_id'];
  $taxonomy_pms_color = $this->entityTypeManager->getStorage('taxonomy_term')->load($taxonomy_pms_color_target_id);
  $image_uri = $taxonomy_pms_color->get('field_image')->entity->getFileUri();
}

$print_locations_rows[] = [
  'pms_colors' => [
    'data' => [
      //Here should be the rendered images
    ],
  ],
];

}

if (!empty($print_locations_rows)) {
  $print_locations_header = [
    'col1' => $this->t('PMS Colors'),
  ];

  $form['confirm_quote']['designs_and_finishing_wrapper']['design_wrapper'][$key]['print_location_table'] = [
    '#type' => 'table',
    '#header' => $print_locations_header,
    '#rows' => $print_locations_rows,
    '#attributes' => [
      'class' => ['my-class'],
    ],
  ];
Score:-1
fi flag

Since I have several images I can render the images like this:

$rednered_image = [];

$rednered_image[] = [
  '#theme' => 'image_style',
  '#uri' => $image_uri,
  '#style_name' => 'thumbnail',
];

I can pass the images to the <td> of the table as follows:

'thread_colors' => [
  'data' => [
    'images' => $rednered_image,
  ],
],

The updated code will look like this:

$quote_printing_pms_colors = $quote_print_location->get('field_pms_colors')->getValue();
$pms_colors_name = '';
$rednered_image = [];

foreach ($quote_printing_pms_colors as $pms_color_value) {
  $taxonomy_pms_color_target_id = $pms_color_value['target_id'];
  $taxonomy_pms_color = $this->entityTypeManager->getStorage('taxonomy_term')->load($taxonomy_pms_color_target_id);

  $taxonomy_pms_color_name = $taxonomy_pms_color->getName();
  $pms_colors_name .= '<span>' . $taxonomy_pms_color_name . ' ' . '</span>';

  if ($taxonomy_pms_color->get('field_image')->entity) {
    $image_uri = $taxonomy_pms_color->get('field_image')->entity->getFileUri();
    $rednered_image[] = [
      '#theme' => 'image_style',
      '#uri' => $image_uri,
      '#style_name' => 'thumbnail',
    ];
  }

}

$print_locations_rows[] = [
  'thread_colors' => [
    'data' => [
      'images' => $rednered_image,
    ],
  ],
];

if (!empty($print_locations_rows)) {
  $print_locations_header = [
    'col1' => $this->t('PMS Colors'),
  ];

  $form['confirm_quote']['designs_and_finishing_wrapper']['design_wrapper'][$key]['print_location_table'] = [
    '#type' => 'table',
    '#header' => $print_locations_header,
    '#rows' => $print_locations_rows,
    '#attributes' => [
      'class' => ['my-class'],
    ],
  ];
  
}
ewik avatar
fi flag
@Community I added additional details. Thanks.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.