Unverified Commit 85468ee4 authored by Alex Pott's avatar Alex Pott
Browse files

feat: #3554447 Use #attributes instead of #item_attributes

By: grimreaper
By: nicxvan
By: smustgrave
parent ab5a470b
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for core/modules/image/image.module: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -151,7 +151,7 @@ function template_preprocess_image_widget(&$variables): void {
 * @param array $variables
 *   An associative array containing:
 *   - item: An ImageItem object.
 *   - item_attributes: An optional associative array of html attributes to be
 *   - attributes: An optional associative array of html attributes to be
 *     placed in the img tag.
 *   - image_style: An optional image style.
 *   - url: An optional \Drupal\Core\Url object.
+6 −2
Changes for core/modules/image/src/Hook/ImageThemeHooks.php: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ public function theme() : array {
      'image_formatter' => [
        'variables' => [
          'item' => NULL,
          'attributes' => NULL,
          'item_attributes' => NULL,
          'url' => NULL,
          'image_style' => NULL,
@@ -145,7 +146,7 @@ public function preprocessImageWidget(array &$variables): void {
   * @param array $variables
   *   An associative array containing:
   *   - item: An ImageItem object.
   *   - item_attributes: An optional associative array of html attributes to be
   *   - attributes: An optional associative array of html attributes to be
   *     placed in the img tag.
   *   - image_style: An optional image style.
   *   - url: An optional \Drupal\Core\Url object.
@@ -162,7 +163,10 @@ public function preprocessImageFormatter(array &$variables): void {
        '#theme' => 'image',
      ];
    }
    $variables['image']['#attributes'] = $variables['item_attributes'];
    $variables['image']['#attributes'] = $variables['attributes'] ?? $variables['item_attributes'];
    if (isset($variables['item_attributes'])) {
      @trigger_error('Usage of #item_attributes is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use #attributes instead. See https://www.drupal.org/node/3554585', E_USER_DEPRECATED);
    }

    $item = $variables['item'];

+3 −3
Changes for core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -251,16 +251,16 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
      // Extract field item attributes for the theme function, and unset them
      // from the $item so that the field template does not re-render them.
      $item = $file->_referringItem;
      $item_attributes = $item->_attributes;
      $attributes = $item->_attributes;
      unset($item->_attributes);

      $image_loading_settings = $this->getSetting('image_loading');
      $item_attributes['loading'] = $image_loading_settings['attribute'];
      $attributes['loading'] = $image_loading_settings['attribute'];

      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#item_attributes' => $item_attributes,
        '#attributes' => $attributes,
        '#image_style' => $image_style_setting,
        '#url' => $url,
        '#cache' => [
+37 −0
Changes for core/modules/image/tests/src/Kernel/ImageThemeFunctionTest.php: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\TestFileCreationTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
@@ -162,6 +163,42 @@ public function testImageFormatterTheme(): void {
    $this->assertCount(1, $elements, 'theme_image_formatter() correctly renders a link fragment.');
  }

  /**
   * Test the deprecated item_attributes property.
   */
  #[IgnoreDeprecations]
  public function testImageFormatterDeprecatedProperty(): void {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');

    // Create an image.
    $files = $this->drupalGetTestFiles('image');
    $file = reset($files);
    $original_uri = \Drupal::service('file_system')->copy($file->uri, 'public://', FileExists::Rename);

    // Create a test entity with the image field set.
    $entity = EntityTest::create();
    $entity->image_test->target_id = $this->image->id();
    $entity->image_test->alt = NULL;
    $entity->image_test->uri = $original_uri;
    $entity->save();

    // Create the base element that we'll use in the tests below.
    $path = $this->randomMachineName();
    $element = [
      '#theme' => 'image_formatter',
      '#item' => $entity->image_test,
      '#url' => Url::fromUri('base:' . $path),
      '#item_attributes' => [
        'class' => [
          'foo',
        ],
      ],
    ];
    $renderer->renderRoot($element);
    $this->expectDeprecation('Usage of #item_attributes is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use #attributes instead. See https://www.drupal.org/node/3554585');
  }

  /**
   * Tests usage of the image style theme function.
   */
+1 −1
Changes for core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -146,7 +146,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $media->get('thumbnail')->first(),
        '#item_attributes' => [
        '#attributes' => [
          'loading' => $this->getSetting('image_loading')['attribute'],
        ],
        '#image_style' => $this->getSetting('image_style'),
Loading