diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml
index 323220b..de28bb2 100644
--- a/core/modules/image/config/schema/image.schema.yml
+++ b/core/modules/image/config/schema/image.schema.yml
@@ -139,6 +139,17 @@ field.formatter.settings.image:
type: string
label: 'Image style'
+field.formatter.settings.image_url:
+ type: mapping
+ label: 'Image URL format settings'
+ mapping:
+ image_link:
+ type: string
+ label: 'Link image to'
+ 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/ImageFormatter.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
index aa24a95..29dec18 100644
--- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
+++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
@@ -7,17 +7,8 @@
namespace Drupal\image\Plugin\Field\FieldFormatter;
-use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
-use Drupal\Core\Field\FieldDefinitionInterface;
-use Drupal\Core\Link;
-use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
-use Drupal\Core\Routing\UrlGeneratorInterface;
-use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
-use Drupal\Core\Utility\LinkGeneratorInterface;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
/**
@@ -31,146 +22,14 @@
* }
* )
*/
-class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPluginInterface {
-
- /**
- * The current user.
- *
- * @var \Drupal\Core\Session\AccountInterface
- */
- protected $currentUser;
-
- /**
- * The image style entity storage.
- *
- * @var \Drupal\Core\Entity\EntityStorageInterface
- */
- protected $imageStyleStorage;
-
- /**
- * Constructs an ImageFormatter object.
- *
- * @param string $plugin_id
- * The plugin_id for the formatter.
- * @param mixed $plugin_definition
- * The plugin implementation definition.
- * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
- * The definition of the field to which the formatter is associated.
- * @param array $settings
- * The formatter settings.
- * @param string $label
- * The formatter label display setting.
- * @param string $view_mode
- * The view mode.
- * @param array $third_party_settings
- * Any third party settings settings.
- * @param \Drupal\Core\Session\AccountInterface $current_user
- * The current user.
- */
- public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage) {
- parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
- $this->currentUser = $current_user;
- $this->imageStyleStorage = $image_style_storage;
- }
-
- /**
- * {@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')
- );
- }
-
- /**
- * {@inheritdoc}
- */
- public static function defaultSettings() {
- return array(
- 'image_style' => '',
- 'image_link' => '',
- ) + parent::defaultSettings();
- }
-
- /**
- * {@inheritdoc}
- */
- public function settingsForm(array $form, FormStateInterface $form_state) {
- $image_styles = image_style_options(FALSE);
- $description_link = Link::fromTextAndUrl(
- $this->t('Configure Image Styles'),
- Url::fromRoute('entity.image_style.collection')
- );
- $element['image_style'] = [
- '#title' => t('Image style'),
- '#type' => 'select',
- '#default_value' => $this->getSetting('image_style'),
- '#empty_option' => t('None (original image)'),
- '#options' => $image_styles,
- '#description' => $description_link->toRenderable() + [
- '#access' => $this->currentUser->hasPermission('administer image styles')
- ],
- ];
- $link_types = array(
- 'content' => t('Content'),
- 'file' => t('File'),
- );
- $element['image_link'] = array(
- '#title' => t('Link image to'),
- '#type' => 'select',
- '#default_value' => $this->getSetting('image_link'),
- '#empty_option' => t('Nothing'),
- '#options' => $link_types,
- );
-
- return $element;
- }
-
- /**
- * {@inheritdoc}
- */
- public function settingsSummary() {
- $summary = array();
-
- $image_styles = image_style_options(FALSE);
- // Unset possible 'No defined styles' option.
- unset($image_styles['']);
- // Styles could be lost because of enabled/disabled modules that defines
- // their styles in code.
- $image_style_setting = $this->getSetting('image_style');
- if (isset($image_styles[$image_style_setting])) {
- $summary[] = t('Image style: @style', array('@style' => $image_styles[$image_style_setting]));
- }
- else {
- $summary[] = t('Original image');
- }
-
- $link_types = array(
- 'content' => t('Linked to content'),
- 'file' => t('Linked to file'),
- );
- // Display this setting only if image is linked.
- $image_link_setting = $this->getSetting('image_link');
- if (isset($link_types[$image_link_setting])) {
- $summary[] = $link_types[$image_link_setting];
- }
-
- return $summary;
- }
+class ImageFormatter extends ImageFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
+ /** @var \Drupal\file\Entity\File[] $files */
$files = $this->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
index 6f79497..72c7721 100644
--- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
+++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
@@ -7,15 +7,156 @@
namespace Drupal\image\Plugin\Field\FieldFormatter;
+use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
-use Drupal\Core\Language\LanguageInterface;
+use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Link;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\Core\Session\AccountInterface;
+use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for image file formatters.
*/
-abstract class ImageFormatterBase extends FileFormatterBase {
+abstract class ImageFormatterBase extends FileFormatterBase implements ContainerFactoryPluginInterface {
+
+ /**
+ * The current user.
+ *
+ * @var \Drupal\Core\Session\AccountInterface
+ */
+ protected $currentUser;
+
+ /**
+ * The image style entity storage.
+ *
+ * @var \Drupal\Core\Entity\EntityStorageInterface
+ */
+ protected $imageStyleStorage;
+
+ /**
+ * Constructs an ImageFormatter object.
+ *
+ * @param string $plugin_id
+ * The plugin_id for the formatter.
+ * @param mixed $plugin_definition
+ * The plugin implementation definition.
+ * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
+ * The definition of the field to which the formatter is associated.
+ * @param array $settings
+ * The formatter settings.
+ * @param string $label
+ * The formatter label display setting.
+ * @param string $view_mode
+ * The view mode.
+ * @param array $third_party_settings
+ * Any third party settings settings.
+ * @param \Drupal\Core\Session\AccountInterface $current_user
+ * The current user.
+ */
+ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage) {
+ parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
+ $this->currentUser = $current_user;
+ $this->imageStyleStorage = $image_style_storage;
+ }
+
+ /**
+ * {@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')
+ );
+ }
+
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ return array(
+ 'image_style' => '',
+ 'image_link' => '',
+ ) + parent::defaultSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ $image_styles = image_style_options(FALSE);
+ $description_link = Link::fromTextAndUrl(
+ $this->t('Configure Image Styles'),
+ Url::fromRoute('entity.image_style.collection')
+ );
+ $element['image_style'] = [
+ '#title' => t('Image style'),
+ '#type' => 'select',
+ '#default_value' => $this->getSetting('image_style'),
+ '#empty_option' => t('None (original image)'),
+ '#options' => $image_styles,
+ '#description' => $description_link->toRenderable() + [
+ '#access' => $this->currentUser->hasPermission('administer image styles')
+ ],
+ ];
+ $link_types = array(
+ 'content' => t('Content'),
+ 'file' => t('File'),
+ );
+ $element['image_link'] = array(
+ '#title' => t('Link image to'),
+ '#type' => 'select',
+ '#default_value' => $this->getSetting('image_link'),
+ '#empty_option' => t('Nothing'),
+ '#options' => $link_types,
+ );
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsSummary() {
+ $summary = array();
+
+ $image_styles = image_style_options(FALSE);
+ // Unset possible 'No defined styles' option.
+ unset($image_styles['']);
+ // Styles could be lost because of enabled/disabled modules that defines
+ // their styles in code.
+ $image_style_setting = $this->getSetting('image_style');
+ if (isset($image_styles[$image_style_setting])) {
+ $summary[] = t('Image style: @style', array('@style' => $image_styles[$image_style_setting]));
+ }
+ else {
+ $summary[] = t('Original image');
+ }
+
+ $link_types = array(
+ 'content' => t('Linked to content'),
+ 'file' => t('Linked to file'),
+ );
+ // Display this setting only if image is linked.
+ $image_link_setting = $this->getSetting('image_link');
+ if (isset($link_types[$image_link_setting])) {
+ $summary[] = $link_types[$image_link_setting];
+ }
+
+ return $summary;
+ }
/**
* {@inheritdoc}
@@ -51,4 +192,19 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
return parent::getEntitiesToView($items, $langcode);
}
+ /**
+ * {@inheritdoc}
+ */
+ public function calculateDependencies() {
+ // Make sure to include 3rd party dependencies.
+ $dependencies = parent::calculateDependencies();
+
+ // Check for image style.
+ if (!empty($image_style_setting) && $image_style = $this->imageStyleStorage->load($image_style_setting)) {
+ // Add the image style dependencies as well.
+ $dependencies['config'][] = $image_style->getConfigDependencyName();
+ }
+
+ return $dependencies;
+ }
}
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..06ab823
--- /dev/null
+++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageUrlFormatter.php
@@ -0,0 +1,82 @@
+getEntitiesToView($items, $langcode);
+
+ // Early opt-out if the field is empty.
+ if (empty($images)) {
+ return $elements;
+ }
+
+ $image_link_setting = $this->getSetting('image_link');
+ // Url to be linked to.
+ $link_url = FALSE;
+ // Check if the formatter involves a link.
+ if ($image_link_setting == 'content') {
+ $entity = $items->getEntity();
+ if (!$entity->isNew()) {
+ $link_url = $entity->urlInfo();
+ }
+ }
+
+ /** @var \Drupal\image\Entity\ImageStyle|false $image_style */
+ $image_style = ($image_style_setting = $this->getSetting('image_style')) && !empty($image_style_setting) ? $this->imageStyleStorage->load($image_style_setting) : NULL;
+
+ foreach ($images as $delta => $image) {
+ /** @var \Drupal\file\Entity\File $image */
+ $image_uri = $image->getFileUri();
+ $url = $image_style
+ ? $image_style->buildUrl($image_uri)
+ : file_create_url($image_uri);
+
+ // Set the link url if settings require such.
+ $link_url = ($image_link_setting == 'file') ? Url::fromUri($url) : $link_url;
+
+ // Add cacheable metadata from the image and image style.
+ $cacheable_metadata = CacheableMetadata::createFromObject($image);
+ if ($image_style) {
+ $cacheable_metadata = $cacheable_metadata->merge(CacheableMetadata::createFromObject($image_style));
+ }
+
+ // Add a link if we have a valid link url.
+ if ($link_url instanceof Url) {
+ $elements[$delta] = Link::fromTextAndUrl($url, $link_url)->toRenderable();
+ }
+ else {
+ $elements[$delta] = ['#markup' => $url];
+ }
+ $cacheable_metadata->applyTo($elements[$delta]);
+ }
+
+ return $elements;
+ }
+}
diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
index c4cf485..b5caad8 100644
--- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php
+++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php
@@ -93,6 +93,7 @@ function _testImageFieldFormatters($scheme) {
// Save node.
$nid = $this->uploadNodeImage($test_image, $field_name, 'article', $alt);
$node_storage->resetCache(array($nid));
+ /** @var \Drupal\node\Entity\Node $node */
$node = $node_storage->load($nid);
// Test that the default formatter is being used.
@@ -202,6 +203,51 @@ function _testImageFieldFormatters($scheme) {
$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' => [],
+ ];
+
+ $expected_url = 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 = [
+ 'type' => 'image_url',
+ 'settings' => [
+ 'image_style' => 'thumbnail',
+ ],
+ ];
+
+ $expected_url = ImageStyle::load('thumbnail')->buildUrl($image_uri);
+ $this->assertEqual($expected_url, $node->{$field_name}->view($display_options)[0]['#markup']);
+
+ // Test the image URL formatter with an image style that links to content.
+ $display_options = [
+ 'type' => 'image_url',
+ 'settings' => [
+ 'image_style' => 'thumbnail',
+ 'image_link' => 'content',
+ ],
+ ];
+
+ $expected_output = '' . ImageStyle::load('thumbnail')->buildUrl($image_uri) . '';
+ $this->assertEqual($expected_output, (string) $renderer->renderRoot($node->{$field_name}->view($display_options)[0]));
+
+ // Test the image URL formatter with an image style that links to file.
+ $display_options = [
+ 'type' => 'image_url',
+ 'settings' => [
+ 'image_style' => 'thumbnail',
+ 'image_link' => 'file',
+ ],
+ ];
+
+ $expected_url = ImageStyle::load('thumbnail')->buildUrl($image_uri);
+ $expected_output = '' . $expected_url . '';
+ $this->assertEqual($expected_output, (string) $renderer->renderRoot($node->{$field_name}->view($display_options)[0]));
}
/**
diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php
index d1290cb..08648e7 100644
--- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php
+++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php
@@ -85,7 +85,7 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
* The current user.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user) {
- parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
+ parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage);
$this->responsiveImageStyleStorage = $responsive_image_style_storage;
$this->imageStyleStorage = $image_style_storage;
@@ -116,10 +116,14 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public static function defaultSettings() {
- return array(
+ $default_settings = parent::defaultSettings();
+
+ // Use responsive_image_style instead of image_style;
+ unset($default_settings['image_style']);
+
+ return [
'responsive_image_style' => '',
- 'image_link' => '',
- ) + parent::defaultSettings();
+ ] + $default_settings ;
}
/**