diff --git a/core/composer.json b/core/composer.json index 4662c1f..8f69344 100644 --- a/core/composer.json +++ b/core/composer.json @@ -109,7 +109,7 @@ "drupal/link": "self.version", "drupal/locale": "self.version", "drupal/minimal": "self.version", - "drupal/media_entity": "self.version", + "drupal/media": "self.version", "drupal/menu_link_content": "self.version", "drupal/menu_ui": "self.version", "drupal/migrate": "self.version", diff --git a/core/modules/media/js/media_bundle_form.js b/core/modules/media/js/media_type_form.js similarity index 87% rename from core/modules/media/js/media_bundle_form.js rename to core/modules/media/js/media_type_form.js index 7db10c5..b8fbddf 100644 --- a/core/modules/media/js/media_bundle_form.js +++ b/core/modules/media/js/media_type_form.js @@ -1,18 +1,18 @@ /** * @file - * Javascript for the media bundle form. + * Javascript for the media type form. */ (function ($, Drupal) { 'use strict'; /** - * Behaviors for setting summaries on media bundle form. + * Behaviors for setting summaries on media type form. * * @type {Drupal~behavior} * * @prop {Drupal~behaviorAttach} attach - * Attaches summary behaviors on media bundle edit forms. + * Attaches summary behaviors on media type edit forms. */ Drupal.behaviors.mediaBundles = { attach: function (context) { diff --git a/core/modules/media/media.libraries.yml b/core/modules/media/media.libraries.yml index cd280bc..ef286b1 100644 --- a/core/modules/media/media.libraries.yml +++ b/core/modules/media/media.libraries.yml @@ -5,9 +5,9 @@ media_form: dependencies: - core/drupal.form -media_bundle_form: +media_type_form: version: VERSION js: - 'js/media_bundle_form.js': {} + 'js/media_type_form.js': {} dependencies: - core/drupal.form diff --git a/core/modules/media/media.module b/core/modules/media/media.module index db53d81..78e2810 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -71,14 +71,14 @@ function media_theme_suggestions_media(array $variables) { */ function media_copy_icons($source, $destination) { if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { - throw new Exception("Unable to create directory $destination."); + throw new RuntimeException("Unable to create directory $destination."); } $files = file_scan_directory($source, '/.*\.(png|jpg)$/'); foreach ($files as $file) { $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE); if (!$result) { - throw new Exception("Unable to copy {$file->uri} to $destination."); + throw new RuntimeException("Unable to copy {$file->uri} to $destination."); } } } diff --git a/core/modules/media/media.permissions.yml b/core/modules/media/media.permissions.yml index 9ea3298..0d0a7a9 100644 --- a/core/modules/media/media.permissions.yml +++ b/core/modules/media/media.permissions.yml @@ -14,5 +14,6 @@ delete media: title: 'Delete own media' delete any media: title: 'Delete any media' + restrict access: TRUE create media: title: 'Create media' diff --git a/core/modules/media/media.tokens.inc b/core/modules/media/media.tokens.inc index e5a3461..afb787a 100644 --- a/core/modules/media/media.tokens.inc +++ b/core/modules/media/media.tokens.inc @@ -126,7 +126,7 @@ function media_tokens($type, $tokens, array $data, array $options, BubbleableMet break; case 'name': - $replacements[$original] = $media->name->value; + $replacements[$original] = $media->label(); break; case 'url': diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 04bd8bc..73a3ee7 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -234,8 +234,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields += static::publishedBaseFieldDefinitions($entity_type); $fields['name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Media name')) - ->setDescription(t('The name of this media.')) + ->setLabel(t('Name')) ->setRequired(TRUE) ->setTranslatable(TRUE) ->setRevisionable(TRUE) @@ -268,10 +267,10 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setReadOnly(TRUE); $fields['uid'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Publisher ID')) - ->setDescription(t('The user ID of the author of the media item.')) + ->setLabel(t('Authored by')) + ->setDescription(t('The username of the media publisher.')) ->setRevisionable(TRUE) - ->setDefaultValueCallback('Drupal\media\Entity\Media::getCurrentUserId') + ->setDefaultValueCallback(static::class . '::getCurrentUserId') ->setSetting('target_type', 'user') ->setTranslatable(TRUE) ->setDisplayOptions('form', [ @@ -293,7 +292,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDisplayConfigurable('view', TRUE); $fields['created'] = BaseFieldDefinition::create('created') - ->setLabel(t('Created')) + ->setLabel(t('Authored on')) ->setDescription(t('The time the media item was created.')) ->setTranslatable(TRUE) ->setRevisionable(TRUE) diff --git a/core/modules/media/src/Entity/MediaType.php b/core/modules/media/src/Entity/MediaType.php index 61cd8e6..7b1f0a8 100644 --- a/core/modules/media/src/Entity/MediaType.php +++ b/core/modules/media/src/Entity/MediaType.php @@ -27,7 +27,7 @@ * "form" = { * "add" = "Drupal\media\MediaTypeForm", * "edit" = "Drupal\media\MediaTypeForm", - * "delete" = "Drupal\media\Form\MediaTypeDeleteConfirm" + * "delete" = "Drupal\media\Form\MediaTypeDeleteConfirmForm" * }, * "list_builder" = "Drupal\media\MediaTypeListBuilder", * "route_provider" = { @@ -124,7 +124,7 @@ class MediaType extends ConfigEntityBundleBase implements MediaTypeInterface, En * * @var array */ - public $field_map = []; + protected $field_map = []; /** * {@inheritdoc} diff --git a/core/modules/media/src/Form/MediaTypeDeleteConfirm.php b/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php similarity index 66% rename from core/modules/media/src/Form/MediaTypeDeleteConfirm.php rename to core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php index c5793b2..5bf9d63 100644 --- a/core/modules/media/src/Form/MediaTypeDeleteConfirm.php +++ b/core/modules/media/src/Form/MediaTypeDeleteConfirmForm.php @@ -10,7 +10,7 @@ /** * Provides a form for media type deletion. */ -class MediaTypeDeleteConfirm extends EntityDeleteForm { +class MediaTypeDeleteConfirmForm extends EntityDeleteForm { /** * The query factory to create entity queries. @@ -47,12 +47,18 @@ public function buildForm(array $form, FormStateInterface $form_state) { ->count() ->execute(); if ($num_entities) { - $caption = '

' . $this->formatPlural($num_entities, - '%type is used by @count piece of content on your site. You can not remove this content type until you have removed all of the %type content.', - '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', - ['%type' => $this->entity->label()]) . '

'; $form['#title'] = $this->getQuestion(); - $form['description'] = ['#markup' => $caption]; + $form['description'] = [ + '#type' => 'inline_template', + '#template' => '

{{ message }}

', + '#context' => [ + 'message' => $this->formatPlural($num_entities, + '%type is used by @count piece of content on your site. You can not remove this content type until you have removed all of the %type content.', + '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', + ['%type' => $this->entity->label()]), + ], + ]; + return $form; } diff --git a/core/modules/media/src/MediaForm.php b/core/modules/media/src/MediaForm.php index 371f1a6..4019c0f 100644 --- a/core/modules/media/src/MediaForm.php +++ b/core/modules/media/src/MediaForm.php @@ -168,7 +168,7 @@ public function save(array $form, FormStateInterface $form_state) { $insert = $this->entity->isNew(); $this->entity->save(); $context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label()]; - $logger = $this->logger($this->entity->id()); + $logger = $this->logger('media'); $t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->label()]; if ($insert) { diff --git a/core/modules/media/src/MediaHandlerBase.php b/core/modules/media/src/MediaHandlerBase.php index f90fa51..d8a1f5d 100644 --- a/core/modules/media/src/MediaHandlerBase.php +++ b/core/modules/media/src/MediaHandlerBase.php @@ -271,7 +271,7 @@ protected function getSourceFieldName() { public function mapFieldValue(MediaInterface $media, $source_field, $destination_field) { // Only save value in entity field if empty. Do not overwrite existing // data. - if ($media->hasField($destination_field) && $media->{$destination_field}->isEmpty() && ($value = $this->getField($media, $source_field))) { + if ($media->hasField($destination_field) && $media->get($destination_field)->isEmpty() && ($value = $this->getField($media, $source_field))) { $media->set($destination_field, $value); } } diff --git a/core/modules/media/src/MediaHandlerInterface.php b/core/modules/media/src/MediaHandlerInterface.php index e1766e5..df4c4f2 100644 --- a/core/modules/media/src/MediaHandlerInterface.php +++ b/core/modules/media/src/MediaHandlerInterface.php @@ -39,7 +39,7 @@ public function providedFields(); * @param string $name * Name of field to fetch. * - * @return mixed + * @return mixed|false * Field value or FALSE if data unavailable. */ public function getField(MediaInterface $media, $name); diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index f348a27..c84e9d8 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -8,7 +8,9 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Form\FormStateInterface; +use Drupal\field\Entity\FieldConfig; use Drupal\media\Entity\MediaType; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -96,12 +98,11 @@ public function form(array $form, FormStateInterface $form_state) { $form_state->set('bundle', $bundle->id()); /** @var \Drupal\media\MediaHandlerInterface $handler */ - try { - // Handler is not set when entity is initially created. + $handler = NULL; + if ($bundle->get('handler')) { + // Handler is not set when the entity is initially created. $handler = $bundle->getHandler(); - } - catch (PluginNotFoundException $e) { - $handler = NULL; + $this->configurableInstances[$handler]['plugin_config'] = empty($configuration[$handler]) ? [] : $configuration[$handler]; } if ($this->operation == 'add') { @@ -201,27 +202,17 @@ public function form(array $form, FormStateInterface $form_state) { $form['handler_dependent']['field_mapping']['#access'] = FALSE; } else { - $skipped_fields = [ - 'mid', - 'uuid', - 'vid', - 'bundle', - 'langcode', - 'default_langcode', - 'uid', - 'revision_timestamp', - 'revision_log', - 'revision_uid', - ]; $options = ['_none' => $this->t('- Skip field -')]; foreach ($this->entityFieldManager->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { - if (!in_array($field_name, $skipped_fields)) { + if (!($field instanceof BaseFieldDefinition) || $field_name == 'name') { $options[$field_name] = $field->getLabel(); } } $field_map = $bundle->getFieldMap(); foreach ($handler->providedFields() as $field_name => $field_definition) { + // This is a BC layer. In the past this function returned string and now + // it returns arrays. if (!is_array($field_definition)) { $field_definition = ['label' => $field_definition]; } @@ -238,7 +229,7 @@ public function form(array $form, FormStateInterface $form_state) { $form['additional_settings'] = [ '#type' => 'vertical_tabs', '#attached' => [ - 'library' => ['media/media_bundle_form'], + 'library' => ['media/media_type_form'], ], '#weight' => 100, ]; @@ -347,12 +338,12 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, $configuration = $form_state->getValue('handler_configuration'); // Store previous plugin config. - try { + $plugin = NULL; + if ($entity->get('handler')) { // Handler is not set when the entity is initially created. $plugin = $entity->getHandler()->getPluginId(); $this->configurableInstances[$plugin]['plugin_config'] = empty($configuration[$plugin]) ? [] : $configuration[$plugin]; } - catch (PluginNotFoundException $e) {} /** @var \Drupal\media\MediaInterface $entity */ parent::copyFormValuesToEntity($entity, $form, $form_state); diff --git a/core/modules/media/src/MediaTypeListBuilder.php b/core/modules/media/src/MediaTypeListBuilder.php index 90c6dce..c0c8490 100644 --- a/core/modules/media/src/MediaTypeListBuilder.php +++ b/core/modules/media/src/MediaTypeListBuilder.php @@ -33,7 +33,7 @@ public function buildRow(EntityInterface $entity) { 'data' => $entity->label(), 'class' => ['menu-label'], ]; - $row['description'] = Xss::filterAdmin($entity->getDescription()); + $row['description']['data'] = ['#markup' => Xss::filterAdmin($entity->getDescription())]; return $row + parent::buildRow($entity); } diff --git a/core/modules/media/tests/src/Kernel/BasicCreationTest.php b/core/modules/media/tests/src/Kernel/BasicCreationTest.php index 67aaf8c..01def63 100644 --- a/core/modules/media/tests/src/Kernel/BasicCreationTest.php +++ b/core/modules/media/tests/src/Kernel/BasicCreationTest.php @@ -72,11 +72,11 @@ public function testMediaBundleCreation() { $this->container->get('module_installer')->install(['media_test_type']); $test_bundle = $bundle_storage->load('test'); $this->assertTrue((bool) $test_bundle, 'The media type from default configuration has not been created in the database.'); - $this->assertEquals($test_bundle->get('label'), 'Test type', 'Could not assure the correct type name.'); - $this->assertEquals($test_bundle->get('description'), 'Test type.', 'Could not assure the correct type description.'); - $this->assertEquals($test_bundle->get('handler'), 'test', 'Could not assure the correct handler.'); - $this->assertEquals($test_bundle->get('handler_configuration'), ['source_field' => 'field_media_test_1', 'test_config_value' => 'Kakec'], 'Could not assure the correct handler configuration.'); - $this->assertEquals($test_bundle->get('field_map'), [], 'Could not assure the correct field map.'); + $this->assertEquals('Test type', $test_bundle->get('label'), 'Could not assure the correct type name.'); + $this->assertEquals('Test type.', $test_bundle->get('description'), 'Could not assure the correct type description.'); + $this->assertEquals('test', $test_bundle->get('handler'), 'Could not assure the correct handler.'); + $this->assertEquals(['source_field' => 'field_media_test_1', 'test_config_value' => 'Kakec'], $test_bundle->get('handler_configuration'), 'Could not assure the correct handler configuration.'); + $this->assertEquals([], $test_bundle->get('field_map'), 'Could not assure the correct field map.'); } /** @@ -94,8 +94,8 @@ public function testMediaEntityCreation() { $media_exists = (bool) Media::load($media->id()); $this->assertTrue($media_exists, 'The new media entity has not been created in the database.'); - $this->assertEquals($media->bundle(), $this->testBundle->id(), 'The media was not created with the correct type.'); - $this->assertEquals($media->label(), 'Unnamed', 'The media was not created with the correct name.'); + $this->assertEquals($this->testBundle->id(), $media->bundle(), 'The media was not created with the correct type.'); + $this->assertEquals('Unnamed', $media->label(), 'The media was not created with the correct name.'); // Test the creation of a media without user-defined label and check if a // default name is provided. @@ -104,8 +104,8 @@ public function testMediaEntityCreation() { ]); $media->save(); $expected_name = 'media' . ':' . $this->testBundle->id() . ':' . $media->uuid(); - $this->assertEquals($media->bundle(), $this->testBundle->id(), 'The media was not created with correct type.'); - $this->assertEquals($media->label(), $expected_name, 'The media was not created with a default name.'); + $this->assertEquals($this->testBundle->id(), $media->bundle(), 'The media was not created with correct type.'); + $this->assertEquals($expected_name, $media->label(), 'The media was not created with a default name.'); } /**