diff --git a/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php index ca75c9b7c5d2ef4f365c0426a3d7e02bf03afe12..6dcd7cc59edd05e3674b50358ecbf784d8f7e30c 100644 --- a/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php +++ b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php @@ -4,7 +4,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\image\ImageStyleStorageInterface; @@ -56,21 +55,11 @@ class MediaThumbnailFormatter extends ImageFormatter { * The current user. * @param \Drupal\image\ImageStyleStorageInterface $image_style_storage * The image style entity storage handler. - * @param \Drupal\Core\File\FileUrlGeneratorInterface|null $file_url_generator - * The file URL generator. - * @param \Drupal\Core\Render\RendererInterface|null $renderer + * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, $file_url_generator = NULL, $renderer = NULL) { - if (!$file_url_generator instanceof FileUrlGeneratorInterface) { - $file_url_generator = \Drupal::service('file_url_generator'); - @trigger_error('Calling ' . __METHOD__ . '() without the $file_url_generator argument is deprecated in drupal:9.3.0 and $file_url_generator argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3255887', E_USER_DEPRECATED); - } - if (!$renderer instanceof RendererInterface) { - $renderer = \Drupal::service('renderer'); - @trigger_error('Calling ' . __METHOD__ . '() without the $renderer argument is deprecated in drupal:9.3.0 and $renderer argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3255887', E_USER_DEPRECATED); - } - parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage, $file_url_generator); + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, RendererInterface $renderer) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage); $this->renderer = $renderer; } @@ -88,7 +77,6 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['third_party_settings'], $container->get('current_user'), $container->get('entity_type.manager')->getStorage('image_style'), - $container->get('file_url_generator'), $container->get('renderer') ); } @@ -109,6 +97,7 @@ protected function needsEntityLoad(EntityReferenceItem $item) { */ public function settingsForm(array $form, FormStateInterface $form_state) { $element = parent::settingsForm($form, $form_state); + $link_types = [ 'content' => $this->t('Content'), 'media' => $this->t('Media item'), diff --git a/core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php b/core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php deleted file mode 100644 index a8e2e1d66eb35a96b93e6f257efe69d654177098..0000000000000000000000000000000000000000 --- a/core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php +++ /dev/null @@ -1,146 +0,0 @@ -<?php - -namespace Drupal\Tests\media\Functional\FieldFormatter; - -use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; -use Drupal\file\Entity\File; -use Drupal\media\Entity\Media; -use Drupal\Tests\media\Functional\MediaFunctionalTestBase; -use Drupal\Tests\TestFileCreationTrait; - -/** - * @covers \Drupal\media\Plugin\Field\FieldFormatter\MediaThumbnailFormatter - * - * @group media - */ -class MediaThumbnailFormatterTest extends MediaFunctionalTestBase { - - use TestFileCreationTrait; - - /** - * {@inheritdoc} - */ - protected $defaultTheme = 'stark'; - - /** - * Tests the media thumbnail field formatter. - */ - public function testRender() { - $this->drupalLogin($this->adminUser); - - /** @var \Drupal\Core\Render\Renderer $renderer */ - $renderer = $this->container->get('renderer'); - - /** @var \Drupal\node\NodeStorage $node_storage */ - $node_storage = $this->container->get('entity_type.manager')->getStorage('node'); - - // Create an image media type for testing the formatter. - $this->createMediaType('image', ['id' => 'image']); - - // Create an article content type. - $this->drupalCreateContentType([ - 'type' => 'article', - 'name' => 'Article', - ]); - - // Creates an entity reference field for media. - $field_storage = FieldStorageConfig::create([ - 'field_name' => 'field_media_reference', - 'type' => 'entity_reference', - 'entity_type' => 'node', - 'cardinality' => 1, - 'settings' => [ - 'target_type' => 'media', - ], - ]); - $field_storage->save(); - FieldConfig::create([ - 'field_storage' => $field_storage, - 'bundle' => 'article', - 'label' => 'Reference media', - 'translatable' => FALSE, - ])->save(); - - // Alter the form display. - $this->container->get('entity_display.repository') - ->getFormDisplay('node', 'article') - ->setComponent('field_media_reference', [ - 'type' => 'entity_reference_autocomplete', - ]) - ->save(); - - // The first case is validate the image with media link. - $this->changeMediaReferenceFieldLinkType('media'); - - // Create and upload a file to the media. - $file = File::create([ - 'uri' => current($this->getTestFiles('image'))->uri, - ]); - $file->save(); - $mediaImage = Media::create([ - 'bundle' => 'image', - 'name' => 'Test image', - 'field_media_image' => $file->id(), - ]); - $mediaImage->save(); - - // Save the article node. - $edit = [ - 'title[0][value]' => $this->randomMachineName(), - ]; - $edit['field_media_reference[0][target_id]'] = $mediaImage->getName(); - $this->drupalGet('node/add/article'); - $this->submitForm($edit, 'Save'); - - // Retrieve node id. - $matches = []; - preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches); - $nid = $matches[1]; - - // Loads the new node entity. - $node = $node_storage->load($nid); - - /** @var \Drupal\media\Entity\Media $media */ - $media = $node->field_media_reference->entity; - $image = [ - '#theme' => 'image_formatter', - '#item' => $media->get('thumbnail')->first(), - '#item_attributes' => [], - '#image_style' => '', - '#url' => $media->toUrl(), - ]; - // Check the image being loaded. - $this->assertSession()->responseContains($renderer->renderRoot($image)); - $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $media->getCacheTags()[0]); - - // The second scenario is to validate the image thumbnail with content link. - $this->changeMediaReferenceFieldLinkType('content'); - $node_storage->resetCache([$nid]); - - $image['#url'] = $node->toUrl(); - $this->assertSession()->responseContains($renderer->renderRoot($image)); - $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $media->getCacheTags()[0]); - } - - /** - * Helper function to change field display. - * - * @param string $type - * Image link type. - */ - private function changeMediaReferenceFieldLinkType(string $type): void { - // Change the display to use the media thumbnail formatter with image link. - $this->container->get('entity_display.repository') - ->getViewDisplay('node', 'article', 'default') - ->setComponent('field_media_reference', [ - 'type' => 'media_thumbnail', - 'settings' => [ - 'image_link' => $type, - 'image_style' => '', - ], - ]) - ->save(); - } - -}