Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects

ELA-892: Make oe_list_item_block translatable

Open Francesco SARDARA requested to merge github/fork/libetho/ELA-892 into 1.x
1 file
+ 61
4
Compare changes
  • Side-by-side
  • Inline
@@ -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
@@ -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 = [];
@@ -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;
}
Loading