diff -u b/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php --- b/core/modules/media/src/MediaSourceBase.php +++ b/core/modules/media/src/MediaSourceBase.php @@ -3,6 +3,8 @@ namespace Drupal\media; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; @@ -320,8 +322,15 @@ /** * {@inheritdoc} */ - public function getDisplayOptions(MediaTypeInterface $type, $display_context, $display_mode = 'default') { - return []; + public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { + $display->setComponent($field_name); + } + + /** + * {@inheritdoc} + */ + public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { + $display->setComponent($field_name); } } diff -u b/core/modules/media/src/MediaSourceInterface.php b/core/modules/media/src/MediaSourceInterface.php --- b/core/modules/media/src/MediaSourceInterface.php +++ b/core/modules/media/src/MediaSourceInterface.php @@ -4,6 +4,8 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Component\Plugin\PluginInspectionInterface; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Plugin\PluginFormInterface; /** @@ -141,28 +143,42 @@ /** - * Gets the options for the source field in the form/view display. + * Prepares the source field in the view display. * - * The display options must have the same structure like the options for - * \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent(). - * Returning an empty array means that the default display options of the - * source field will be used. If the $display_context is 'view', then the - * returned array must be a set of formatter options (i.e, for an entity view - * display), and if the $display_context is 'form', the returned array must be - * a set of widget options (i.e., for an entity form display). + * This method should normally call + * \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or + * \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to + * configure the source field in the view display. * * @param \Drupal\media\MediaTypeInterface $type * The media type which is using this source. - * @param string $display_context - * The display context which will use these options. Can be 'view' or 'form. - * @param string $display_mode - * The display mode which will use these options. - * - * @return array|bool - * The display options, or FALSE for removing this component from - * the display. + * @param string $field_name + * Field name of the source field. + * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display + * The display in which the field appears. * * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() + * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() */ - public function getDisplayOptions(MediaTypeInterface $type, $display_context, $display_mode = 'default'); + public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display); + + /** + * Prepares the source field in the form display. + * + * This method should normally call + * \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or + * \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to + * configure the source field in the form display. + * + * @param \Drupal\media\MediaTypeInterface $type + * The media type which is using this source. + * @param string $field_name + * Field name of the source field. + * @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display + * The display in which the field appears. + * + * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() + * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() + */ + public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display); } diff -u b/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php --- b/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -333,14 +333,14 @@ // @todo Replace entity_get_form_display() when #2367933 is done. // https://www.drupal.org/node/2872159. $display = entity_get_form_display('media', $media_type->id(), 'default'); - $this->setDisplayOptions($display, $field_name, $source->getDisplayOptions($media_type, 'form')); + $source->prepareFormDisplay($media_type, $field_name, $display); $display->save(); } if ($source_field->isDisplayConfigurable('view')) { // @todo Replace entity_get_display() when #2367933 is done. // https://www.drupal.org/node/2872159. $display = entity_get_display('media', $media_type->id(), 'default'); - $this->setDisplayOptions($display, $field_name, $source->getDisplayOptions($media_type, 'view')); + $source->prepareViewDisplay($media_type, $field_name, $display); $display->save(); } } @@ -368,24 +368,2 @@ - /** - * Sets source display options on an entity display. - * - * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display - * A entity display. - * @param string $field_name - * Field that will be configured in the display. - * @param array|bool $display_options - * The source display options. - */ - protected function setDisplayOptions(EntityDisplayInterface $entity_display, $field_name, $display_options) { - if ($display_options === FALSE) { - $entity_display->removeComponent($field_name); - } - elseif (is_array($display_options)) { - $entity_display->setComponent($field_name, $display_options); - } - else { - $entity_display->setComponent($field_name); - } - } - } diff -u b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestDifferentDisplays.php b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestDifferentDisplays.php --- b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestDifferentDisplays.php +++ b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestDifferentDisplays.php @@ -2,6 +2,8 @@ namespace Drupal\media_test_source\Plugin\media\Source; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\media\MediaTypeInterface; /** @@ -19,18 +21,15 @@ /** * {@inheritdoc} */ - public function getDisplayOptions(MediaTypeInterface $type, $display_context, $display_mode = 'default') { - switch ($display_context) { - case 'view': - return [ - 'type' => 'entity_reference_entity_id', - ]; - - case 'form': - return [ - 'type' => 'entity_reference_autocomplete_tags', - ]; - } + public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { + $display->setComponent($field_name, ['type' => 'entity_reference_entity_id']); + } + + /** + * {@inheritdoc} + */ + public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { + $display->setComponent($field_name, ['type' => 'entity_reference_autocomplete_tags']); } /** diff -u b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestWithHiddenSourceField.php b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestWithHiddenSourceField.php --- b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestWithHiddenSourceField.php +++ b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestWithHiddenSourceField.php @@ -2,6 +2,8 @@ namespace Drupal\media_test_source\Plugin\media\Source; +use Drupal\Core\Entity\Display\EntityFormDisplayInterface; +use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\media\MediaTypeInterface; /** @@ -19,8 +21,15 @@ /** * {@inheritdoc} */ - public function getDisplayOptions(MediaTypeInterface $type, $display_context, $display_mode = 'default') { - return FALSE; + public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { + $display->removeComponent($field_name); + } + + /** + * {@inheritdoc} + */ + public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { + $display->removeComponent($field_name); } /**