Skip to content
Snippets Groups Projects
Commit 418e2a5b authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3366706 by DamienMcKenna: Add support for media fields in media:thumbnail.

parent 91dddb0e
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ Advanced Views RSS Feed 8.x-2.x-dev, 2022-xx-xx
#3365915 by DamienMcKenna: Merge Views RSS Media submodules.
#3299453 by afsch, DamienMcKenna, Project Update Bot: Automated Drupal 10
compatibility fixes for media submodules.
#3366706 by DamienMcKenna: Add support for media fields in media:thumbnail.
Advanced Views RSS Feed 8.x-2.0, 2023-06-14
......
......@@ -5,6 +5,7 @@
* Preprocess functions for Views RSS: Media Elements module.
*/
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
......@@ -96,26 +97,27 @@ function views_rss_media_preprocess_item_thumbnail(&$variables) {
$variables['elements'] = array();
foreach ($variables['raw']['items'] as $item) {
// Start building RSS element.
$element = array(
'key' => 'media:thumbnail',
'attributes' => array(),
);
// File fields.
$file = NULL;
if (!empty($item['rendered']['#file'])) {
$file = $item['rendered']['#file'];
}
// Image fields.
if (!empty($item['rendered']['#item']->entity)) {
elseif (!empty($item['rendered']['#item']->entity)) {
$file = $item['rendered']['#item']->entity;
}
// Start building RSS element.
$element = array(
'key' => 'media:thumbnail',
'attributes' => array(),
);
// File entity found.
if (!empty($file)) {
// Image style is defined, need to link to resized version.
if ($image_style_name = $item['rendered']['#image_style']) {
$image_style = ImageStyle::load($image_style_name);
if (!empty($item['rendered']['#image_style'])) {
$image_style = ImageStyle::load($item['rendered']['#image_style']);
$element['attributes']['url'] = \Drupal::service('file_url_generator')->generateAbsoluteString($image_style->buildUri($file->getFileUri()));
}
// Normal image size.
......@@ -123,6 +125,25 @@ function views_rss_media_preprocess_item_thumbnail(&$variables) {
$element['attributes']['url'] = $file->createFileUrl(FALSE);
}
}
// Media fields.
elseif (!empty($item['rendered']['#media'])) {
// Render the output so the URL can be extracted.
$output = \Drupal::service('renderer')->renderPlain($item['rendered'], FALSE);
// Extract the URL from the output string.
preg_match('/src="(.*)" width="(.*)" height="(.*)" alt="([^"]*)"/', $output, $matches);
if (!empty($matches)) {
$element['attributes']['url'] = $matches[1];
$element['attributes']['width'] = $matches[2];
$element['attributes']['height'] = $matches[3];
// @todo Work out a better location for this.
// @code
// $element['attributes']['title'] = $matches[4];
// @endcode
}
}
// No file entity found, but something still was assigned to be displayed
// as enclosure, so we just put its value in the url attribute.
elseif (!empty($item['rendered']['#markup'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment