diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 49f7283..dc8e0dc 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -404,12 +404,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { 'weight' => -5, ]) ->setDisplayConfigurable('form', TRUE) - ->setDisplayConfigurable('view', TRUE) - ->setDisplayOptions('view', [ - 'label' => 'hidden', - 'type' => 'string', - 'weight' => -5, - ]); + ->setDisplayConfigurable('view', TRUE); $fields['thumbnail'] = BaseFieldDefinition::create('image') ->setLabel(t('Thumbnail')) diff --git a/core/modules/media/templates/media.html.twig b/core/modules/media/templates/media.html.twig index 4173199..32cdb0f 100644 --- a/core/modules/media/templates/media.html.twig +++ b/core/modules/media/templates/media.html.twig @@ -32,17 +32,6 @@ */ #} - {# - In the 'full' view mode the entity label is assumed to be displayed as the - page title, so we do not display it here. - #} - {{ title_prefix }} - {% if label and view_mode != 'full' %} - - {{ label }} - - {% endif %} {{ title_suffix }} - {{ content }} diff --git a/core/modules/media/tests/src/Functional/MediaContextualLinksTest.php b/core/modules/media/tests/src/Functional/MediaContextualLinksTest.php new file mode 100644 index 0000000..1d97773 --- /dev/null +++ b/core/modules/media/tests/src/Functional/MediaContextualLinksTest.php @@ -0,0 +1,45 @@ +createMediaType(); + + // Create a media item. + $media = Media::create([ + 'bundle' => $mediaType->id(), + 'name' => 'Unnamed', + ]); + $media->save(); + + $user = $this->drupalCreateUser([ + 'administer media', + 'access contextual links', + ]); + $this->drupalLogin($user); + + $this->drupalGet('media/' . $media->id()); + $this->assertSession()->elementAttributeContains('css', 'div[data-contextual-id]', 'data-contextual-id', 'media:media=' . $media->id() . ':'); + } + +} diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php new file mode 100644 index 0000000..5282270 --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php @@ -0,0 +1,172 @@ +container->get('config.installer')->installOptionalConfig($storage, ''); + // Reset all the static caches and list caches. + $this->container->get('config.factory')->reset(); + } + + /** + * Test basic media display. + */ + public function testMediaDisplay() { + $assert_session = $this->assertSession(); + $page = $this->getSession()->getPage(); + + $media_type = $this->createMediaType(); + + // Create a media item. + $media = Media::create([ + 'bundle' => $media_type->id(), + 'name' => 'Fantastic!', + ]); + $media->save(); + + $this->drupalGet('media/' . $media->id()); + // Verify the "name" field is really not present. + $assert_session->elementNotExists('css', '.field--name-name'); + + // Enable the field on the display and verify it becomes visible on the UI. + $this->drupalGet("/admin/structure/media/manage/{$media_type->id()}/display"); + $page->selectFieldOption('fields[name][region]', 'content'); + $assert_session->waitForElementVisible('css', '#edit-fields-name-settings-edit'); + $page->pressButton('Save'); + $this->drupalGet('media/' . $media->id()); + // Verify the name is present, and its text matches what is expected. + $assert_session->elementExists('css', '.field--name-name'); + $name_field = $page->find('css', '.field--name-name .field__item'); + $this->assertEquals($media->label(), $name_field->getText()); + + // In the standard profile, there are some pre-cooked types. Make sure the + // elements configured on their displays are the expected ones. + $this->drupalGet('media/add/image'); + $image_media_name = 'Fantastic image asset!'; + $page->fillField('name[0][value]', $image_media_name); + $page->attachFileToField('files[field_media_image_0]', \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg'); + $result = $assert_session->waitForButton('Remove'); + $this->assertNotEmpty($result); + $page->fillField('field_media_image[0][alt]', 'Image Alt Text 1'); + $page->pressButton('Save'); + $image_media_id = $this->container->get('entity.query')->get('media') + ->sort('mid', 'DESC') + ->execute(); + $image_media_id = reset($image_media_id); + + // Here we expect to see only the image, nothing else. + // 1) Assert only one element in the content region. + $this->assertEquals(1, count($page->findAll('css', '.media--type-image > div'))); + // 2) Assert the image is present inside the media element, with "medium" + // image style. + $media_item = $assert_session->elementExists('css', '.media--type-image > div'); + $assert_session->elementExists('css', 'img.image-style-medium', $media_item); + + $test_filename = $this->randomMachineName() . '.txt'; + $test_filepath = 'public://' . $test_filename; + file_put_contents($test_filepath, $this->randomMachineName()); + $this->drupalGet("media/add/file"); + $page->fillField('name[0][value]', 'File media 1'); + $page->attachFileToField("files[field_media_file_0]", \Drupal::service('file_system')->realpath($test_filepath)); + $result = $assert_session->waitForButton('Remove'); + $this->assertNotEmpty($result); + $page->pressButton('Save'); + + // Here we expect to see only the linked filename. + // 1) Assert only one element in the content region. + $this->assertEquals(1, count($page->findAll('css', 'article.media--type-file > div'))); + // 2) Assert the file link is present, and its text matches the filename. + $assert_session->elementExists('css', 'article.media--type-file .field--name-field-media-file a'); + $link = $page->find('css', 'article.media--type-file .field--name-field-media-file a'); + $this->assertEquals($test_filename, $link->getText()); + + // Create a node type "page" to use as host entity. + $node_type = NodeType::create([ + 'type' => 'page', + 'name' => 'Page', + ]); + $node_type->save(); + + // Reference the created media using an entity_refernce field and make sure + // the output is what we expect. + $storage = FieldStorageConfig::create([ + 'entity_type' => 'node', + 'field_name' => 'field_related_media', + 'type' => 'entity_reference', + 'settings' => [ + 'target_type' => 'media', + ], + ]); + $storage->save(); + + FieldConfig::create([ + 'field_storage' => $storage, + 'entity_type' => 'node', + 'bundle' => $node_type->id(), + 'label' => 'Related media', + 'settings' => [ + 'handler_settings' => [ + 'target_bundles' => [ + 'image' => 'image', + ], + ], + ], + ])->save(); + + entity_get_display('node', $node_type->id(), 'default') + ->setComponent('field_related_media', [ + 'type' => 'entity_reference_entity_view', + 'label' => 'hidden', + 'settings' => [ + 'view_mode' => 'full', + ], + ])->save(); + + $node = Node::create([ + 'title' => 'Host node', + 'type' => $node_type->id(), + 'field_related_media' => [ + 'target_id' => $image_media_id, + ], + ]); + $node->save(); + + $this->drupalGet('/node/' . $node->id()); + // Media field is there. + $assert_session->elementExists('css', '.field--name-field-related-media'); + // Media name element is not there. + $assert_session->elementNotExists('css', '.field--name-name'); + $assert_session->pageTextNotContains($image_media_name); + // Only one element is present inside the media container. + $this->assertEquals(1, count($page->findAll('css', '.field--name-field-related-media article.media--type-image > div'))); + // Assert the image is present, with "medium" image style. + $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image img.image-style-medium'); + } + +} diff --git a/core/modules/media/tests/src/Kernel/MediaTest.php b/core/modules/media/tests/src/Kernel/MediaTest.php index d246fb6..df48a9b 100644 --- a/core/modules/media/tests/src/Kernel/MediaTest.php +++ b/core/modules/media/tests/src/Kernel/MediaTest.php @@ -21,14 +21,17 @@ public function testEntity() { } /** - * Ensure media name is configurable on manage display. + * Tests the Media "name" base field behavior. */ - public function testNameIsConfigurable() { + public function testNameBaseField() { /** @var \Drupal\Core\Field\BaseFieldDefinition[] $field_definitions */ $field_definitions = $this->container->get('entity_field.manager') ->getBaseFieldDefinitions('media'); + // Ensure media name is configurable on manage display. $this->assertTrue($field_definitions['name']->isDisplayConfigurable('view')); + // Ensure it is not visible by default. + $this->assertEquals($field_definitions['name']->getDisplayOptions('view'), ['region' => 'hidden']); } } diff --git a/core/profiles/standard/config/optional/core.entity_view_display.media.file.default.yml b/core/profiles/standard/config/optional/core.entity_view_display.media.file.default.yml index f42bab7..72b0bdc 100644 --- a/core/profiles/standard/config/optional/core.entity_view_display.media.file.default.yml +++ b/core/profiles/standard/config/optional/core.entity_view_display.media.file.default.yml @@ -18,15 +18,8 @@ content: type: file_default weight: 1 region: content - name: - label: hidden - type: string - weight: 0 - region: content - settings: - link_to_entity: false - third_party_settings: { } hidden: created: true thumbnail: true uid: true + name: true diff --git a/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml b/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml index c02d908..eee9348 100644 --- a/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml +++ b/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml @@ -21,15 +21,8 @@ content: type: image weight: 1 region: content - name: - label: hidden - type: string - weight: 0 - region: content - settings: - link_to_entity: false - third_party_settings: { } hidden: created: true thumbnail: true uid: true + name: true diff --git a/core/themes/classy/templates/content/media.html.twig b/core/themes/classy/templates/content/media.html.twig index f36975c..cb65943 100644 --- a/core/themes/classy/templates/content/media.html.twig +++ b/core/themes/classy/templates/content/media.html.twig @@ -21,6 +21,7 @@ ] %} + {{ title_suffix }} {% if content %} {{ content }} {% endif %} diff --git a/core/themes/stable/templates/content/media.html.twig b/core/themes/stable/templates/content/media.html.twig index 769b7be..58732de 100644 --- a/core/themes/stable/templates/content/media.html.twig +++ b/core/themes/stable/templates/content/media.html.twig @@ -13,6 +13,7 @@ */ #} + {{ title_suffix }} {% if content %} {{ content }} {% endif %}