21c21
< index 5f15073..fcdcb87 100644
---
> index 3069721..f81a6d8 100644
24c24
< @@ -7,15 +7,8 @@
---
> @@ -7,17 +7,8 @@
30a31
> -use Drupal\Core\Link;
37c38
< use Drupal\Core\Form\FormStateInterface;
---
> -use Drupal\Core\Form\FormStateInterface;
40c41,42
< @@ -30,113 +23,34 @@
---
> /**
> @@ -31,146 +22,15 @@
45c47,48
< -
---
> +class ImageFormatter extends ImageFormatterBase {
>
54,60d56
< - * The link generator.
< - *
< - * @var \Drupal\Core\Utility\LinkGeneratorInterface
< - */
< - protected $linkGenerator;
< -
< - /**
86,87d81
< - * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
< - * The link generator service.
89c83
< - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, EntityStorageInterface $image_style_storage) {
---
> - 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) {
92d85
< - $this->linkGenerator = $link_generator;
109d101
< - $container->get('link_generator'),
113,118c105,109
< +class ImageFormatter extends ImageFormatterBase {
<
< /**
< * {@inheritdoc}
< */
< public static function defaultSettings() {
---
> -
> - /**
> - * {@inheritdoc}
> - */
> - public static function defaultSettings() {
121,122c112
< + return [
< 'image_link' => '',
---
> - 'image_link' => '',
124,130c114,119
< + ] + parent::defaultSettings();
< }
<
< /**
< * {@inheritdoc}
< */
< public function settingsForm(array $form, FormStateInterface $form_state) {
---
> - }
> -
> - /**
> - * {@inheritdoc}
> - */
> - public function settingsForm(array $form, FormStateInterface $form_state) {
132c121,125
< - $element['image_style'] = array(
---
> - $description_link = Link::fromTextAndUrl(
> - $this->t('Configure Image Styles'),
> - Url::fromRoute('entity.image_style.collection')
> - );
> - $element['image_style'] = [
138,142c131,134
< - '#description' => array(
< - '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')),
< - '#access' => $this->currentUser->hasPermission('administer image styles'),
< - ),
< - );
---
> - '#description' => $description_link->toRenderable() + [
> - '#access' => $this->currentUser->hasPermission('administer image styles')
> - ],
> - ];
144,148c136,137
< + $element = parent::settingsForm($form, $form_state);
< +
< + $link_types = [
< 'content' => t('Content'),
< 'file' => t('File'),
---
> - 'content' => t('Content'),
> - 'file' => t('File'),
151,157c140,144
< + ];
< + $element['image_link'] = [
< '#title' => t('Link image to'),
< '#type' => 'select',
< '#default_value' => $this->getSetting('image_link'),
< '#empty_option' => t('Nothing'),
< '#options' => $link_types,
---
> - '#title' => t('Link image to'),
> - '#type' => 'select',
> - '#default_value' => $this->getSetting('image_link'),
> - '#empty_option' => t('Nothing'),
> - '#options' => $link_types,
159,166c146,153
< + ];
<
< return $element;
< }
< @@ -145,25 +59,12 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
< * {@inheritdoc}
< */
< public function settingsSummary() {
---
> -
> - return $element;
> - }
> -
> - /**
> - * {@inheritdoc}
> - */
> - public function settingsSummary() {
181,182c168
< + $summary = parent::settingsSummary();
<
---
> -
184,186c170,171
< + $link_types = [
< 'content' => t('Linked to content'),
< 'file' => t('Linked to file'),
---
> - 'content' => t('Linked to content'),
> - 'file' => t('Linked to file'),
188,192c173,182
< + ];
< // Display this setting only if image is linked.
< $image_link_setting = $this->getSetting('image_link');
< if (isset($link_types[$image_link_setting])) {
< @@ -177,7 +78,7 @@ public function settingsSummary() {
---
> - // 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;
> - }
>
> /**
197a188
> + /** @var \Drupal\file\Entity\File[] $files */
201c192
< @@ -201,7 +102,7 @@ public function viewElements(FieldItemListInterface $items) {
---
> @@ -194,7 +54,7 @@ public function viewElements(FieldItemListInterface $items) {
210c201
< @@ -220,19 +121,18 @@ public function viewElements(FieldItemListInterface $items) {
---
> @@ -213,19 +73,18 @@ public function viewElements(FieldItemListInterface $items) {
235c226
< index 6a0d3ed..7e47597 100644
---
> index 6a0d3ed..cd4ab7e 100644
238c229
< @@ -7,14 +7,91 @@
---
> @@ -7,14 +7,154 @@
245a237
> +use Drupal\Core\Link;
249d240
< +use Drupal\Core\Utility\LinkGeneratorInterface;
260d250
< +
269,275d258
< + * The link generator.
< + *
< + * @var \Drupal\Core\Utility\LinkGeneratorInterface
< + */
< + protected $linkGenerator;
< +
< + /**
301,302d283
< + * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
< + * The link generator service.
304c285
< + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, EntityStorageInterface $image_style_storage) {
---
> + 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) {
307d287
< + $this->linkGenerator = $link_generator;
324d303
< + $container->get('link_generator'),
328,352c307
<
< /**
< * {@inheritdoc}
< @@ -34,7 +111,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
< // so that the fallback image can be rendered without affecting the
< // field values in the entity being rendered.
< $items = clone $items;
< - $items->setValue(array(
< + $items->setValue([
< 'target_id' => $file->id(),
< 'alt' => $default_image['alt'],
< 'title' => $default_image['title'],
< @@ -43,7 +120,7 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
< 'entity' => $file,
< '_loaded' => TRUE,
< '_is_default' => TRUE,
< - ));
< + ]);
< $file->_referringItem = $items[0];
< }
< }
< @@ -51,4 +128,77 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
< return parent::getEntitiesToView($items);
< }
<
---
> +
358a314
> + 'image_link' => '',
366a323,326
> + $description_link = Link::fromTextAndUrl(
> + $this->t('Configure Image Styles'),
> + Url::fromRoute('entity.image_style.collection')
> + );
373,376c333,335
< + '#description' => [
< + '#markup' => $this->linkGenerator->generate($this->t('Configure Image Styles'), new Url('entity.image_style.collection')),
< + '#access' => $this->currentUser->hasPermission('administer image styles'),
< + ],
---
> + '#description' => $description_link->toRenderable() + [
> + '#access' => $this->currentUser->hasPermission('administer image styles')
> + ],
389d347
< +
398d355
< +
399a357
> +
401a360
> +
406c365
< + $summary[] = $this->t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]);
---
> + $summary[] = t('Image style: @style', ['@style' => $image_styles[$image_style_setting]]);
409c368,378
< + $summary[] = $this->t('Original image');
---
> + $summary[] = t('Original image');
> + }
> + $link_types = [
> + '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];
412a382,396
>
> /**
> * {@inheritdoc}
> @@ -23,18 +163,19 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
> // Add the default image if needed.
> if ($items->isEmpty()) {
> $default_image = $this->getFieldSetting('default_image');
> +
> // If we are dealing with a configurable field, look in both
> // instance-level and field-level settings.
> if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
> $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
> }
> -
> if (!empty($default_image['uuid']) && $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid'])) {
413a398,419
> // Clone the FieldItemList into a runtime-only object for the formatter,
> // so that the fallback image can be rendered without affecting the
> // field values in the entity being rendered.
> $items = clone $items;
> - $items->setValue(array(
> + $items->setValue([
> 'target_id' => $file->id(),
> 'alt' => $default_image['alt'],
> 'title' => $default_image['title'],
> @@ -43,12 +184,26 @@ protected function getEntitiesToView(EntityReferenceFieldItemListInterface $item
> 'entity' => $file,
> '_loaded' => TRUE,
> '_is_default' => TRUE,
> - ));
> + ]);
> $file->_referringItem = $items[0];
> }
> }
> -
> return parent::getEntitiesToView($items);
> }
>
418c424,425
< + $dependencies = [];
---
> + // Make sure to include 3rd party dependencies.
> + $dependencies = parent::calculateDependencies();
419a427
> + // Check for image style.
420a429
> + // Add the image style dependencies as well.
429c438
< index 0000000..0e74d32
---
> index 0000000..9d4fc11
432c441
< @@ -0,0 +1,70 @@
---
> @@ -0,0 +1,85 @@
434d442
< +
443a452,453
> +use Drupal\Core\Link;
> +use Drupal\Core\Url;
462a473
> + /** @var \Drupal\file\Entity\File[] $images */
470,472c481,490
< + $url = NULL;
< +
< + $image_style_setting = $this->getSetting('image_style');
---
> + $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();
> + }
> + }
475,478c493
< + $image_style = FALSE;
< + if (!empty($image_style_setting)) {
< + $image_style = $this->imageStyleStorage->load($image_style_setting);
< + }
---
> + $image_style = ($image_style_setting = $this->getSetting('image_style')) && !empty($image_style_setting) ? $this->imageStyleStorage->load($image_style_setting) : NULL;
486a502,504
> + // Set the link url if settings require such.
> + $link_url = ($image_link_setting == 'file') ? Url::fromUri($url) : $link_url;
> +
492,497c510,521
< + $elements[$delta] = [
< + '#markup' => $url,
< + '#cache' => [
< + 'tags' => $cache_tags,
< + ],
< + ];
---
> + // Add a link if we have a valid link url.
> + $elements[$delta] = ($link_url instanceof Url)
> + ? Link::fromTextAndUrl($url, $link_url)->toRenderable() + [
> + '#cache' => [
> + 'tags' => $cache_tags,
> + ],
> + ] : [
> + '#markup' => $url,
> + '#cache' => [
> + 'tags' => $cache_tags,
> + ],
> + ];
504c528
< index c4cf485..72d990e 100644
---
> index c4cf485..8cd47ad 100644
507c531,547
< @@ -202,6 +202,26 @@ function _testImageFieldFormatters($scheme) {
---
> @@ -8,6 +8,7 @@
> namespace Drupal\image\Tests;
>
> use Drupal\Core\Field\FieldStorageDefinitionInterface;
> +use Drupal\Core\Url;
> use Drupal\field\Entity\FieldStorageConfig;
> use Drupal\user\RoleInterface;
> use Drupal\image\Entity\ImageStyle;
> @@ -93,6 +94,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 +204,51 @@ function _testImageFieldFormatters($scheme) {
530a571,595
> +
> + // 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]));
535c600
< index 3620501..1d5b60e 100644
---
> index 7749452..8a0a9a7 100644
538,600c603
< @@ -13,7 +13,9 @@
< use Drupal\Core\Field\FieldItemListInterface;
< use Drupal\Core\Form\FormStateInterface;
< use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
< +use Drupal\Core\Session\AccountInterface;
< use Drupal\Core\Url;
< +use Drupal\Core\Utility\LinkGeneratorInterface;
< use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
< use Symfony\Component\DependencyInjection\ContainerInterface;
<
< @@ -35,13 +37,6 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
< */
< protected $responsiveImageStyleStorage;
<
< - /*
< - * The image style entity storage.
< - *
< - * @var \Drupal\Core\Entity\EntityStorageInterface
< - */
< - protected $imageStyleStorage;
< -
< /**
< * Constructs a ResponsiveImageFormatter object.
< *
< @@ -59,16 +54,19 @@ class ResponsiveImageFormatter extends ImageFormatterBase implements ContainerFa
< * The view mode.
< * @param array $third_party_settings
< * Any third party settings.
< - * @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage
< - * The responsive image style storage.
< + * @param \Drupal\Core\Session\AccountInterface $current_user
< + * The current user.
< + * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
< + * The link generator service.
< * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
< * The image style storage.
< + * @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage
< + * The responsive image style storage.
< */
< - 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) {
< - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
< + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, LinkGeneratorInterface $link_generator, EntityStorageInterface $image_style_storage, EntityStorageInterface $responsive_image_style_storage) {
< + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $link_generator, $image_style_storage);
<
< $this->responsiveImageStyleStorage = $responsive_image_style_storage;
< - $this->imageStyleStorage = $image_style_storage;
< }
<
< /**
< @@ -83,8 +81,10 @@ public static function create(ContainerInterface $container, array $configuratio
< $configuration['label'],
< $configuration['view_mode'],
< $configuration['third_party_settings'],
< - $container->get('entity.manager')->getStorage('responsive_image_style'),
< - $container->get('entity.manager')->getStorage('image_style')
< + $container->get('current_user'),
< + $container->get('link_generator'),
< + $container->get('entity.manager')->getStorage('image_style'),
< + $container->get('entity.manager')->getStorage('responsive_image_style')
< );
< }
<
< @@ -92,17 +92,17 @@ public static function create(ContainerInterface $container, array $configuratio
---
> @@ -116,17 +116,17 @@ public static function create(ContainerInterface $container, array $configuratio
609c612
< + ];
---
> + ] + parent::defaultSettings();
621c624
< @@ -112,25 +112,25 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
---
> @@ -136,29 +136,29 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
632,633c635,641
< - );
< + ];
---
> - '#description' => array(
> + '#description' => [
> '#markup' => $this->linkGenerator->generate($this->t('Configure Responsive Image Styles'), new Url('entity.responsive_image_style.collection')),
> '#access' => $this->currentUser->hasPermission('administer responsive image styles'),
> - ),
> + ],
> );
653c661
< @@ -139,16 +139,16 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
---
> @@ -167,16 +167,16 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
674c682
< @@ -165,7 +165,7 @@ public function settingsSummary() {
---
> @@ -193,7 +193,7 @@ public function settingsSummary() {
683c691
< @@ -187,7 +187,7 @@ public function viewElements(FieldItemListInterface $items) {
---
> @@ -215,7 +215,7 @@ public function viewElements(FieldItemListInterface $items) {
692c700
< @@ -210,16 +210,16 @@ public function viewElements(FieldItemListInterface $items) {
---
> @@ -238,16 +238,16 @@ public function viewElements(FieldItemListInterface $items) {