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-892: Make oe_list_item_block translatable #238

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
65 changes: 61 additions & 4 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 @@ -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
Expand All @@ -234,11 +236,20 @@ 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;
$card_image_item = $card_paragraph->get('field_oe_image')->first();
$card_image = $card_image_item ? ImageValueObject::fromImageItem($card_image_item) : NULL;
// Get the paragraph translation.
$card_paragraph = \Drupal::service('entity.repository')
->getTranslationFromContext($sub_paragraph->entity, $paragraph->language()->getId());
// Retrieve image.
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 @@ -660,3 +671,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;
}