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 @@ -140,15 +140,15 @@ public function createSourceField(MediaTypeInterface $type); /** - * Get the options for the source field in the form/view display. + * Gets the options for the source field in the form/view display. * * @param \Drupal\media\MediaTypeInterface $type - * A media type. + * The media type which is using this source. * @param string $display_context - * Possible parameters are 'view' or 'form'. + * The display context which will use these options. Can be 'view' or 'form. * * @return array|bool - * The display options as array or FALSE for removing this component from + * The display options, or FALSE for removing this component from * the display. * * @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() 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 @@ -367,24 +367,24 @@ } /** - * Set's source display options to an entity display. + * Sets source display options to an entity display. * - * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entityDisplay + * @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 $entityDisplay, $field_name, $display_options) { + protected function setDisplayOptions(EntityDisplayInterface $entity_display, $field_name, $display_options) { if ($display_options === FALSE) { - $entityDisplay->removeComponent($field_name); + $entity_display->removeComponent($field_name); } elseif (is_array($display_options)) { - $entityDisplay->setComponent($field_name, $display_options); + $entity_display->setComponent($field_name, $display_options); } else { - $entityDisplay->setComponent($field_name); + $entity_display->setComponent($field_name); } } diff -u b/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php --- b/core/modules/media/tests/src/Kernel/MediaSourceTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php @@ -450,44 +450,18 @@ $id = 'test_different_displays'; $field_name = 'field_media_different_display'; - /** @var \Drupal\media\MediaTypeInterface $type */ - $type = MediaType::create([ - 'source' => $id, - ]); - - $form = MediaTypeForm::create(\Drupal::getContainer()); - $form->setEntity($type); - $form->setModuleHandler(\Drupal::moduleHandler()); - $form->setEntityTypeManager(\Drupal::entityTypeManager()); - - $form_state = new FormState(); - $form_state->setValues([ - 'label' => 'Test type', - 'id' => $id, - 'op' => t('Save'), - ]); - - /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ - $field_manager = \Drupal::service('entity_field.manager'); - - // Source field not created yet. - $fields = $field_manager->getFieldDefinitions('media', $id); - $this->assertArrayNotHasKey($field_name, $fields); - - \Drupal::formBuilder()->submitForm($form, $form_state); - - // Source field exists now. - $fields = $field_manager->getFieldDefinitions('media', $id); - $this->assertArrayHasKey($field_name, $fields); + $this->createMediaTypeViaForm($id, $field_name); // Source field not in displays. $display = entity_get_display('media', $id, 'default'); - $this->assertArrayHasKey($field_name, $display->getComponents()); - $this->assertSame('entity_reference_entity_id', $display->getComponents()[$field_name]['type']); + $components = $display->getComponents(); + $this->assertArrayHasKey($field_name, $components); + $this->assertSame('entity_reference_entity_id', $components[$field_name]['type']); $display = entity_get_form_display('media', $id, 'default'); - $this->assertArrayHasKey($field_name, $display->getComponents()); - $this->assertSame('entity_reference_autocomplete_tags', $display->getComponents()[$field_name]['type']); + $components = $display->getComponents(); + $this->assertArrayHasKey($field_name, $components); + $this->assertSame('entity_reference_autocomplete_tags', $components[$field_name]['type']); } /** @@ -497,20 +471,36 @@ $id = 'test_hidden_source_field'; $field_name = 'field_media_hidden'; + $this->createMediaTypeViaForm($id, $field_name); + + // Source field not in displays. + $display = entity_get_display('media', $id, 'default'); + $this->assertArrayNotHasKey($field_name, $display->getComponents()); + + $display = entity_get_form_display('media', $id, 'default'); + $this->assertArrayNotHasKey($field_name, $display->getComponents()); + } + + /** + * Creates a media type via form submit. + * + * @param int $media_type_id + * Media type id. + * @param string $field_name + * Source field name. + */ + protected function createMediaTypeViaForm($media_type_id, $field_name) { /** @var \Drupal\media\MediaTypeInterface $type */ - $type = MediaType::create([ - 'source' => $id, - ]); + $type = MediaType::create(['source' => $media_type_id]); - $form = MediaTypeForm::create(\Drupal::getContainer()); - $form->setEntity($type); - $form->setModuleHandler(\Drupal::moduleHandler()); - $form->setEntityTypeManager(\Drupal::entityTypeManager()); + $form = $this->container->get('entity_type.manager') + ->getFormObject('media_type', 'add') + ->setEntity($type); $form_state = new FormState(); $form_state->setValues([ 'label' => 'Test type', - 'id' => $id, + 'id' => $media_type_id, 'op' => t('Save'), ]); @@ -518,21 +508,14 @@ $field_manager = \Drupal::service('entity_field.manager'); // Source field not created yet. - $fields = $field_manager->getFieldDefinitions('media', $id); + $fields = $field_manager->getFieldDefinitions('media', $media_type_id); $this->assertArrayNotHasKey($field_name, $fields); \Drupal::formBuilder()->submitForm($form, $form_state); // Source field exists now. - $fields = $field_manager->getFieldDefinitions('media', $id); + $fields = $field_manager->getFieldDefinitions('media', $media_type_id); $this->assertArrayHasKey($field_name, $fields); - - // Source field not in displays. - $display = entity_get_display('media', $id, 'default'); - $this->assertArrayNotHasKey($field_name, $display->getComponents()); - - $display = entity_get_form_display('media', $id, 'default'); - $this->assertArrayNotHasKey($field_name, $display->getComponents()); } }