diff --git a/core/modules/media/media.module b/core/modules/media/media.module index 5df059c284b89cd65cd88d4aad43c6b02178b08d..296945973ae5eae6f05cf833894fa4cf7cf22e8c 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -6,12 +6,13 @@ */ use Drupal\Core\Access\AccessResult; -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\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\field\FieldConfigInterface; /** * Implements hook_help(). @@ -20,18 +21,39 @@ function media_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.media': $output = '
' . t('The Media module manages the creation, editing, deletion, settings and display of media. Items are typically images, documents, slideshows, YouTube videos, tweets, Instagram photos, etc. You can reference media items from any other content on your site. For more information, see the online documentation for the Media module.', [':media' => 'https://www.drupal.org/docs/8/core/modules/media']) . '
'; + $output .= '' . t('The Media module manages the creation, editing, deletion, settings, and display of media. Items are typically images, documents, slideshows, YouTube videos, tweets, Instagram photos, etc. You can reference media items from any other content on your site. For more information, see the online documentation for the Media module.', [':media' => 'https://www.drupal.org/docs/8/core/modules/media']) . '
'; $output .= '' . t('Media reference fields offer several advantages over basic File and Image references:') . '
'; + $output .= '' . t('Use Media reference fields for most files, images, audio, videos, and remote media. Use File or Image reference fields when creating your own media types, or for legacy files and images created before enabling the Media module.') . '
'; return $output; } } @@ -115,3 +137,38 @@ function media_field_ui_preconfigured_options_alter(array &$options, $field_type $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view'; } } + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function media_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state, $form_id) { + // Provide some help text to aid users decide whether they need a Media, + // File, or Image reference field. + $description_text = t('Use Media reference fields for most files, images, audio, videos, and remote media. Use File or Image reference fields when creating your own media types, or for legacy files and images created before enabling the Media module.'); + if (\Drupal::moduleHandler()->moduleExists('help')) { + $description_text .= ' ' . t('For more information, see the Media help page.', [ + '@help_url' => Url::fromRoute('help.page', ['name' => 'media'])->toString(), + ]); + } + $form['add']['description_wrapper'] = [ + '#type' => 'container', + ]; + $field_types = [ + 'file', + 'image', + 'field_ui:entity_reference:media', + ]; + foreach ($field_types as $field_name) { + $form['add']['description_wrapper']["description_{$field_name}"] = [ + '#type' => 'item', + '#markup' => $description_text, + '#states' => [ + 'visible' => [ + ':input[name="new_storage_type"]' => ['value' => $field_name], + ], + ], + ]; + } + $form['add']['new_storage_type']['#weight'] = 0; + $form['add']['description_wrapper']['#weight'] = 1; +} diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaReferenceFieldHelpTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaReferenceFieldHelpTest.php new file mode 100644 index 0000000000000000000000000000000000000000..7d224842381e634c28b3c6420595b199868be380 --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaReferenceFieldHelpTest.php @@ -0,0 +1,56 @@ +assertSession(); + $page = $this->getSession()->getPage(); + + $type = $this->drupalCreateContentType([ + 'type' => 'foo', + ]); + $this->drupalGet("/admin/structure/types/manage/{$type->id()}/fields/add-field"); + + $field_types = [ + 'file', + 'image', + 'field_ui:entity_reference:media', + ]; + $description_ids = array_map(function ($item) { + return '#edit-description-' . Html::cleanCssIdentifier($item); + }, $field_types); + + // Choose a boolean field, none of the description containers should be + // visible. + $assert_session->optionExists('edit-new-storage-type', 'boolean'); + $page->selectFieldOption('edit-new-storage-type', 'boolean'); + foreach ($description_ids as $description_id) { + $this->assertFalse($assert_session->elementExists('css', $description_id)->isVisible()); + } + // Select each of the file, image, and media fields and verify their + // descriptions are now visible and match the expected text. + $help_text = 'Use Media reference fields for most files, images, audio, videos, and remote media. Use File or Image reference fields when creating your own media types, or for legacy files and images created before enabling the Media module.'; + foreach ($field_types as $field_name) { + $assert_session->optionExists('edit-new-storage-type', $field_name); + $page->selectFieldOption('edit-new-storage-type', $field_name); + $field_description_element = $assert_session->elementExists('css', '#edit-description-' . Html::cleanCssIdentifier($field_name)); + $this->assertTrue($field_description_element->isVisible()); + $this->assertEquals($help_text, $field_description_element->getText()); + } + } + +}