Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELA-1080: update patch: oe_whitelabel_paragraphs-238-oe_list_item_blo… #297

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions modules/oe_whitelabel_paragraphs/oe_whitelabel_paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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;
}