diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index e45f4e2679..0b69ef09e5 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -273,25 +273,31 @@ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); - // Try to set fields provided by the media source and mapped in - // media type config. - foreach ($this->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) { - // Only save value in entity field if empty. Do not overwrite existing - // data. - if ($this->hasField($entity_field_name) && ($this->get($entity_field_name)->isEmpty() || $this->sourceFieldChanged()) && ($value = $media_source->getMetadata($this, $metadata_attribute_name)) && $value !== NULL) { - $this->set($entity_field_name, $value); + foreach ($this->translations as $langcode => $data) { + if ($this->hasTranslation($langcode)) { + $translation = $this->getTranslation($langcode); + // Try to set fields provided by the media source and mapped in + // media type config. + foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) { + // Only save value in entity field if empty. Do not overwrite existing + // data. + if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->sourceFieldChanged()) && ($value = $media_source->getMetadata($translation, $metadata_attribute_name)) && $value !== NULL) { + $translation->set($entity_field_name, $value); + } + } + + // Try to set a default name for this media item if no label is + // provided. + if (!$translation->label()) { + $translation->set('name', $media_source->getMetadata($translation, $media_source->getPluginDefinition()['default_name_metadata_attribute'])); + } + + // Set thumbnail. + if ($translation->shouldUpdateThumbnail()) { + $translation->updateThumbnail(); + } } } - - // Try to set a default name for this media item if no label is provided. - if (!$this->label()) { - $this->set('name', $media_source->getMetadata($this, $media_source->getPluginDefinition()['default_name_metadata_attribute'])); - } - - // Set thumbnail. - if ($this->shouldUpdateThumbnail()) { - $this->updateThumbnail(); - } } /** @@ -300,8 +306,13 @@ public function preSave(EntityStorageInterface $storage) { public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); $is_new = !$update; - if ($this->bundle->entity->thumbnailDownloadsAreQueued() && $this->shouldUpdateThumbnail($is_new)) { - \Drupal::queue('media_entity_thumbnail')->createItem(['id' => $this->id()]); + foreach ($this->translations as $langcode => $data) { + if ($this->hasTranslation($langcode)) { + $translation = $this->getTranslation($langcode); + if ($translation->bundle->entity->thumbnailDownloadsAreQueued() && $translation->shouldUpdateThumbnail($is_new)) { + \Drupal::queue('media_entity_thumbnail')->createItem(['id' => $translation->id()]); + } + } } } diff --git a/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php b/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php index f31a7ba095..a181858b86 100644 --- a/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php +++ b/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php @@ -2,7 +2,7 @@ namespace Drupal\media\Form; -use Drupal\Core\Entity\Query\QueryFactory; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityDeleteForm; use Drupal\Core\Form\FormStateInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -13,20 +13,20 @@ class MediaTypeDeleteConfirmForm extends EntityDeleteForm { /** - * The query factory to create entity queries. + * The entity type manager. * - * @var \Drupal\Core\Entity\Query\QueryFactory + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $queryFactory; + protected $entityTypeManager; /** * Constructs a new MediaTypeDeleteConfirm object. * - * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory - * The entity query factory. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(QueryFactory $query_factory) { - $this->queryFactory = $query_factory; + public function __construct(EntityTypeManagerInterface $entity_type_manager) { + $this->entityTypeManager = $entity_type_manager; } /** @@ -34,7 +34,7 @@ public function __construct(QueryFactory $query_factory) { */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.query') + $container->get('entity_type.manager') ); } @@ -42,7 +42,7 @@ public static function create(ContainerInterface $container) { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $num_entities = $this->queryFactory->get('media') + $num_entities = $this->entityTypeManager->getStorage('media')->getQuery() ->condition('bundle', $this->entity->id()) ->count() ->execute(); diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index 397e7e0df1..9ae84e9baa 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -333,6 +333,8 @@ public function save(array $form, FormStateInterface $form_state) { $component = \Drupal::service('plugin.manager.field.widget') ->prepareConfiguration($field_type, []); + // @todo Replace entity_get_form_display() when #2367933 is done. + // https://www.drupal.org/node/2872159. entity_get_form_display('media', $media_type->id(), 'default') ->setComponent($field_name, $component) ->save(); @@ -342,6 +344,8 @@ public function save(array $form, FormStateInterface $form_state) { $component = \Drupal::service('plugin.manager.field.formatter') ->prepareConfiguration($field_type, []); + // @todo Replace entity_get_display() when #2367933 is done. + // https://www.drupal.org/node/2872159. entity_get_display('media', $media_type->id(), 'default') ->setComponent($field_name, $component) ->save(); diff --git a/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php index 56defffc88..e0e5294122 100644 --- a/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php +++ b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php @@ -22,7 +22,6 @@ class MediaTemplateSuggestionsTest extends MediaFunctionalTestBase { * Tests template suggestions from media_theme_suggestions_media(). */ public function testMediaThemeHookSuggestions() { - $media_type = $this->createMediaType([ 'new_revision' => FALSE, 'queue_thumbnail_downloads' => FALSE, @@ -41,8 +40,7 @@ public function testMediaThemeHookSuggestions() { $variables['elements'] = $build; $suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_media', [$variables]); - $this->assertEqual($suggestions, ['media__full', 'media__' . $media_type->id(), 'media__' . $media_type->id() . '__full'], 'Found expected media suggestions.'); - + $this->assertEquals($suggestions, ['media__full', 'media__' . $media_type->id(), 'media__' . $media_type->id() . '__full'], 'Found expected media suggestions.'); } } diff --git a/core/modules/media/tests/src/Kernel/MediaTranslationTest.php b/core/modules/media/tests/src/Kernel/MediaTranslationTest.php index 7f73e9d396..009e7c4d6b 100644 --- a/core/modules/media/tests/src/Kernel/MediaTranslationTest.php +++ b/core/modules/media/tests/src/Kernel/MediaTranslationTest.php @@ -94,9 +94,9 @@ public function testTranslatableFieldSaveLoad() { } $this->assertTrue($result, new FormattableMarkup('%language translation field value not correct.', ['%language' => $langcode])); $this->assertEquals('public://' . $langcode . '.png', $media_translation->getSource()->getMetadata($media_translation, 'thumbnail_uri'), new FormattableMarkup('%language translation thumbnail metadata attribute is not correct.', ['%language' => $langcode])); - $this->assertEquals('public://' . $langcode . '.png', $media_translation->get('thumbnail')->entity->getFileUri(), new FormattableMarkup('%language translation thumbnail metadata attribute is not correct.', ['%language' => $langcode])); - $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->getSource()->getMetadata($media_translation, 'test_thumbnail_alt')); - $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->get('thumbnail')->alt); + $this->assertEquals('public://' . $langcode . '.png', $media_translation->get('thumbnail')->entity->getFileUri(), new FormattableMarkup('%language translation thumbnail value is not correct.', ['%language' => $langcode])); + $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->getSource()->getMetadata($media_translation, 'test_thumbnail_alt'), new FormattableMarkup('%language translation thumbnail alt metadata attribute is not correct.', ['%language' => $langcode])); + $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->get('thumbnail')->alt, new FormattableMarkup('%language translation thumbnail alt value is not correct.', ['%language' => $langcode])); } }