Unverified Commit 99a2607a authored by Alex Pott's avatar Alex Pott
Browse files

task: #3571111 Remove item_attributes deprecations

By: grimreaper
By: dcam
parent 78087dfd
Loading
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -108,7 +108,6 @@ public function theme() : array {
        'variables' => [
          'item' => NULL,
          'attributes' => NULL,
          'item_attributes' => NULL,
          'url' => NULL,
          'image_style' => NULL,
        ],
@@ -163,10 +162,7 @@ public function preprocessImageFormatter(array &$variables): void {
        '#theme' => 'image',
      ];
    }
    $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);
    }
    $variables['image']['#attributes'] = $variables['attributes'];

    $item = $variables['item'];

+0 −37
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\TestFileCreationTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
@@ -163,42 +162,6 @@ 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.
   */
+0 −6
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ public function theme() : array {
        'variables' => [
          'attributes' => [],
          'item' => NULL,
          'item_attributes' => NULL,
          'url' => NULL,
          'responsive_image_style_id' => NULL,
        ],
@@ -86,11 +85,6 @@ public function preprocessResponsiveImageFormatter(&$variables): void {
      $attributes['title'] = $item->title;
    }
    $attributes['alt'] = $item->alt;
    // Need to check that item_attributes has a value since it can be NULL.
    if ($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);
      $attributes += $variables['item_attributes'];
    }
    if (($entity = $item->entity) && empty($item->uri)) {
      $variables['responsive_image']['#uri'] = $entity->getFileUri();
    }
+0 −33
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
use Drupal\Tests\TestFileCreationTrait;
use Drupal\user\RoleInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
@@ -451,38 +450,6 @@ public function testResponsiveImageFieldFormatterAttributesInRenderArray(): void
    $this->assertStringContainsString('data-test2="test2"', $html_output);
  }

  /**
   * Test the deprecated item_attributes property.
   */
  #[IgnoreDeprecations]
  public function testResponsiveImageFieldFormatterDeprecatedProperty(): void {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $field_name = $this->randomMachineName();
    $this->createImageField($field_name, 'node', 'article', ['uri_scheme' => 'public']);

    // Create a new node with an image attached.
    $test_image = current($this->getTestFiles('image'));
    $nid = $this->uploadNodeImage($test_image, $field_name, 'article', $this->randomMachineName());
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);

    $image = [
      '#theme' => 'responsive_image_formatter',
      '#item' => $node->{$field_name}[0],
      '#responsive_image_style_id' => 'style_one',
      '#item_attributes' => [
        'data-test1' => 'test1',
        'data-test2' => 'test2',
      ],
    ];
    $html_output = str_replace("\n", '', (string) $renderer->renderRoot($image));
    $this->assertStringContainsString('data-test1="test1"', $html_output);
    $this->assertStringContainsString('data-test2="test2"', $html_output);
    $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 responsive image formatter on node display with one and two sources.
   */