From de016a4d467eaeadc8b738470b38379465b71dcf Mon Sep 17 00:00:00 2001 From: LIBERT Thomas Date: Wed, 24 Jul 2024 09:47:08 +0000 Subject: [PATCH] ELA-1080: update patch: oe_whitelabel_paragraphs-238-oe_list_item_block_translatable --- .../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 bd9340ca..6efe0e85 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; @@ -190,6 +191,7 @@ function oe_whitelabel_preprocess_paragraph__oe_av_media(array &$variables): voi 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 @@ -210,8 +212,16 @@ function oe_whitelabel_preprocess_paragraph__oe_list_item_block(array &$variable foreach ($variables['paragraph']->get('field_oe_paragraphs')->referencedEntities() as $paragraph_item) { /** @var \Drupal\paragraphs\ParagraphInterface $card_paragraph */ $card_paragraph = \Drupal::service('entity.repository')->getTranslationFromContext($paragraph_item); - $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 = []; @@ -696,3 +706,49 @@ function _oe_whitelabel_paragraphs_preprocess_paragraph_media(array &$variables) $cacheability->applyTo($variables); } + +/** + * 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; +}