diff --git a/core/modules/media/media.module b/core/modules/media/media.module index dd68ce86ba..f55a0aadc3 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -9,6 +9,7 @@ use Drupal\Core\Session\AccountInterface; use Drupal\field\FieldConfigInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; @@ -100,21 +101,17 @@ function template_preprocess_media(array &$variables) { /** * Implements hook_form_FORM_ID_alter(). */ -function media_form_field_ui_field_storage_add_form_alter(array &$form) { - $reference = (string) t('Reference'); - $general = (string) t('General'); +function media_form_field_ui_field_storage_add_form_alter(array &$form, FormStateInterface $form_state) { + $reference = t('Reference')->__toString(); - // Move the "file" option to the "General" optgroup. - if (isset($form['add']['new_storage_type']['#options'][$reference]['image'])) { - $image = $form['add']['new_storage_type']['#options'][$reference]['image']; - $form['add']['new_storage_type']['#options'][$general]['image'] = $image; - unset($form['add']['new_storage_type']['#options'][$reference]['image']); - } - - // Move the "file" option to the "General" optgroup. - if (isset($form['add']['new_storage_type']['#options'][$reference]['file'])) { - $file = $form['add']['new_storage_type']['#options'][$reference]['file']; - unset($form['add']['new_storage_type']['#options'][$reference]['file']); - $form['add']['new_storage_type']['#options'][$general]['file'] = $file; + // Remove the "image" and "file" options from the "Reference" optgroup if this + // is NOT a media entity. + if ($form_state->get('entity_type_id') !== 'media') { + if (isset($form['add']['new_storage_type']['#options'][$reference]['image'])) { + unset($form['add']['new_storage_type']['#options'][$reference]['image']); + } + if (isset($form['add']['new_storage_type']['#options'][$reference]['file'])) { + unset($form['add']['new_storage_type']['#options'][$reference]['file']); + } } } diff --git a/core/modules/media/tests/src/Functional/MediaFieldStorageAddFormTest.php b/core/modules/media/tests/src/Functional/MediaFieldStorageAddFormTest.php new file mode 100644 index 0000000000..11b9a423ac --- /dev/null +++ b/core/modules/media/tests/src/Functional/MediaFieldStorageAddFormTest.php @@ -0,0 +1,37 @@ +assertSession(); + // On non-media entities "file" and "image" should not exist in the option + // list. + $node_type = NodeType::create(['type' => 'page', 'label' => 'Page']); + $node_type->save(); + $this->drupalGet('admin/structure/types/manage/' . $node_type->id() . '/fields/add-field'); + $assert->statusCodeEquals(200); + $assert->optionNotExists('Add a new field', 'file'); + $assert->optionNotExists('Add a new field', 'image'); + + // On media entities "file" and "image" should exist in the option list. + $media_type = $this->createMediaType([], 'file'); + $media_type_id = $media_type->id(); + $this->drupalGet('admin/structure/media/manage/' . $media_type_id . '/fields/add-field'); + $assert->statusCodeEquals(200); + $assert->optionExists('Add a new field', 'file'); + $assert->optionExists('Add a new field', 'image'); + } + +}