summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-11-08 10:12:02 +0000
committerAlex Pott2016-11-08 10:12:02 +0000
commit10fead729df6c405a01422e5fb5f9837716e9521 (patch)
tree39ed61c78a4d70c6c8d2958073184ff714ff0d84
parent982a790502bf1f85e22b6481c17d645982bc7425 (diff)
Issue #2517030 by Jaesin, dawehner, tedbow, claudiu.cristea, mglaman, amateescu, borisson_, joelpittet, mondrake, Wim Leers, tstoeckler, alexpott, joshi.rohit100, tim.plunkett, nketchum, mattferderer: Add a URL formatter for the image field
-rw-r--r--core/modules/image/config/schema/image.schema.yml8
-rw-r--r--core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php82
-rw-r--r--core/modules/image/src/Tests/ImageFieldDisplayTest.php13
3 files changed, 103 insertions, 0 deletions
diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml
index 323220b..0006ad7 100644
--- a/core/modules/image/config/schema/image.schema.yml
+++ b/core/modules/image/config/schema/image.schema.yml
@@ -139,6 +139,14 @@ field.formatter.settings.image:
type: string
label: 'Image style'
+field.formatter.settings.image_url:
+ type: mapping
+ label: 'Image URL formatter settings'
+ mapping:
+ image_style:
+ type: string
+ label: 'Image style'
+
field.widget.settings.image_image:
type: mapping
label: 'Image field display format settings'
diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php
new file mode 100644
index 0000000..1dea1f8
--- /dev/null
+++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php
@@ -0,0 +1,82 @@
+<?php
+
+namespace Drupal\image\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Field\FieldItemListInterface;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Plugin implementation of the 'image_url' formatter.
+ *
+ * @FieldFormatter(
+ * id = "image_url",
+ * label = @Translation("URL to image"),
+ * field_types = {
+ * "image"
+ * }
+ * )
+ */
+class ImageUrlFormatter extends ImageFormatter {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ return [
+ 'image_style' => '',
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $element = parent::settingsForm($form, $form_state);
+
+ unset($element['image_link']);;
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsSummary() {
+ $summary = parent::settingsSummary();
+ return [$summary[0]];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function viewElements(FieldItemListInterface $items, $langcode) {
+ $elements = [];
+
+ /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
+ if (empty($images = $this->getEntitiesToView($items, $langcode))) {
+ // Early opt-out if the field is empty.
+ return $elements;
+ }
+
+ /** @var \Drupal\image\ImageStyleInterface $image_style */
+ $image_style = $this->imageStyleStorage->load($this->getSetting('image_style'));
+ /** @var \Drupal\file\FileInterface[] $images */
+ foreach ($images as $delta => $image) {
+ $image_uri = $image->getFileUri();
+ $url = $image_style ? $image_style->buildUrl($image_uri) : file_create_url($image_uri);
+ $url = file_url_transform_relative($url);
+
+ // Add cacheability metadata from the image and image style.
+ $cacheability = CacheableMetadata::createFromObject($image);
+ if ($image_style) {
+ $cacheability->addCacheableDependency(CacheableMetadata::createFromObject($image_style));
+ }
+
+ $elements[$delta] = ['#markup' => $url];
+ $cacheability->applyTo($elements[$delta]);
+ }
+ return $elements;
+ }
+
+}
diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
index 666a791..f3ce858 100644
--- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php
+++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
@@ -199,6 +199,19 @@ class ImageFieldDisplayTest extends ImageFieldTestBase {
$this->drupalGet(ImageStyle::load('thumbnail')->buildUrl($image_uri));
$this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
}
+
+ // Test the image URL formatter without an image style.
+ $display_options = [
+ 'type' => 'image_url',
+ 'settings' => ['image_style' => ''],
+ ];
+ $expected_url = file_url_transform_relative(file_create_url($image_uri));
+ $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
+
+ // Test the image URL formatter with an image style.
+ $display_options['settings']['image_style'] = 'thumbnail';
+ $expected_url = file_url_transform_relative(ImageStyle::load('thumbnail')->buildUrl($image_uri));
+ $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
}
/**