From c3265eca43d93dc6e3db6b51508b52671662057c Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Fri, 31 May 2024 14:56:37 -0300 Subject: [PATCH 1/3] Option to get alt text from Original File. --- .../IslandoraImageFormatter.php | 119 ++++++++++++++++-- 1 file changed, 106 insertions(+), 13 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php index deb6c7690..b96c1898f 100644 --- a/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php +++ b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php @@ -6,9 +6,11 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\File\FileUrlGeneratorInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter; use Drupal\islandora\IslandoraUtils; +use Drupal\islandora\MediaSource\MediaSourceService; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -34,6 +36,13 @@ class IslandoraImageFormatter extends ImageFormatter { */ protected $utils; + /** + * Islandora media source service. + * + * @var \Drupal\islandora\MediaSource\MediaSourceService + */ + protected $mediaSourceService; + /** * Constructs an IslandoraImageFormatter object. * @@ -59,6 +68,8 @@ class IslandoraImageFormatter extends ImageFormatter { * Islandora utils. * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator * The File URL Generator. + * @param \Drupal\islandora\MediaSource\MediaSourceService $media_source_service + * Utils to get the source file from media. */ public function __construct( $plugin_id, @@ -71,7 +82,8 @@ public function __construct( AccountInterface $current_user, EntityStorageInterface $image_style_storage, IslandoraUtils $utils, - FileUrlGeneratorInterface $file_url_generator + FileUrlGeneratorInterface $file_url_generator, + MediaSourceService $media_source_service ) { parent::__construct( $plugin_id, @@ -86,6 +98,7 @@ public function __construct( $file_url_generator ); $this->utils = $utils; + $this->mediaSourceService = $media_source_service; } /** @@ -103,10 +116,44 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('current_user'), $container->get('entity_type.manager')->getStorage('image_style'), $container->get('islandora.utils'), - $container->get('file_url_generator') + $container->get('file_url_generator'), + $container->get('islandora.media_source_service') ); } + /** + * {@inheritdoc} + */ + + /** + * {@inheritdoc} + */ + public static function defaultSettings() { + return [ + 'image_alt_text' => 'local', + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $element = parent::settingsForm($form, $form_state); + $alt_text_options = [ + 'local' => $this->t('Local'), + 'original_file_fallback' => $this->t('Local, with fallback to Original File'), + 'original_file' => $this->t('Original File'), + ]; + $element['image_alt_text'] = [ + '#title' => $this->t('Alt text source'), + '#type' => 'select', + '#default_value' => $this->getSetting('image_alt_text'), + '#empty_option' => $this->t('None'), + '#options' => $alt_text_options, + ]; + return $element; + } + /** * {@inheritdoc} */ @@ -114,28 +161,74 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = parent::viewElements($items, $langcode); $image_link_setting = $this->getSetting('image_link'); - // Check if the formatter involves a link. - if ($image_link_setting != 'content') { + $alt_text_setting = $this->getsetting('image_alt_text'); + // Check if we can leave the image as-is: + if ($image_link_setting != 'content' && $alt_text_setting == 'local') { return $elements; } - $entity = $items->getEntity(); if ($entity->isNew() || $entity->getEntityTypeId() != 'media') { return $elements; } - $node = $this->utils->getParentNode($entity); - - if ($node === NULL) { - return $elements; + if ($alt_text_setting == 'none') { + foreach ($elements as $element) { + $element['#item']->set('alt', ''); + } } - $url = $node->toUrl(); + if ($image_link_setting == 'content' || $alt_text_setting == 'original_file' || $alt_text_setting == 'original_file_fallback') { + $node = $this->utils->getParentNode($entity); + if ($node === NULL) { + return $elements; + } - foreach ($elements as &$element) { - $element['#url'] = $url; - } + if ($image_link_setting == 'content') { + // Set image link. + $url = $node->toUrl(); + foreach ($elements as &$element) { + $element['#url'] = $url; + } + } + + if ($alt_text_setting == 'original_file' || $alt_text_setting == 'original_file_fallback') { + do { + $original_file_term = $this->utils->getTermForUri("http://pcdm.org/use#OriginalFile"); + if ($original_file_term === NULL) { + break; + } + + $original_file_media = $this->utils->getMediaWithTerm($node, $original_file_term); + + if ($original_file_media === NULL) { + break; + } + $source_field_name = $this->mediaSourceService->getSourceFieldName($original_file_media->bundle()); + if (!$original_file_media->hasField($source_field_name)) { + break; + } + $original_file_files = $original_file_media->get($source_field_name); + $alt_text = ''; + // Get Nth alt text from the Nth file field. + $i = 0; + foreach ($original_file_files as $file) { + if (!$file->hasProperty('alt')) { + break; + } + $alt_text = $file->get('alt')->getValue(); + if (!isset($elements[$i])) { + break; + } + $element = $elements[$i]; + if ($alt_text_setting == 'original_file' || $element['#item']->get('alt')->getValue() == '') { + $elements[$i]['#item']->set('alt', $alt_text); + } + $i++; + } + } while (FALSE); + } + } return $elements; } From 4547b30707d2b9fb97635579d0a37c8bde836b20 Mon Sep 17 00:00:00 2001 From: Rosie Le Faive Date: Tue, 4 Jun 2024 08:16:26 -0300 Subject: [PATCH 2/3] Add schema. --- config/schema/islandora.schema.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index 49de998b3..282f7b3c5 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -173,6 +173,10 @@ condition.plugin.node_had_namespace: field.formatter.settings.islandora_image: type: field.formatter.settings.image label: 'Islandora image field display format settings' + mapping: + image_alt_text: + type: string + label: "Alt text source" condition.plugin.islandora_entity_bundle: type: condition.plugin From 6258f9fe3d3fa3654687e96964b459be42c6cb52 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Mon, 10 Jun 2024 20:45:26 -0300 Subject: [PATCH 3/3] Logic re-work and fixing a undefined method. --- .../IslandoraImageFormatter.php | 70 ++++++++----------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php index b96c1898f..ffe6d47a2 100644 --- a/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php +++ b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php @@ -121,10 +121,6 @@ public static function create(ContainerInterface $container, array $configuratio ); } - /** - * {@inheritdoc} - */ - /** * {@inheritdoc} */ @@ -162,71 +158,65 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $image_link_setting = $this->getSetting('image_link'); $alt_text_setting = $this->getsetting('image_alt_text'); + // Check if we can leave the image as-is: - if ($image_link_setting != 'content' && $alt_text_setting == 'local') { + if ($image_link_setting !== 'content' && $alt_text_setting === 'local') { return $elements; } $entity = $items->getEntity(); - if ($entity->isNew() || $entity->getEntityTypeId() != 'media') { + if ($entity->isNew() || $entity->getEntityTypeId() !== 'media') { return $elements; } - if ($alt_text_setting == 'none') { + if ($alt_text_setting === 'none') { foreach ($elements as $element) { $element['#item']->set('alt', ''); } } - if ($image_link_setting == 'content' || $alt_text_setting == 'original_file' || $alt_text_setting == 'original_file_fallback') { + if ($image_link_setting === 'content' || $alt_text_setting === 'original_file' || $alt_text_setting === 'original_file_fallback') { $node = $this->utils->getParentNode($entity); if ($node === NULL) { return $elements; } - if ($image_link_setting == 'content') { + if ($image_link_setting === 'content') { // Set image link. $url = $node->toUrl(); foreach ($elements as &$element) { $element['#url'] = $url; } + unset($element); } - if ($alt_text_setting == 'original_file' || $alt_text_setting == 'original_file_fallback') { - do { - $original_file_term = $this->utils->getTermForUri("http://pcdm.org/use#OriginalFile"); - - if ($original_file_term === NULL) { - break; - } + if ($alt_text_setting === 'original_file' || $alt_text_setting === 'original_file_fallback') { + $original_file_term = $this->utils->getTermForUri("http://pcdm.org/use#OriginalFile"); + if ($original_file_term !== NULL) { $original_file_media = $this->utils->getMediaWithTerm($node, $original_file_term); - if ($original_file_media === NULL) { - break; - } - $source_field_name = $this->mediaSourceService->getSourceFieldName($original_file_media->bundle()); - if (!$original_file_media->hasField($source_field_name)) { - break; - } - $original_file_files = $original_file_media->get($source_field_name); - $alt_text = ''; - // Get Nth alt text from the Nth file field. - $i = 0; - foreach ($original_file_files as $file) { - if (!$file->hasProperty('alt')) { - break; + if ($original_file_media !== NULL) { + $source_field_name = $this->mediaSourceService->getSourceFieldName($original_file_media->bundle()); + if ($original_file_media->hasField($source_field_name)) { + $original_file_files = $original_file_media->get($source_field_name); + // XXX: Support the multifile media use case where there could + // be multiple files in the source field. + $i = 0; + foreach ($original_file_files as $file) { + if (isset($file->alt)) { + $alt_text = $file->get('alt')->getValue(); + if (isset($elements[$i])) { + $element = $elements[$i]; + if ($alt_text_setting === 'original_file' || $element['#item']->get('alt')->getValue() === '') { + $elements[$i]['#item']->set('alt', $alt_text); + } + $i++; + } + } + } } - $alt_text = $file->get('alt')->getValue(); - if (!isset($elements[$i])) { - break; - } - $element = $elements[$i]; - if ($alt_text_setting == 'original_file' || $element['#item']->get('alt')->getValue() == '') { - $elements[$i]['#item']->set('alt', $alt_text); - } - $i++; } - } while (FALSE); + } } } return $elements;