diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index d175572..c63a6ff 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -88,6 +88,42 @@ class Media extends EditorialContentEntityBase implements MediaInterface { /** * {@inheritdoc} */ + public function getType() { + return $this->bundle(); + } + + /** + * {@inheritdoc} + */ + public function label() { + return $this->getName(); + } + + /** + * {@inheritdoc} + */ + public function getName() { + $name = $this->get('name'); + + if ($name->isEmpty()) { + $media_source = $this->getSource(); + return $media_source->getMetadata($this, $media_source->getPluginDefinition()['default_name_metadata_attribute']); + } + else { + return $name->value; + } + } + + /** + * {@inheritdoc} + */ + public function setName($name) { + return $this->set('name', $name); + } + + /** + * {@inheritdoc} + */ public function getCreatedTime() { return $this->get('created')->value; } diff --git a/core/modules/media/src/MediaInterface.php b/core/modules/media/src/MediaInterface.php index 7064441..d2d038f 100644 --- a/core/modules/media/src/MediaInterface.php +++ b/core/modules/media/src/MediaInterface.php @@ -14,6 +14,33 @@ interface MediaInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface, EntityOwnerInterface, EntityPublishedInterface { /** + * Gets the media type. + * + * @return string + * The media type. + */ + public function getType(); + + /** + * Gets the media item name. + * + * @return string + * The name of the media item. + */ + public function getName(); + + /** + * Sets the media item name. + * + * @param string $name + * The name of the media item. + * + * @return \Drupal\media\MediaInterface + * The called media entity. + */ + public function setName($name); + + /** * Returns the media item creation timestamp. * * @todo Remove and use the new interface when #2833378 is done. diff --git a/core/modules/media/tests/src/Functional/MediaRevisionTest.php b/core/modules/media/tests/src/Functional/MediaRevisionTest.php index 9ce5f9e..b8ff7c5 100644 --- a/core/modules/media/tests/src/Functional/MediaRevisionTest.php +++ b/core/modules/media/tests/src/Functional/MediaRevisionTest.php @@ -65,7 +65,7 @@ public function testRevisions() { $assert->statusCodeEquals(200); // Confirm the revision page shows the correct title. - $assert->pageTextContains($media->label()); + $assert->pageTextContains($media->getName()); // Confirm that the last revision is the default revision. $this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.'); @@ -168,7 +168,7 @@ public function testImageMediaRevision() { * A media object with up to date revision information. */ protected function createMediaRevision(MediaInterface $media) { - $media->set('name', $this->randomMachineName()); + $media->setName($this->randomMachineName()); $media->setNewRevision(); $media->save(); return $media; diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php index 4caab94..74ac67c 100644 --- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -67,7 +67,7 @@ public function testMediaWithOnlyOneMediaType() { ->getStorage('media') ->loadUnchanged($media_id); $this->assertEquals($media->getRevisionLogMessage(), $revision_log_message); - $this->assertEquals($media->label(), $media_name); + $this->assertEquals($media->getName(), $media_name); $assert_session->titleEquals($media_name . ' | Drupal'); // Tests media edit form. @@ -83,7 +83,7 @@ public function testMediaWithOnlyOneMediaType() { $media = $this->container->get('entity_type.manager') ->getStorage('media') ->loadUnchanged($media_id); - $this->assertEquals($media->label(), $media_name2); + $this->assertEquals($media->getName(), $media_name2); $assert_session->titleEquals($media_name2 . ' | Drupal'); // Test that there is no empty vertical tabs element, if the container is @@ -171,12 +171,12 @@ public function testMediaWithMultipleMediaTypes() { // Go to first media item. $this->drupalGet('media/' . $first_media_item->id()); $assert_session->statusCodeEquals(200); - $assert_session->pageTextContains($first_media_item->label()); + $assert_session->pageTextContains($first_media_item->getName()); // Go to second media item. $this->drupalGet('media/' . $second_media_item->id()); $assert_session->statusCodeEquals(200); - $assert_session->pageTextContains($second_media_item->label()); + $assert_session->pageTextContains($second_media_item->getName()); } } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php index 6cc7936..e32627d 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php @@ -57,7 +57,7 @@ public function testMediaFileSource() { // Load the media and check if the label was properly populated. $media = Media::load(1); - $this->assertEquals($test_filename, $media->label()); + $this->assertEquals($test_filename, $media->getName()); // Test the MIME type icon. $icon_base = \Drupal::config('media.settings')->get('icon_base_uri'); diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php index 33fe10d..dd99442 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php @@ -72,7 +72,7 @@ public function testMediaImageSource() { // Load the media and check that all fields are properly populated. $media = Media::load(1); - $this->assertEquals('example_1.jpeg', $media->label()); + $this->assertEquals('example_1.jpeg', $media->getName()); $this->assertEquals('200', $media->get('field_string_width')->value); $this->assertEquals('89', $media->get('field_string_height')->value); } diff --git a/core/modules/media/tests/src/Kernel/MediaCreationTest.php b/core/modules/media/tests/src/Kernel/MediaCreationTest.php index 1234703..be8f4f7 100644 --- a/core/modules/media/tests/src/Kernel/MediaCreationTest.php +++ b/core/modules/media/tests/src/Kernel/MediaCreationTest.php @@ -49,8 +49,8 @@ public function testMediaEntityCreation() { $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.'); $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.'); - $this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.'); - $this->assertEquals('Unnamed', $media->label(), 'The media item was not created with the correct name.'); + $this->assertEquals($this->testMediaType->id(), $media->getType(), 'The media item was not created with the correct type.'); + $this->assertEquals('Unnamed', $media->getName(), 'The media item was not created with the correct name.'); $source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName(); $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.'); } diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php index 90f6f2b..5b6f20a 100644 --- a/core/modules/media/tests/src/Kernel/MediaSourceTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php @@ -24,9 +24,12 @@ public function testDefaultName() { $media = Media::create(['bundle' => $this->testMediaType->id()]); $media_source = $media->getSource(); $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.'); - $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); + $this->assertEquals('media:' . $media->getType() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); + $this->assertEquals('media:' . $media->getType() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().'); + $this->assertEquals($media->getName(), $media->label(), 'Default name and label are not the same.'); $media->save(); - $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->label(), 'Default name was not set correctly.'); + $this->assertEquals('media:' . $media->getType() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.'); + $this->assertEquals($media->getName(), $media->label(), 'The label changed during save.'); // Make sure that the user-supplied name is used. /** @var \Drupal\media\MediaInterface $media */ @@ -37,9 +40,10 @@ public function testDefaultName() { ]); $media_source = $media->getSource(); $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.'); - $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); + $this->assertEquals('media:' . $media->getType() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); $media->save(); - $this->assertEquals($name, $media->label(), 'User-supplied name was not set correctly.'); + $this->assertEquals($name, $media->getName(), 'User-supplied name was not set correctly.'); + $this->assertEquals($media->getName(), $media->label(), 'The user-supplied name does not match the label.'); // Change the default name attribute and see if it is used to set the name. $name = 'Old Major'; @@ -51,7 +55,8 @@ public function testDefaultName() { $this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.'); $this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.'); $media->save(); - $this->assertEquals($name, $media->label(), 'Default name was not set correctly.'); + $this->assertEquals($name, $media->getName(), 'Default name was not set correctly.'); + $this->assertEquals($media->getName(), $media->label(), 'The default name does not match the label.'); } /** @@ -263,7 +268,7 @@ public function testThumbnail() { 'field_media_test' => 'some_value', ]); $media->save(); - $this->assertEquals('Boxer', $media->label(), 'Correct name was not set on the media entity.'); + $this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media entity.'); $this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); $this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); } @@ -293,7 +298,7 @@ public function testConstraints() { $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.'); // Fix the violation and make sure it is not reported anymore. - $media->set('name', 'I love Drupal!'); + $media->setName('I love Drupal!'); $violations = $media->validate(); $this->assertCount(0, $violations, 'Expected number of validations not found.'); @@ -301,6 +306,7 @@ public function testConstraints() { $this->assertEmpty($media->id(), 'Entity ID was found.'); $media->save(); $this->assertNotEmpty($media->id(), 'Entity ID was not found.'); + $this->assertSame($media->getName(), 'I love Drupal!'); // Test source field constraints. \Drupal::state()->set('media_source_test_field_constraints', [