From f76822a23077e03598f0a9481e735a369846c0a3 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Fri, 28 Feb 2020 20:15:17 -0500 Subject: [PATCH] Issue-54: Finally schemas are fixed and we have linked Thumbnails (#63) * Fix StrawberryMetadataTwigFormatter Settings form was using an 'item' #type wich implies a saved value in the settings. That is wrong since we needed that element only to display contextual info the user. Since i'm here also add a Link Element to the Image Formatter (around) and fix other (yes more!) wrong schema elements. * Its '#uri 'not '#url' * String-i-fy * Give image_formatter a try * That was a bad idea! We are generating images via IIIF, we really don't want to use Drupal's entity view of files here. * Tiny typo * maybe nested render array works? * Add alt? I thought Drupal was adding this.. well no! * No need for a type --- .../schema/format_strawberryfield.schema.yml | 6 ++- .../StrawberryImageFormatter.php | 46 ++++++++++++------- .../StrawberryMetadataTwigFormatter.php | 4 +- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/config/schema/format_strawberryfield.schema.yml b/config/schema/format_strawberryfield.schema.yml index 601f9a10..29a85e64 100644 --- a/config/schema/format_strawberryfield.schema.yml +++ b/config/schema/format_strawberryfield.schema.yml @@ -55,13 +55,15 @@ field.formatter.settings.strawberry_image_formatter: type: string label: 'Whether to use global IIIF settings or not.' number_images: - type: integer - images_type: + type: string + image_type: type: string quality: type: string rotation: type: string + image_link: + type: boolean field.formatter.settings.strawberry_media_formatter: type: mapping label: 'Specific Config for strawberry_media_formatter' diff --git a/src/Plugin/Field/FieldFormatter/StrawberryImageFormatter.php b/src/Plugin/Field/FieldFormatter/StrawberryImageFormatter.php index 4cd1e9ca..4c12b684 100644 --- a/src/Plugin/Field/FieldFormatter/StrawberryImageFormatter.php +++ b/src/Plugin/Field/FieldFormatter/StrawberryImageFormatter.php @@ -20,7 +20,7 @@ * * @FieldFormatter( * id = "strawberry_image_formatter", - * label = @Translation("Strawberry Field Image Formatter for IIIF media"), + * label = @Translation("Strawberry Field Simple Image Formatter using IIIF"), * class = "\Drupal\format_strawberryfield\Plugin\Field\FieldFormatter\StrawberryImageFormatter", * field_types = { * "strawberryfield_field" @@ -45,6 +45,7 @@ public static function defaultSettings() { 'number_images' => 1, 'quality' => 'default', 'rotation' => '0', + 'image_link' => TRUE, ]; } @@ -67,6 +68,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#maxlength' => 2, '#min' => 0, ], + 'image_link' => [ + '#type' => 'checkbox', + '#title' => t('Link this image to the Full Node'), + '#default_value' => $this->getSetting('image_link'), + ], 'max_width' => [ '#type' => 'number', '#title' => $this->t('Maximum width'), @@ -113,6 +119,9 @@ public function settingsSummary() { '%max_height' => $this->getSetting('max_height') . ' pixels', ] ); + $summary[] = $this->t('Link to Node? %value', [ + '%value' => boolval($this->getSetting('image_link')) === TRUE ? "Yes." : "No", + ]); return $summary; } @@ -238,7 +247,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } $iiifserverthumb = "{$this->getIiifUrls()['public']}/{$iiifidentifier}"."/full/{$max_width},/0/default.jpg"; - $elements[$delta]['media_thumb' . $i] = [ + $image_render_array = [ '#theme' => 'image', '#attributes' => [ 'class' => ['field-iiif', 'image-iiif'], @@ -253,6 +262,22 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#width' => $max_width, '#height' => $max_height, ]; + + // With Link + if (boolval($this->getSetting('image_link')) === TRUE && !$items->getEntity()->isNew()) { + $elements[$delta]['media_thumb' . $i] = [ + '#type' => 'link', + '#title' => $image_render_array, + '#url' => $items->getEntity()->toUrl(), + '#attributes' => [ + 'alt' => $items->getEntity()->label() + ] + ]; + } + else { + $elements[$delta]['media_thumb' . $i] = $image_render_array; + } + if (isset($item->_attributes)) { $elements[$delta] += ['#attributes' => []]; $elements[$delta]['#attributes'] += $item->_attributes; @@ -260,33 +285,20 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // formatter output and should not be rendered in the field template. unset($item->_attributes); } - // @TODO deal with a lot of Media single strawberryfield - // Idea would be to allow a setting that says, A) all same viewer(aggregate) - // B) individual viewers for each? - // C) only first one? - // We will assign a group based on the UUID of the node containing this - // to idenfity all the divs that we will create. And only first one will be the container in case of many? - // so a jquery selector that uses that group as filter for a search. - // Drupal JS settings get accumulated. So in a single search results site we will have for each - // Formatter one passed. Reason we use 'innode' array using our $uniqueid - // @TODO probably better to use uuid() or the node id() instead of $uniqueid - $elements[$delta]['media'.$i]['#attributes']['data-iiif-infojson'] = $iiifpublicinfojson; - $elements[$delta]['media'.$i]['#attached']['drupalSettings']['format_strawberryfield']['openseadragon']['innode'][$uniqueid] = $nodeuuid; } - } else { // @TODO Deal with no access here // Should we put a thumb? Just hide? // @TODO we can bring a plugin here and there that deals with - $elements[$delta]['media'.$i] = [ + $elements[$delta]['media_thumb'.$i] = [ '#markup' => '', '#prefix' => '', '#suffix' => '', ]; } } elseif (isset($mediaitem['url'])) { - $elements[$delta]['media'.$i] = [ + $elements[$delta]['media_thumb'.$i] = [ '#markup' => 'Non managed '.$mediaitem['url'], '#prefix' => '
',
                 '#suffix' => '
', diff --git a/src/Plugin/Field/FieldFormatter/StrawberryMetadataTwigFormatter.php b/src/Plugin/Field/FieldFormatter/StrawberryMetadataTwigFormatter.php index 62f0ecbe..93fad9eb 100644 --- a/src/Plugin/Field/FieldFormatter/StrawberryMetadataTwigFormatter.php +++ b/src/Plugin/Field/FieldFormatter/StrawberryMetadataTwigFormatter.php @@ -158,7 +158,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) { return [ 'customtext' => [ - '#type' => 'item', '#markup' => '

Use this form to select the template for your metadata.

Several templates such as MODS 3.6 and a simple Object Description ship with Archipelago. To design your own template for any metadata standard you like, or see the full list of existing templates, visit /metadatadisplay/list.

', ], 'metadatadisplayentity_id' => [ @@ -243,7 +242,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) { continue; } - $jsondata = json_decode($item->value, true); + $jsondata = json_decode($item->value, TRUE); + // Probably good idea to strip our own keys here // @TODO remove private access to keys