From 43b971cdf627403d7e3436c79161c06ef8eb26e9 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Mon, 6 May 2019 12:33:02 -0300 Subject: [PATCH] Slideshow image formatter (#127) * Custom image formatter with URL to node instead of media * Tests --- config/schema/islandora.schema.yml | 11 ++ .../IslandoraImageFormatter.php | 136 ++++++++++++++++++ .../IslandoraImageFormatterTest.php | 93 ++++++++++++ 3 files changed, 240 insertions(+) create mode 100644 src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php create mode 100644 tests/src/Functional/IslandoraImageFormatterTest.php diff --git a/config/schema/islandora.schema.yml b/config/schema/islandora.schema.yml index a2ca48d3..1df5b7e8 100644 --- a/config/schema/islandora.schema.yml +++ b/config/schema/islandora.schema.yml @@ -120,3 +120,14 @@ condition.plugin.content_entity_type: type: sequence sequence: type: string + +field.formatter.settings.islandora_image: + type: mapping + label: 'Image field display format settings' + mapping: + image_link: + type: string + label: 'Link image to' + image_style: + type: string + label: 'Image style' diff --git a/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php new file mode 100644 index 00000000..321dd05d --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/IslandoraImageFormatter.php @@ -0,0 +1,136 @@ +utils = $utils; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('current_user'), + $container->get('entity.manager')->getStorage('image_style'), + $container->get('islandora.utils') + ); + } + + /** + * {@inheritdoc} + */ + 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') { + return $elements; + } + + $entity = $items->getEntity(); + if ($entity->isNew() || $entity->getEntityTypeId() != 'media') { + return $elements; + } + + $node = $this->utils->getParentNode($entity); + + if ($node === NULL) { + return $elements; + } + + $url = $node->urlInfo(); + + foreach ($elements as &$element) { + $element['#url'] = $url; + } + + return $elements; + } + +} diff --git a/tests/src/Functional/IslandoraImageFormatterTest.php b/tests/src/Functional/IslandoraImageFormatterTest.php new file mode 100644 index 00000000..aa85162c --- /dev/null +++ b/tests/src/Functional/IslandoraImageFormatterTest.php @@ -0,0 +1,93 @@ +drupalCreateUser([ + 'bypass node access', + 'view media', + 'create media', + ]); + $this->drupalLogin($account); + + // Create an image media type. + $testImageMediaType = $this->createMediaType('image', ['id' => 'test_image_media_type']); + $testImageMediaType->save(); + $this->createEntityReferenceField('media', $testImageMediaType->id(), 'field_media_of', 'Media Of', 'node', 'default', [], 2); + + // Set the display mode to use the islandora_image formatter. + // Also, only show the image on display to remove clutter. + $display_options = [ + 'type' => 'islandora_image', + 'settings' => ['image_style' => NULL, 'image_link' => 'content'], + ]; + $display = entity_get_display('media', $testImageMediaType->id(), 'default'); + $display->setComponent('field_media_image', $display_options) + ->removeComponent('created') + ->removeComponent('uid') + ->removeComponent('thumbnail') + ->save(); + + // Make a node. + $node = $this->container->get('entity_type.manager')->getStorage('node')->create([ + 'type' => $this->testType->id(), + 'title' => 'Test Node', + ]); + $node->save(); + + // Make a image for the Media. + $file = $this->container->get('entity_type.manager')->getStorage('file')->create([ + 'uid' => $account->id(), + 'uri' => "public://test.jpeg", + 'filename' => "test.jpeg", + 'filemime' => "image/jpeg", + 'status' => FILE_STATUS_PERMANENT, + ]); + $file->save(); + + // Make the media, and associate it with the image and node. + $media = $this->container->get('entity_type.manager')->getStorage('media')->create([ + 'bundle' => $testImageMediaType->id(), + 'name' => 'Media', + 'field_media_image' => + [ + 'target_id' => $file->id(), + 'alt' => 'Some Alt', + 'title' => 'Some Title', + ], + 'field_media_of' => ['target_id' => $node->id()], + ]); + $media->save(); + + // View the media. + $this->drupalGet("media/1"); + + // Assert that the image is rendered into html as a link pointing + // to the Node, not the Media (that's what the islandora_image + // formatter does). + $elements = $this->xpath( + '//a[@href=:path]/img[@src=:url and @alt=:alt and @title=:title]', + [ + ':path' => $node->url(), + ':url' => file_url_transform_relative(file_create_url($file->getFileUri())), + ':alt' => 'Some Alt', + ':title' => 'Some Title', + ] + ); + $this->assertEqual(count($elements), 1, 'Image linked to content formatter displaying points to Node and not Media.'); + } + +}