diff --git a/photoswipe.theme.inc b/photoswipe.theme.inc index 2082f63..9fa02f9 100644 --- a/photoswipe.theme.inc +++ b/photoswipe.theme.inc @@ -7,6 +7,7 @@ use Drupal\Core\Language\Language; use Drupal\image\Entity\ImageStyle; +use Drupal\media\MediaInterface; /** * Prepares variables for a Photoswipe image field formatter. @@ -18,11 +19,13 @@ use Drupal\image\Entity\ImageStyle; * * @ingroup themeable */ -function template_preprocess_photoswipe_image_formatter(array &$variables) -{ +function template_preprocess_photoswipe_image_formatter(array &$variables) { $item = $variables['item']; $settings = $variables['display_settings']; - $entity = $item->getParent()->getParent()->getValue(); + if ($item->entity instanceof MediaInterface && $item->entity->hasField($settings['photoswipe_reference_image_field'])) { + $item = $item->entity->get($settings['photoswipe_reference_image_field']); + } + $entity = $item->getParent()->getValue(); $uri = $item->entity->getFileUri(); $alt = !empty($item->alt) ? $item->alt : ''; $title = !empty($item->title) ? $item->title : ''; @@ -51,8 +54,9 @@ function template_preprocess_photoswipe_image_formatter(array &$variables) if ($image_file->isValid()) { $image_width = $image_file->getWidth(); $image_height = $image_file->getHeight(); - } else { - $image_width = $image_height = null; + } + else { + $image_width = $image_height = NULL; } $dimensions = []; @@ -71,7 +75,8 @@ function template_preprocess_photoswipe_image_formatter(array &$variables) // Set the dimensions. $style->transformDimensions($dimensions, $uri); - } else { + } + else { $path = file_create_url($uri); } @@ -95,22 +100,23 @@ function template_preprocess_photoswipe_image_formatter(array &$variables) case 'node_title': if (!empty($entity->title)) { $caption = $entity->title->value; - } else { + } + else { $caption = $alt; } break; default: - // Assume the user wants to use another node field as the caption + // Assume the user wants to use another node field as the caption. $field_view['#view_mode'] = ($settings['photoswipe_view_mode']) ? $settings['photoswipe_view_mode'] : 'default'; if (!isset($entity->{$caption_setting})) { // No such field exists. We'd better warn and use something reliable. $id = $entity->id(); $msg = "'Photoswipe Caption' is unset for field view '@fv' on node: @nid."; - \Drupal::logger('photoswipe')->warning($msg, array( + \Drupal::logger('photoswipe')->warning($msg, [ '@fv' => $field_view['#view_mode'], - '@nid' => $id - )); + '@nid' => $id, + ]); // Fallback to alt text: $caption = $alt; break; @@ -119,7 +125,8 @@ function template_preprocess_photoswipe_image_formatter(array &$variables) $caption = render($field_view); break; } - } else { + } + else { $caption = $alt; } @@ -128,7 +135,7 @@ function template_preprocess_photoswipe_image_formatter(array &$variables) $variables['attributes']['class'][] = 'photoswipe'; $variables['attributes']['data-size'] = $dimensions['width'] . 'x' . $dimensions['height']; $variables['attributes']['data-overlay-title'] = $caption; - if($image['#style_name'] === 'hide'){ + if ($image['#style_name'] === 'hide') { // Do not display if hidden is selected: $variables['attributes']['class'][] = 'hidden'; } diff --git a/src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php b/src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php index a99aea7..8396d5f 100644 --- a/src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php +++ b/src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php @@ -2,6 +2,7 @@ namespace Drupal\photoswipe\Plugin\Field\FieldFormatter; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; @@ -14,6 +15,7 @@ use Drupal\image\Entity\ImageStyle; * id = "photoswipe_field_formatter", * label = @Translation("Photoswipe"), * field_types = { + * "entity_reference", * "image" * } * ) @@ -28,6 +30,7 @@ class PhotoswipeFieldFormatter extends FormatterBase { 'photoswipe_node_style_first' => '', 'photoswipe_node_style' => '', 'photoswipe_image_style' => '', + 'photoswipe_reference_image_field' => '', 'photoswipe_caption' => '', 'photoswipe_view_mode' => '', ] + parent::defaultSettings(); @@ -71,11 +74,14 @@ class PhotoswipeFieldFormatter extends FormatterBase { 'alt' => $this->t('Image alt tag'), 'node_title' => $this->t('Entity title'), ]; - // Add the other node fields as options. - if (!empty($form['#fields'])) { - foreach ($form['#fields'] as $node_field) { - if ($node_field != $this->fieldDefinition->getName()) { - $caption_options[$node_field] = $node_field; + + $element = $this->addEntityReferenceSettings($element); + + // Add the other parent entity fields as options. + if (isset($form['#fields'])) { + foreach ($form['#fields'] as $parent_field) { + if ($parent_field != $this->fieldDefinition->getName()) { + $caption_options[$parent_field] = $parent_field; } } } @@ -98,6 +104,49 @@ class PhotoswipeFieldFormatter extends FormatterBase { } /** + * Adds extra settings related when dealing with an entity reference. + * + * @param array $element + * The settings form structure of this formatter. + * + * @return array + * The modified settings form structure of this formatter. + */ + private function addEntityReferenceSettings(array $element) { + if ($this->fieldDefinition->getType() !== 'entity_reference') { + return $element; + } + + $target_type = $this->fieldDefinition->getSetting('target_type'); + $target_bundles = $this->fieldDefinition->getSetting('handler_settings')['target_bundles']; + + /* @var $fields FieldDefinitionInterface[] */ + $fields = []; + foreach ($target_bundles as $bundle) { + $fields += \Drupal::service('entity_field.manager') + ->getFieldDefinitions($target_type, $bundle); + } + $fields = array_filter($fields, function (FieldDefinitionInterface $field) { + return $field->getType() === 'image' && $field->getName() !== 'thumbnail'; + }); + + $field_options = []; + foreach ($fields as $name => $field) { + $field_options[$name] = $field->getName(); + } + + $element['photoswipe_reference_image_field'] = [ + '#title' => $this->t('Image field of the referenced entity'), + '#type' => 'select', + '#default_value' => $this->getSetting('photoswipe_reference_image_field'), + '#options' => $field_options, + '#description' => $this->t('Field that contains the image to be used.'), + ]; + + return $element; + } + + /** * {@inheritdoc} */ public function settingsSummary() { @@ -135,6 +184,10 @@ class PhotoswipeFieldFormatter extends FormatterBase { $summary[] = $this->t('photoswipe image style: Original image'); } + if ($this->getSetting('photoswipe_reference_image_field')) { + $summary[] = $this->t('Referenced entity image field: @field', ['@field' => $this->getSetting('photoswipe_reference_image_field')]); + } + if ($this->getSetting('photoswipe_caption')) { $caption_options = [ 'alt' => $this->t('Image alt tag'), @@ -162,6 +215,8 @@ class PhotoswipeFieldFormatter extends FormatterBase { if (!empty($items)) { \Drupal::service('photoswipe.assets_manager')->attach($elements); + $elements['#prefix'] = '