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 @@ -322,15 +322,15 @@ /** * {@inheritdoc} */ - public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { - $display->setComponent($field_name); + public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { + $display->setComponent($this->getSourceFieldDefinition($type)->getName()); } /** * {@inheritdoc} */ - public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { - $display->setComponent($field_name); + public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) { + $display->setComponent($this->getSourceFieldDefinition($type)->getName()); } } 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 @@ -143,42 +143,38 @@ /** - * Prepares the source field in the view display. + * Prepares the media type fields for this source in the view 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. + * configure the media type fields in the view 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\EntityViewDisplayInterface $display - * The display in which the field appears. + * The display which should be prepared. * * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() */ - public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display); + public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display); /** - * Prepares the source field in the form display. + * Prepares the media type fields for this source 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. + * configure the media type fields 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. + * The display which should be prepared. * * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() */ - public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display); + public function prepareFormDisplay(MediaTypeInterface $type, 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 @@ -4,7 +4,6 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; -use Drupal\Core\Entity\Display\EntityDisplayInterface; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Field\BaseFieldDefinition; @@ -328,19 +327,18 @@ // Add the new field to the default form and view displays for this // media type. - $field_name = $source_field->getName(); if ($source_field->isDisplayConfigurable('form')) { // @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'); - $source->prepareFormDisplay($media_type, $field_name, $display); + $source->prepareFormDisplay($media_type, $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'); - $source->prepareViewDisplay($media_type, $field_name, $display); + $source->prepareViewDisplay($media_type, $display); $display->save(); } } 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 @@ -21,15 +21,19 @@ /** * {@inheritdoc} */ - public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { - $display->setComponent($field_name, ['type' => 'entity_reference_entity_id']); + public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { + $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ + 'type' => 'entity_reference_entity_id', + ]); } /** * {@inheritdoc} */ - public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { - $display->setComponent($field_name, ['type' => 'entity_reference_autocomplete_tags']); + public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) { + $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [ + '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 @@ -21,15 +21,15 @@ /** * {@inheritdoc} */ - public function prepareViewDisplay(MediaTypeInterface $type, $field_name, EntityViewDisplayInterface $display) { - $display->removeComponent($field_name); + public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) { + $display->removeComponent($this->getSourceFieldDefinition($type)->getName()); } /** * {@inheritdoc} */ - public function prepareFormDisplay(MediaTypeInterface $type, $field_name, EntityFormDisplayInterface $display) { - $display->removeComponent($field_name); + public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) { + $display->removeComponent($this->getSourceFieldDefinition($type)->getName()); } /**