diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php index dea0140260..6071441f17 100644 --- a/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -317,4 +317,11 @@ protected function getSourceFieldName() { return $id; } + /** + * {@inheritdoc} + */ + public function getDisplayOptions($display_context) { + return []; + } + } diff --git a/core/modules/media/src/MediaSourceInterface.php b/core/modules/media/src/MediaSourceInterface.php index 723e0b9a8e..56ab10574f 100644 --- a/core/modules/media/src/MediaSourceInterface.php +++ b/core/modules/media/src/MediaSourceInterface.php @@ -139,4 +139,18 @@ public function getSourceFieldDefinition(MediaTypeInterface $type); */ public function createSourceField(MediaTypeInterface $type); + /** + * Get the options for the source field in the form/view display. + * + * @param string $display_context + * Possible parameters are 'view' or 'form'. + * + * @return array|bool + * The display options as array or FALSE for removing this component from + * the display. + * + * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() + */ + public function getDisplayOptions($display_context); + } diff --git a/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php index 5f301d58ed..9c657a2cc4 100644 --- a/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -328,29 +328,37 @@ public function save(array $form, FormStateInterface $form_state) { // Add the new field to the default form and view displays for this // media type. $field_name = $source_field->getName(); - $field_type = $source_field->getType(); - if ($source_field->isDisplayConfigurable('form')) { - // Use the default widget and settings. - $component = \Drupal::service('plugin.manager.field.widget') - ->prepareConfiguration($field_type, []); - + $display_options = $source->getDisplayOptions('form'); // @todo Replace entity_get_form_display() when #2367933 is done. // https://www.drupal.org/node/2872159. - entity_get_form_display('media', $media_type->id(), 'default') - ->setComponent($field_name, $component) - ->save(); + $display = entity_get_form_display('media', $media_type->id(), 'default'); + if ($display_options === FALSE) { + $display->removeComponent($field_name); + } + elseif (is_array($display_options)) { + $display->setComponent($field_name, $display_options); + } + else { + $display->setComponent($field_name); + } + $display->save(); } if ($source_field->isDisplayConfigurable('view')) { - // Use the default formatter and settings. - $component = \Drupal::service('plugin.manager.field.formatter') - ->prepareConfiguration($field_type, []); - + $display_options = $source->getDisplayOptions('view'); // @todo Replace entity_get_display() when #2367933 is done. // https://www.drupal.org/node/2872159. - entity_get_display('media', $media_type->id(), 'default') - ->setComponent($field_name, $component) - ->save(); + $display = entity_get_display('media', $media_type->id(), 'default'); + if ($display_options === FALSE) { + $display->removeComponent($field_name); + } + elseif (is_array($display_options)) { + $display->setComponent($field_name, $display_options); + } + else { + $display->setComponent($field_name); + } + $display->save(); } }