diff --git a/media_entity.install b/media_entity.install index b0c1415..2aade08 100644 --- a/media_entity.install +++ b/media_entity.install @@ -101,3 +101,42 @@ function media_entity_update_8003() { throw new \Drupal\Core\Utility\UpdateException('Entity API >= 8.x-1.0-alpha3 (drupal.org/project/entity) module is now a dependency and needs to be installed before running updates.'); } } + +/** + * Keep existing bundle names visible in entity forms. + */ +function media_entity_update_8004() { + $entity_type_manager = \Drupal::entityTypeManager(); + $bundles = $entity_type_manager->getStorage('media_bundle')->getQuery()->execute(); + foreach ($bundles as $bundle_name) { + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $form_display = $entity_type_manager->getStorage('entity_form_display') + ->load('media.' . $bundle_name . '.default'); + if (empty($form_display)) { + $values = [ + 'targetEntityType' => 'media', + 'bundle' => $bundle_name, + 'mode' => 'default', + 'status' => TRUE, + ]; + $form_display = \Drupal::entityTypeManager() + ->getStorage('entity_form_display') + ->create($values); + $form_display->save(); + } + $name_settings = $form_display->getComponent('name'); + if (empty($name_settings)) { + // This bundle has no override for the 'name' fieldbase, so it would get + // hidden after #2813685. Create an override to keep it visible. + $form_display->setComponent('name', [ + 'type' => 'string_textfield', + 'weight' => -5, + 'label' => 'hidden', + 'settings' => [ + 'size' => 60, + 'placeholder' => '', + ], + ])->save(); + } + } +} diff --git a/src/Entity/Media.php b/src/Entity/Media.php index a73200a..b59d0f5 100644 --- a/src/Entity/Media.php +++ b/src/Entity/Media.php @@ -303,15 +303,10 @@ class Media extends ContentEntityBase implements MediaInterface { $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Media name')) ->setDescription(t('The name of this media.')) - ->setRequired(TRUE) ->setTranslatable(TRUE) ->setRevisionable(TRUE) ->setDefaultValue('') ->setSetting('max_length', 255) - ->setDisplayOptions('form', [ - 'type' => 'string_textfield', - 'weight' => -5, - ]) ->setDisplayConfigurable('form', TRUE) ->setDisplayOptions('view', [ 'label' => 'hidden', diff --git a/src/Tests/MediaUITest.php b/src/Tests/MediaUITest.php index 5bfcceb..376f0ee 100644 --- a/src/Tests/MediaUITest.php +++ b/src/Tests/MediaUITest.php @@ -233,11 +233,9 @@ class MediaUITest extends WebTestBase { // Tests media item add form. $edit = [ - 'name[0][value]' => $this->randomMachineName(), 'revision_log' => $this->randomString(), ]; $this->drupalPostForm('media/add', $edit, t('Save and publish')); - $this->assertTitle($edit['name[0][value]'] . ' | Drupal'); $media_id = $this->container->get('entity.query')->get('media')->execute(); $media_id = reset($media_id); /** @var \Drupal\media_entity\MediaInterface $media */ @@ -245,13 +243,22 @@ class MediaUITest extends WebTestBase { ->getStorage('media') ->loadUnchanged($media_id); $this->assertEqual($media->getRevisionLogMessage(), $edit['revision_log'], 'Revision log was saved.'); + $this->assertTitle($media->label() . ' | Drupal'); // Test if the media list contains exactly 1 media bundle. $this->drupalGet('admin/content/media'); $this->assertResponse(200); - $this->assertText($edit['name[0][value]']); + $this->assertText($media->label()); + + // Make visible the "name" field on the entity form. + $edit = [ + 'fields[name][weight]' => -5, + 'fields[name][type]' => 'string_textfield', + ]; + $this->drupalPostForm('admin/structure/media/manage/' . $bundle->id() . '/form-display', $edit, t('Save')); // Tests media edit form. + $edit = []; $this->drupalGet('media/' . $media_id . '/edit'); $this->assertNoFieldChecked('edit-revision', 'New revisions are disabled by default.'); $edit['name[0][value]'] = $this->randomMachineName(); @@ -449,16 +456,16 @@ class MediaUITest extends WebTestBase { * Returns the */ public function createMediaItem($media_bundle) { - // Define the media item name. - $name = $this->randomMachineName(); - $edit = [ - 'name[0][value]' => $name, - ]; - // Save it and retrieve new media item ID, then return all information. - $this->drupalPostForm('media/add/' . $media_bundle['id'], $edit, t('Save and publish')); - $this->assertTitle($edit['name[0][value]'] . ' | Drupal'); - $media_id = \Drupal::entityQuery('media')->execute(); + // Create media and retrieve new media item ID, then return all information. + $this->drupalPostForm('media/add/' . $media_bundle['id'], [], t('Save and publish')); + $media_id = \Drupal::entityQuery('media')->condition('bundle', $media_bundle['id'])->execute(); $media_id = reset($media_id); + /** @var \Drupal\media_entity\MediaInterface $media */ + $media = $this->container->get('entity_type.manager') + ->getStorage('media') + ->loadUnchanged($media_id); + $edit['label'] = $media->label(); + $this->assertTitle($edit['label'] . ' | Drupal'); $edit['id'] = $media_id; return $edit; @@ -481,30 +488,30 @@ class MediaUITest extends WebTestBase { $this->assertLink('Add media'); // Assert that all available media items are in the list. - $this->assertText($first_media_item['name[0][value]']); + $this->assertText($first_media_item['label']); $this->assertText($first_media_bundle['label']); - $this->assertText($second_media_item['name[0][value]']); + $this->assertText($second_media_item['label']); $this->assertText($second_media_bundle['label']); // Filter for each bundle and assert that the list has been updated. $this->drupalGet('admin/content/media', ['query' => ['provider' => $first_media_bundle['id']]]); $this->assertResponse(200); - $this->assertText($first_media_item['name[0][value]']); + $this->assertText($first_media_item['label']); $this->assertText($first_media_bundle['label']); - $this->assertNoText($second_media_item['name[0][value]']); + $this->assertNoText($second_media_item['label']); $this->drupalGet('admin/content/media', ['query' => ['provider' => $second_media_bundle['id']]]); $this->assertResponse(200); - $this->assertNoText($first_media_item['name[0][value]']); - $this->assertText($second_media_item['name[0][value]']); + $this->assertNoText($first_media_item['label']); + $this->assertText($second_media_item['label']); $this->assertText($second_media_bundle['label']); // Filter all and check for all items again. $this->drupalGet('admin/content/media', ['query' => ['provider' => 'All']]); $this->assertResponse(200); - $this->assertText($first_media_item['name[0][value]']); + $this->assertText($first_media_item['label']); $this->assertText($first_media_bundle['label']); - $this->assertText($second_media_item['name[0][value]']); + $this->assertText($second_media_item['label']); $this->assertText($second_media_bundle['label']); }