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

EWPP-4565: Adapt Thumbnail class to work with AV Portal media as well. #1485

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Drupal\oe_theme_helper\Plugin\MediaDataExtractor;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Image\Image;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalPhotoSource;
use Drupal\oe_theme\ValueObject\GalleryItemValueObject;
use Drupal\oe_theme\ValueObject\ImageValueObject;
use Drupal\oe_theme\ValueObject\ImageValueObjectInterface;
Expand Down Expand Up @@ -102,12 +105,11 @@ public function getThumbnail(MediaInterface $media): ?ImageValueObjectInterface
$image_style = $this->entityTypeManager
->getStorage('image_style')
->load($configuration['thumbnail_image_style']);
$source = $media->getSource();
$url = $image_style->buildUrl($source->getMetadata($media, 'thumbnail_uri'));
$uri = $this->getImageUriFromMedia($media);

// Create a new image value object with the new src.
$thumbnail = ImageValueObject::fromArray([
'src' => \Drupal::service('file_url_generator')->transformRelative($url),
'src' => $image_style->buildUrl($uri),
] + $thumbnail->getArray());
$thumbnail->addCacheableDependency($image_style);

Expand All @@ -123,4 +125,31 @@ public function getSource(MediaInterface $media): ?string {
return $this->getThumbnail($media)->getSource();
}

/**
* Returns the image URI for a media entity.
*
* @param \Drupal\media\MediaInterface $media
* The media entity.
*
* @return string
* The image uri.
*/
protected static function getImageUriFromMedia(MediaInterface $media): string {
$source = $media->getSource();
$field_name = $source->getConfiguration()['source_field'];

if ($source instanceof Image && $media->get($field_name)->entity instanceof FileInterface) {
$uri = $media->get($field_name)->entity->getFileUri();
}
elseif ($source instanceof MediaAvPortalPhotoSource) {
$resource_ref = $media->get($field_name)->value;
$uri = 'avportal://' . $resource_ref . '.jpg';
}
else {
$uri = $source->getMetadata($media, 'thumbnail_uri');
}

return $uri;
}

}