diff --git a/core/modules/media_entity/src/Plugin/MediaEntity/Type/Image.php b/core/modules/media_entity/src/Plugin/MediaEntity/Type/Image.php index 4cbd777..582e3f2 100644 --- a/core/modules/media_entity/src/Plugin/MediaEntity/Type/Image.php +++ b/core/modules/media_entity/src/Plugin/MediaEntity/Type/Image.php @@ -10,7 +10,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Image\ImageFactory; use Drupal\media_entity\MediaInterface; -use Drupal\media_entity\MediaTypeBase; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -22,7 +21,7 @@ * description = @Translation("Provides business logic and metadata for local images.") * ) */ -class Image extends MediaTypeBase { +class Image extends File { /** * The image factory service. @@ -98,9 +97,23 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ + protected function createSourceFieldStorage() { + return $this->entityTypeManager + ->getStorage('field_storage_config') + ->create([ + 'entity_type' => 'media', + 'field_name' => $this->getSourceFieldName(), + 'type' => 'image', + ]); + } + + /** + * {@inheritdoc} + */ public function providedFields() { - $fields = [ - 'mime' => $this->t('Image MIME'), + $fields = parent::providedFields(); + + $fields += [ 'width' => $this->t('Width'), 'height' => $this->t('Height'), ]; @@ -123,25 +136,25 @@ public function providedFields() { * {@inheritdoc} */ public function getField(MediaInterface $media, $name) { + $value = parent::getField($media, $name); + + if ($value !== FALSE) { + return $value; + } + // Get the file, image and EXIF data. /** @var \Drupal\file\FileInterface $file */ - $file = $this->getSourceField($media); + $file = $this->getSourceFieldValue($media); $uri = $file->getFileUri(); $image = $this->imageFactory->get($uri); // Return the field. switch ($name) { - case 'mime': - return $file->getMimeType() ?: FALSE; - case 'width': return $image->getWidth() ?: FALSE; case 'height': return $image->getHeight() ?: FALSE; - - case 'size': - return $file->getSize() ?: FALSE; } if ($this->canReadExifData()) { @@ -180,7 +193,7 @@ public function getField(MediaInterface $media, $name) { * @return \Drupal\Core\Entity\EntityInterface * Returns file entity. */ - protected function getSourceField(MediaInterface $media) { + protected function getSourceFieldValue(MediaInterface $media) { $source_field = $this->configuration['source_field']; if (empty($source_field)) { @@ -242,7 +255,7 @@ public function getDefaultThumbnail() { * {@inheritdoc} */ public function thumbnail(MediaInterface $media) { - return $this->getSourceField($media)->getFileUri(); + return $this->getSourceFieldValue($media)->getFileUri(); } /** @@ -293,7 +306,7 @@ protected function getExifData($uri) { * {@inheritdoc} */ public function getDefaultName(MediaInterface $media) { - return $this->getSourceField($media)->getFilename(); + return $this->getSourceFieldValue($media)->getFilename(); } } diff --git a/core/modules/media_entity/tests/src/FunctionalJavascript/MediaHandlerImageTest.php b/core/modules/media_entity/tests/src/FunctionalJavascript/MediaHandlerImageTest.php new file mode 100644 index 0000000..e280231 --- /dev/null +++ b/core/modules/media_entity/tests/src/FunctionalJavascript/MediaHandlerImageTest.php @@ -0,0 +1,137 @@ + $field_name, + 'entity_type' => 'media', + 'type' => $field_type, + ]); + $storage->save(); + + $config = FieldConfig::create([ + 'field_storage' => $storage, + 'bundle' => $bundle_name, + ]); + $config->save(); + + // Make the field visible in the form display. + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $component = \Drupal::service('plugin.manager.field.widget') + ->prepareConfiguration($field_type, []); + + entity_get_form_display('media', $bundle_name, 'default') + ->setComponent($field_name, $component) + ->save(); + } + + /** + * Helper to create a set of fields in a bundle. + * + * @param array $fields + * An associative array where keys are field names and values field types. + * @param string $bundle_name + * The bundle machine name. + */ + protected function createMediaFields(array $fields, $bundle_name) { + foreach ($fields as $field_name => $field_type) { + $this->createMediaField($field_name, $field_type, $bundle_name); + } + } + + /** + * Just dummy test to check generic methods. + */ + public function testMediaImagePlugin() { + $bundle_name = 'test_media_image_mf1v7s1hwn'; + $source_field_id = 'field_media_image'; + $provided_fields = [ + 'mime', + 'width', + 'height', + 'size', + 'created', + 'model', + 'iso', + 'exposure', + 'aperture', + 'focal_length', + ]; + + $session = $this->getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + // Create image media handler. + $this->createMediaBundleTest($bundle_name, 'image', $provided_fields); + + // Create a supported and a non-supported field. + $fields = [ + 'field_string_mime' => 'string', + 'field_string_width' => 'string', + 'field_string_model' => 'string', + ]; + $this->createMediaFields($fields, $bundle_name); + + // Adjust the allowed extensions on the file field. + $image_field = FieldConfig::load("media.{$bundle_name}.{$source_field_id}"); + $image_field->setSetting('file_extensions', 'png jpeg')->save(); + + // Hide the media name to test default name generation. + $this->hideMediaField('name', $bundle_name); + + $this->drupalGet("admin/structure/media/manage/$bundle_name"); + $this->assertSelectOptions("type_configuration[image][source_field]", + [$source_field_id], + ['field_string_mime', 'field_string_width', 'field_string_model'] + ); + $page->selectFieldOption("field_mapping[mime]", 'field_string_mime'); + $page->selectFieldOption("field_mapping[width]", 'field_string_width'); + $page->selectFieldOption("field_mapping[model]", 'field_string_model'); + $page->pressButton('Save'); + + // Create a media item. + $this->drupalGet("media/add/{$bundle_name}"); + $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::root() . '/core/modules/media_entity/tests/fixtures/exif_example.jpeg'); + $assert_session->assertWaitOnAjaxRequest(); + $page->fillField("{$source_field_id}[0][alt]", 'EXIF Image Alt Text'); + $page->pressButton('Save and publish'); + + $assert_session->addressEquals('media/1'); + + // Make sure the thumbnail is created from uploaded image. + $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'exif_example.jpeg'); + + // Load the media and check that all fields are properly populated. + $media = Media::load(1); + $this->assertEquals('exif_example.jpeg', $media->label()); + $this->assertEquals('Drupal EXIF Camera', $media->get('field_string_model')->value); + $this->assertEquals('200', $media->get('field_string_width')->value); + $this->assertEquals('image/jpeg', $media->get('field_string_mime')->value); + } + +} diff --git a/core/modules/media_entity/tests/src/FunctionalJavascript/MediaTypeImageTest.php b/core/modules/media_entity/tests/src/FunctionalJavascript/MediaTypeImageTest.php deleted file mode 100644 index 4833dc3..0000000 --- a/core/modules/media_entity/tests/src/FunctionalJavascript/MediaTypeImageTest.php +++ /dev/null @@ -1,78 +0,0 @@ -getSession(); - $page = $session->getPage(); - $assert_session = $this->assertSession(); - - // Create image media handler. - $this->createMediaBundleTest($bundle_name, 'image', $provided_fields); - - // Create a supported and a non-supported field. - $fields = [ - 'field_image' => 'image', - 'field_string_mime' => 'string', - 'field_string_width' => 'string', - 'field_string_model' => 'string', - ]; - $this->createMediaFields($fields, $bundle_name); - - // Adjust the allowed extensions on the file field. - $file_field = FieldConfig::load("media.$bundle_name.field_image"); - $file_field->setSetting('file_extensions', 'png jpeg')->save(); - - $this->drupalGet("admin/structure/media/manage/$bundle_name"); - $this->assertSelectOptions("type_configuration[image][source_field]", - ['field_image'], - ['field_string_mime', 'field_string_width', 'field_string_model'] - ); - $page->selectFieldOption("type_configuration[image][source_field]", 'field_image'); - $page->selectFieldOption("field_mapping[mime]", 'field_string_mime'); - $page->selectFieldOption("field_mapping[width]", 'field_string_width'); - $page->selectFieldOption("field_mapping[model]", 'field_string_model'); - $page->pressButton('Save media bundle'); - - // Hide the media name to test default name generation. - $this->hideMediaField('name', $bundle_name); - - // Create a media item. - $this->drupalGet("media/add/$bundle_name"); - $page->attachFileToField("files[field_image_0]", \Drupal::root() . '/core/modules/media_entity/tests/fixtures/exif_example.jpeg'); - $assert_session->assertWaitOnAjaxRequest(); - $page->fillField('field_image[0][alt]', 'EXIF Image Alt Text'); - $page->pressButton('Save and publish'); - - $assert_session->addressEquals('media/1'); - - // Make sure the thumbnail is created from uploaded image. - $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'exif_example.jpeg'); - - // Load the media and check that all fields are properly populated. - $media = Media::load(1); - $this->assertEquals('exif_example.jpeg', $media->label()); - $this->assertEquals('Drupal EXIF Camera', $media->get('field_string_model')->value); - $this->assertEquals('200', $media->get('field_string_width')->value); - $this->assertEquals('image/jpeg', $media->get('field_string_mime')->value); - } - -}