core/modules/media/src/Entity/Media.php | 6 +-- core/modules/media/src/MediaStorageProxyBase.php | 2 +- .../media/src/MediaStorageProxyInterface.php | 25 --------- .../MediaStorageProxyMetadataHandlerInterface.php | 63 ++++++++++++++++++++++ core/modules/media/src/MediaTypeForm.php | 10 ++-- .../src/Plugin/MediaStorageProxy/Test.php | 12 ++--- .../FunctionalJavascript/MediaUiJavascriptTest.php | 12 ++--- 7 files changed, 82 insertions(+), 48 deletions(-) diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index dbf79d0..12b6900 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -139,11 +139,11 @@ public function preSave(EntityStorageInterface $storage) { // Try to set fields provided by the media handler and mapped in // media type config. - foreach ($this->bundle->entity->getFieldMap() as $source_field => $destination_field) { + 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($destination_field) && $this->get($destination_field)->isEmpty() && ($value = $this->getHandler()->getField($this, $source_field))) { - $this->set($destination_field, $value); + if ($this->hasField($entity_field_name) && $this->get($entity_field_name)->isEmpty() && ($value = $this->getHandler()->getMetadata($this, $metadata_attribute_name))) { + $this->set($entity_field_name, $value); } } diff --git a/core/modules/media/src/MediaStorageProxyBase.php b/core/modules/media/src/MediaStorageProxyBase.php index 1b92e8f..b3a277b 100644 --- a/core/modules/media/src/MediaStorageProxyBase.php +++ b/core/modules/media/src/MediaStorageProxyBase.php @@ -15,7 +15,7 @@ /** * Base implementation of media storage proxy plugin. */ -abstract class MediaStorageProxyBase extends PluginBase implements SourceFieldInterface, ContainerFactoryPluginInterface { +abstract class MediaStorageProxyBase extends PluginBase implements SourceFieldInterface, MediaStorageProxyMetadataHandlerInterface, ContainerFactoryPluginInterface { /** * Plugin label. diff --git a/core/modules/media/src/MediaStorageProxyInterface.php b/core/modules/media/src/MediaStorageProxyInterface.php index 3685cba..88d44c0 100644 --- a/core/modules/media/src/MediaStorageProxyInterface.php +++ b/core/modules/media/src/MediaStorageProxyInterface.php @@ -62,31 +62,6 @@ interface MediaStorageProxyInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { /** - * Gets list of fields provided by this plugin. - * - * @return array - * Associative array with field names as keys and arrays as values. Each - * of those arrays should have the following values: - * - label: human-readable label of the field - * - field_type: (optional) entity field type that the field should be - * mapped to by default. "string" will be assumed if omitted. - */ - public function getProvidedFields(); - - /** - * Gets a media-related field/value. - * - * @param \Drupal\media\MediaInterface $media - * A media item. - * @param string $name - * Name of field to fetch. - * - * @return mixed|false - * Field value or FALSE if data unavailable. - */ - public function getField(MediaInterface $media, $name); - - /** * Attaches media handler-specific validation constraints to a media item. * * @param \Drupal\media\MediaInterface $media diff --git a/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php b/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php new file mode 100644 index 0000000..0ab5745 --- /dev/null +++ b/core/modules/media/src/MediaStorageProxyMetadataHandlerInterface.php @@ -0,0 +1,63 @@ +getProvidedFields())) { + if (empty($handler) || empty($handler->getMetadataLabels())) { $form['handler_dependent']['field_map']['#access'] = FALSE; } else { @@ -163,12 +163,12 @@ public function form(array $form, FormStateInterface $form_state) { } $field_map = $this->entity->getFieldMap(); - foreach ($handler->getProvidedFields() as $field_name => $field_definition) { - $form['handler_dependent']['field_map'][$field_name] = [ + foreach ($handler->getMetadataLabels() as $metadata_attribute_name => $metadata_attribute_label) { + $form['handler_dependent']['field_map'][$metadata_attribute_name] = [ '#type' => 'select', - '#title' => $field_definition['label'], + '#title' => $metadata_attribute_label, '#options' => $options, - '#default_value' => isset($field_map[$field_name]) ? $field_map[$field_name] : '_none', + '#default_value' => isset($field_map[$metadata_attribute_name]) ? $field_map[$metadata_attribute_name] : '_none', ]; } } diff --git a/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php index 5756a3b..b70bf2c 100644 --- a/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php +++ b/core/modules/media/tests/modules/media_test_handler/src/Plugin/MediaStorageProxy/Test.php @@ -28,21 +28,17 @@ public function getThumbnail(MediaInterface $media) { /** * {@inheritdoc} */ - public function getProvidedFields() { + public function getMetadataLabels() { return [ - 'field_1' => [ - 'label' => $this->t('Field 1'), - ], - 'field_2' => [ - 'label' => $this->t('Field 2'), - ], + 'attribute_1' => $this->t('Attribute 1'), + 'attribute_2' => $this->t('Attribute 2'), ]; } /** * {@inheritdoc} */ - public function getField(MediaInterface $media, $name) { + public function getMetadata(MediaInterface $media, $attribute_name) { return FALSE; } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php index a153d50..a2bcb23 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php @@ -99,8 +99,8 @@ public function testMediaTypes() { $assert_session->assertWaitOnAjaxRequest(); $assert_session->fieldExists('Test config value'); $assert_session->fieldValueEquals('Test config value', 'This is default value.'); - $assert_session->fieldExists('Field 1'); - $assert_session->fieldExists('Field 2'); + $assert_session->fieldExists('Attribute 1'); + $assert_session->fieldExists('Attribute 2'); // Test if the edit machine name is not editable. $assert_session->fieldDisabled('Machine-readable name'); @@ -112,7 +112,7 @@ public function testMediaTypes() { $page->fillField('description', $new_description); $page->selectFieldOption('handler', 'test'); $page->fillField('Test config value', 'This is new config value.'); - $page->selectFieldOption('field_map[field_1]', 'name'); + $page->selectFieldOption('field_map[attribute_1]', 'name'); $page->checkField('options[new_revision]'); $page->uncheckField('options[status]'); $page->checkField('options[queue_thumbnail_downloads]'); @@ -128,8 +128,8 @@ public function testMediaTypes() { $assert_session->checkboxNotChecked('options[status]'); $assert_session->checkboxChecked('options[queue_thumbnail_downloads]'); $assert_session->fieldValueEquals('Test config value', 'This is new config value.'); - $assert_session->fieldValueEquals('Field 1', 'name'); - $assert_session->fieldValueEquals('Field 2', '_none'); + $assert_session->fieldValueEquals('Attribute 1', 'name'); + $assert_session->fieldValueEquals('Attribute 2', '_none'); /** @var \Drupal\media\MediaTypeInterface $loaded_media_type */ $loaded_media_type = $this->container->get('entity_type.manager') @@ -143,7 +143,7 @@ public function testMediaTypes() { $this->assertTrue($loaded_media_type->shouldCreateNewRevision()); $this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued()); $this->assertFalse($loaded_media_type->getStatus()); - $this->assertEquals($loaded_media_type->getFieldMap(), ['field_1' => 'name']); + $this->assertEquals($loaded_media_type->getFieldMap(), ['attribute_1' => 'name']); // We need to clear the statically cached field definitions to account for // fields that have been created by API calls in this test, since they exist