From fa13bd537de57b614318ba8c4618b7ea9a997e82 Mon Sep 17 00:00:00 2001
From: LIBERT Thomas <Thomas.LIBERT@ext.ec.europa.eu>
Date: Thu, 16 Nov 2023 10:40:38 +0000
Subject: [PATCH 1/2] ELA-892: Make oe_list_item_block translatable

---
 .../oe_whitelabel_paragraphs.module                         | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module b/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
index 845de48f..f94197ff 100644
--- a/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
+++ b/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
@@ -234,9 +234,11 @@ function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variable
   $variables['columns'] = ['two_columns' => '2', 'three_columns' => '3'][$layout_name] ?? '1';
 
   $variables['items'] = [];
-  foreach ($variables['paragraph']->get('field_oe_paragraphs') as $card_paragraph_item) {
+  foreach ($variables['paragraph']->get('field_oe_paragraphs') as $sub_paragraph) {
     /** @var \Drupal\paragraphs\ParagraphInterface $card_paragraph */
-    $card_paragraph = $card_paragraph_item->entity;
+    // Get the paragraph translation.
+    $card_paragraph = \Drupal::service('entity.repository')
+      ->getTranslationFromContext($sub_paragraph->entity, $paragraph->language()->getId());
     $card_image_item = $card_paragraph->get('field_oe_image')->first();
     $card_image = $card_image_item ? ImageValueObject::fromImageItem($card_image_item) : NULL;
 
-- 
GitLab


From fe2e2820d6294f28e599ffe3505a1050160ff8e2 Mon Sep 17 00:00:00 2001
From: LIBERT Thomas <Thomas.LIBERT@ext.ec.europa.eu>
Date: Mon, 22 Jan 2024 11:35:26 +0000
Subject: [PATCH 2/2] ELA-796: patch updated

---
 .../oe_whitelabel_paragraphs.module           | 60 ++++++++++++++++++-
 1 file changed, 58 insertions(+), 2 deletions(-)

diff --git a/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module b/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
index f94197ff..09dfc885 100644
--- a/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
+++ b/modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
@@ -16,6 +16,7 @@ use Drupal\Component\Utility\Html;
 use Drupal\Core\Cache\CacheableMetadata;
 use Drupal\Core\Entity\Entity\EntityViewDisplay;
 use Drupal\Core\Render\Element;
+use Drupal\Core\Session\AccountProxy;
 use Drupal\Core\Url;
 use Drupal\media\Entity\Media;
 use Drupal\media\MediaInterface;
@@ -217,6 +218,7 @@ function oe_whitelabel_preprocess_paragraph__oe_text_feature_media(array &$varia
 function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variables): void {
   /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
   $paragraph = $variables['paragraph'];
+  $user = $variables['user'];
 
   // @todo Use ->isEmpty() as in other preprocess functions.
   //   In the OpenEuropa team it was decided that ->isEmpty() calls should be
@@ -239,8 +241,16 @@ function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variable
     // Get the paragraph translation.
     $card_paragraph = \Drupal::service('entity.repository')
       ->getTranslationFromContext($sub_paragraph->entity, $paragraph->language()->getId());
-    $card_image_item = $card_paragraph->get('field_oe_image')->first();
-    $card_image = $card_image_item ? ImageValueObject::fromImageItem($card_image_item) : NULL;
+    // Retrieve image.
+    $card_image = null;
+    if(!$card_paragraph->get('field_oe_image')->isEmpty()) {
+      $card_image_item = $card_paragraph->get('field_oe_image')->first();
+      $card_image = $card_image_item ? ImageValueObject::fromImageItem($card_image_item) : NULL;
+    }
+    if(!$card_paragraph->get('field_oe_media')->isEmpty()) {
+      $card_image_item = $card_paragraph->get('field_oe_media')->entity;
+      $card_image = _oe_whitelabel_get_image_from_media_reference($paragraph, $card_image_item, $user);
+    }
 
     // Prepare the metas if available.
     $card_badges = [];
@@ -662,3 +672,49 @@ function oe_whitelabel_paragraphs_preprocess_paragraph__oe_gallery(&$variables)
   // it.
   $variables['title_tag'] = 'h2';
 }
+
+/**
+ * Prepares media referenced for field_oe_media into paragraph.
+ *
+ * @param \Drupal\paragraphs\Entity\Paragraph $paragraph
+ *   Paragraph object.
+ * @param \Drupal\media\MediaInterface $media
+ *   Media object.
+ * @param \Drupal\Core\Session\AccountProxy $user
+ *   AccountProxy object.
+ */
+function _oe_whitelabel_get_image_from_media_reference(
+  Paragraph $paragraph,
+  MediaInterface $media,
+  AccountProxy $user
+  ) {
+    /** @var \Drupal\media\Entity\Media $media */
+  if (!$media instanceof MediaInterface) {
+    // The media entity is not available anymore, bail out.
+    return [];
+  }
+
+  // Retrieve the correct media translation.
+  /** @var \Drupal\media\Entity\Media $media */
+  $media = \Drupal::service('entity.repository')->getTranslationFromContext($media, $paragraph->language()->getId());
+
+  // Run access checks on the media entity.
+  $access = $media->access('view', $user, TRUE);
+  if (!$access->isAllowed()) {
+    return [];
+  }
+
+  // Get the media source.
+  $source = $media->getSource();
+
+  $is_image = $source instanceof MediaAvPortalPhotoSource || $source instanceof Image;
+
+  // If it's not an image and not a video, bail out.
+  if (!$is_image) {
+    return [];
+  }
+  
+  $thumbnail = $media->get('thumbnail')->first();
+  $image = ImageValueObject::fromImageItem($thumbnail);
+  return $image;
+}
-- 
GitLab