diff --git a/src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php b/src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php index 5ccc64d8ab5667761e3c5bccc2f818f95e993c17..7cf2ea79d830f604576b370ebd451359f7c17334 100755 --- a/src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php +++ b/src/Plugin/Field/FieldFormatter/TinySliderFieldFormatter.php @@ -5,6 +5,7 @@ namespace Drupal\tiny_slider\Plugin\Field\FieldFormatter; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Random; use Drupal\Core\Cache\Cache; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemInterface; @@ -25,7 +26,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * id = "tiny_slider_field_formatter", * label = @Translation("Tiny Slider Carousel"), * field_types = { - * "image" + * "image", + * "entity_reference" * } * ) */ @@ -493,21 +495,17 @@ class TinySliderFieldFormatter extends EntityReferenceFormatterBase implements C foreach ($files as $delta => $file) { if (isset($link_file)) { - $image_uri = $file->getFileUri(); + $image_uri = $this->getEntityFileUrl($file); $url = \Drupal::service('file_url_generator')->generate($image_uri); } $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags()); - // Extract field item attributes for the theme function, and unset them - // from the $item so that the field template does not re-render them. - $item = $file->_referringItem; - $item_attributes = $item->_attributes; - unset($item->_attributes); + $item_info = $this->getEntityItem($file); $elements[$delta] = [ '#theme' => 'image_formatter', - '#item' => $item, - '#item_attributes' => $item_attributes, + '#item' => $item_info['item'], + '#item_attributes' => $item_info['attributes'], '#image_style' => $image_style_setting, '#url' => $url, '#cache' => [ @@ -533,6 +531,70 @@ class TinySliderFieldFormatter extends EntityReferenceFormatterBase implements C } + /** + * Get the FieldItem + * + * @param EntityInterface $entity + * The file or media entity. + * + * @return mixed[] + * 'item' is a \Drupal\Core\Field\FieldItemInterface. + * 'attributes' is an array of attributes. + */ + protected function getEntityItem(EntityInterface $entity) { + // Extract field item attributes for the theme function, and unset them + // from the $item so that the field template does not re-render them. + $item = $entity->_referringItem; + $item_attributes = $item->_attributes; + unset($item->_attributes); + + if ($entity->getEntityTypeId() === 'media') { + $file_id = $entity->getSource()->getSourceFieldValue($entity); + $file = \Drupal::entityTypeManager()->getStorage('file')->load($file_id); + // @see BlazyFormatterBase::getEntitiesToView(). + $source_field = $entity->getSource()->getConfiguration()['source_field']; + $file_meta = $entity->get($source_field)->getValue()[0] ?? []; + $item = (object) [ + 'target_id' => $file->id(), + 'alt' => $file_meta['alt'] ?? '', + 'title' => $file_meta['title'] ?? '', + 'width' => intval($file_meta['width'] ?? '0'), + 'height' => intval($file_meta['height'] ?? '0'), + 'entity' => $file, + '_loaded' => TRUE, + '_is_default' => TRUE, + ]; + } + + return [ + 'item' => $item, + 'attributes' => $item_attributes, + ]; + } + + /** + * Fetch the URL of the file attached to this entity. + * + * Works with file or media entities. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + * + * @return \Drupal\Core\Url|null + * The file URL if available. + */ + protected function getEntityFileUrl(EntityInterface $entity): ?Url { + if ($entity->getEntityTypeId() === 'file') { + return $entity->getFileUri(); + } + if ($entity->getEntityTypeId() === 'media') { + $file_id = $entity->getSource()->getSourceFieldValue($entity); + $file = \Drupal::entityTypeManager()->getStorage('file')->load($file_id); + return $file->getFileUri(); + } + return NULL; + } + /** * Generate the output appropriate for one field item. *