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('About') . '

'; - $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('Uses') . '

'; $output .= '
'; $output .= '
' . t('Creating media items') . '
'; $output .= '
' . t('When a new media item is created, the Media module records basic information about it, including the author, date of creation, and the media type. It also manages the publishing options, which define whether or not the item is published. Default settings can be configured for each type of media on your site.', [':media-type' => Url::fromRoute('entity.media_type.collection')->toString()]) . '
'; + $output .= '
' . t('Listing media items') . '
'; + $output .= '
' . t('Media items are listed at the media administration page.', [ + ':media-collection' => Url::fromRoute('entity.media.collection')->toString(), + ]) . '
'; $output .= '
' . t('Creating custom media types') . '
'; - $output .= '
' . t('The Media module gives users with the Administer media types permission the ability to create new media types in addition to the default ones already configured. Each media type has an associated media source (such as the image source) which support thumbnail generation and metadata extraction. Fields managed by the Field module may be added for storing that metadata, such as width and height, as well as any other associated values.', [':media-new' => Url::fromRoute('entity.media_type.add_form')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '
'; + $output .= '
' . t('The Media module gives users with the Administer media types permission the ability to create new media types in addition to the default ones already configured. Each media type has an associated media source (such as the image source) which support thumbnail generation and metadata extraction. Fields managed by the Field module may be added for storing that metadata, such as width and height, as well as any other associated values.', [ + ':media-new' => Url::fromRoute('entity.media_type.add_form')->toString(), + ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), + ]) . '
'; $output .= '
' . t('Creating revisions') . '
'; $output .= '
' . t('The Media module also enables you to create multiple versions of any media item, and revert to older versions using the Revision information settings.') . '
'; $output .= '
' . t('User permissions') . '
'; - $output .= '
' . t('The Media module makes a number of permissions available, which can be set by role on the permissions page.', [':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-media'])->toString()]) . '
'; + $output .= '
' . t('The Media module makes a number of permissions available, which can be set by role on the permissions page.', [ + ':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-media'])->toString(), + ]) . '
'; + $output .= '
' . t('Adding media to other content') . '
'; + $output .= '
' . t('Users with permission to administer content types can add media support by adding a media reference field to the content type on the content type administration page. (The same is true of block types, taxonomy terms, user profiles, and other content that supports fields.) A media reference field can refer to any configured media type. It is possible to allow multiple media types in the same field.') . '
'; $output .= '
'; + $output .= '

' . t('Differences between Media, File, and Image reference fields') . '

'; + $output .= '

' . t('Media reference fields offer several advantages over basic File and Image references:') . '

'; + $output .= ''; + $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()); + } + } + +}