Commit 8f7562b9 authored by David Suissa's avatar David Suissa
Browse files

Issue #3294374 by DYdave: Added support for Responsive images with new...

Issue #3294374 by DYdave: Added support for Responsive images with new formatter 'responsive_image_link_formatter' integrated with core 'responsive_image'.
parent 11ce9924
Loading
Loading
Loading
Loading
+1 −1
Changes for composer.json: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
    "name": "drupal/image_link_formatter",
    "description": "Display images wrapped within a link provided by a field.",
    "type": "drupal-module",
    "keywords": ["Drupal", "Formatter"],
    "keywords": ["Drupal", "Formatter", "Responsive"],
    "homepage": "https://www.drupal.org/project/image_link_formatter",
    "license": "GPL-2.0-or-later",
    "authors": [
+10
Changes for responsive_image_link_formatter/responsive_image_link_formatter.info.yml: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
name: Responsive Image Link Formatter
description: Extends Core 'responsive_image' formatter to allow displaying responsive images wrapped within a link provided by a field.
package: Fields

type: module
core_version_requirement: '^9.3 || ^10'

dependencies:
  - image_link_formatter:image_link_formatter
  - drupal:responsive_image
+101
Changes for responsive_image_link_formatter/src/Plugin/Field/FieldFormatter/ResponsiveImageLinkFormatter.php: 101 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\responsive_image_link_formatter\Plugin\Field\FieldFormatter;

/**
 * @file
 * Contains implementation of formatter 'responsive_image_link_formatter'.
 */

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\image_link_formatter\Plugin\Field\FieldFormatter\ImageLinkFormatterTrait;
use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Extends Core 'responsive_image' formatter to add a link from a custom field.
 *
 * Essentially combines core 'ResponsiveImageFormatter' object with methods of
 * the trait 'ImageLinkFormatterTrait' which extends formatter's methods to
 * allow wrapping a link from a custom field around the image field formatter's
 * output.
 *
 * @FieldFormatter(
 *   id = "responsive_image_link_formatter",
 *   label = @Translation("Responsive image wrapped within link field"),
 *   field_types = {
 *     "image"
 *   },
 *   quickedit = {
 *     "editor" = "image"
 *   }
 * )
 *
 * @see \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter
 * @see \Drupal\image_link_formatter\Plugin\Field\FieldFormatter\ImageLinkFormatterTrait
 */
class ResponsiveImageLinkFormatter extends ResponsiveImageFormatter implements ContainerFactoryPluginInterface {

  use ImageLinkFormatterTrait;

  /**
   * Constructs a ResponsiveImageLinkFormatter 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.
   * @param \Drupal\Core\Entity\EntityStorageInterface $responsive_image_style_storage
   *   The responsive image style storage.
   * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
   *   The image style storage.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The link generator service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   */
  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, EntityFieldManagerInterface $entity_field_manager) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $responsive_image_style_storage, $image_style_storage, $link_generator, $current_user);
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   *
   * Adds the 'entity_field.manager' service to parent's container.
   */
  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('entity_type.manager')->getStorage('responsive_image_style'),
      $container->get('entity_type.manager')->getStorage('image_style'),
      $container->get('link_generator'),
      $container->get('current_user'),
      $container->get('entity_field.manager')
    );
  }

}
+84
Changes for responsive_image_link_formatter/tests/src/Functional/ResponsiveImageLinkFormatterTest.php: 84 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\responsive_image_link_formatter\Functional;

/**
 * @file
 * Contains Functional test cases for the 'responsive_image_link_formatter'.
 */

use Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait;
use Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests the output of a node with a responsive image wrapped within a link.
 *
 * Extends the core test case 'ResponsiveImageFieldDisplayTest' for the creation
 * of the base configuration objects (responsive image styles), user login,
 * needed for the tests ('setUp'). On top, the logic related with the 'link'
 * field is added by the trait 'ImageLinkFormatterTestTrait' which provides
 * common methods, such as the effective tests
 * ('doTestImageLinkFormatterWrappedImage', which also creates its 'image'
 * field), or their assertions ('assertImageLinkFormatter'), based on the
 * HTML results to be expected, defined by this object (for the
 * 'responsive_image_link_formatter).
 *
 * @group image_link_formatter
 * @see \Drupal\Tests\image_link_formatter\Functional\ImageLinkFormatterTest
 * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait
 * @see \Drupal\Tests\responsive_image\Functional\ResponsiveImageFieldDisplayTest
 */
class ResponsiveImageLinkFormatterTest extends ResponsiveImageFieldDisplayTest {

  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }

  use ImageLinkFormatterTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['field_ui', 'responsive_image_link_formatter'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests the display of a responsive image wrapped within a custom link field.
   *
   * Defines the results to be expected for the supported Drupal versions and
   * calls the standard test method from the trait which displays a node with
   * the responsive image link formatter with and without a link and checks
   * whether the expected HTML is found on the page.
   *
   * @see \Drupal\Tests\image_link_formatter\Functional\ImageLinkFormatterTest::testImageLinkFormatterWrappedImage()
   * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::doTestImageLinkFormatterWrappedImage()
   * @see \Drupal\Tests\image_link_formatter\Traits\Functional\ImageLinkFormatterTestTrait::assertImageLinkFormatter()
   */
  public function testResponsiveImageLinkFormatterWrappedImage() {
    // For each supported Drupal core version, provide the expected HTML result
    // specific to the test, with which to compare the output generated by the
    // formatter, based on the evolutions of the core 'image' module and its
    // formatters, for example with added attributes.
    $expected_result = [
      // Moving forward, attribute 'loading' added by default, see #3173719.
      '9.4' => [
        'link' => '<a href="http://example.com"><img src="@file_url" width="40" height="20" alt="" loading="lazy" /></a>',
        'no_link' => '<img src="@file_url" width="40" height="20" alt="" loading="lazy" />',
      ],
      // As of 9.4.0, lazy loading is part of core image formatter's
      // configuration, changing the position of the attribute, see #3173180.
      'all' => [
        'link' => '<a href="http://example.com"><img alt="" src="@file_url" width="40" height="20" loading="lazy" /></a>',
        'no_link' => '<img alt="" src="@file_url" width="40" height="20" loading="lazy" />',
      ],
    ];
    // Call the test from the trait for the 'responsive_image_link_formatter'.
    $this->doTestImageLinkFormatterWrappedImage('responsive_image_link_formatter', $expected_result);
  }

}
+59
Changes for responsive_image_link_formatter/tests/src/Kernel/ResponsiveImageLinkFormatterTest.php: 59 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\responsive_image_link_formatter\Kernel;

/**
 * @file
 * Contains Kernel test cases for the 'responsive_image_link_formatter'.
 */

use Drupal\Tests\image\Kernel\ImageFormatterTest;
use Drupal\Tests\image_link_formatter\Traits\Kernel\ImageLinkFormatterTestTrait;

/**
 * Tests the rendering of a responsive image wrapped within a custom link.
 *
 * Extends the core test case 'ImageFormatterTest' for the creation of the base
 * configuration objects needed for the tests ('setUp'), in particular the
 * 'image' field. On top, the logic related with the 'link' field is added by
 * the trait 'ImageLinkFormatterTestTrait' which provides common methods, such
 * as the effective tests ('doTestImageLinkFormatterUrlOptions'), or the
 * creation and configuration of the link field.
 *
 * @group image_link_formatter
 * @see \Drupal\Tests\image\Kernel\ImageFormatterTest
 * @see \Drupal\Tests\image_link_formatter\Kernel\ImageLinkFormatterTest
 * @see \Drupal\Tests\image_link_formatter\Traits\Kernel\ImageLinkFormatterTestTrait
 */
class ResponsiveImageLinkFormatterTest extends ImageFormatterTest {

  use ImageLinkFormatterTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'file',
    'image',
    'link',
    'responsive_image',
    'responsive_image_link_formatter',
  ];

  /**
   * Tests rendering link attributes options wrapped around a responsive image.
   *
   * Call standard test method from the trait which renders the values of the
   * image field of a node, with the responsive image link formatter with a
   * link and checks whether the expected HTML is found with the correct
   * attributes options.
   *
   * @see \Drupal\Tests\image_link_formatter\Kernel\ImageLinkFormatterTest::testImageLinkFormatterUrlOptions()
   * @see \Drupal\Tests\image_link_formatter\Traits\Kernel\ImageLinkFormatterTestTrait::doTestImageLinkFormatterUrlOptions()
   */
  public function testImageLinkFormatterUrlOptions() {
    // Call the test from the trait for the 'responsive_image_link_formatter'.
    $this->doTestImageLinkFormatterUrlOptions('responsive_image_link_formatter');
  }

}
Loading