diff --git a/core/modules/media/media.module b/core/modules/media/media.module index b96828391d..cd8c842726 100644 --- a/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -9,7 +9,6 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; -use Drupal\Core\Render\Markup; use Drupal\Core\Render\Element\RenderElement; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; @@ -226,7 +225,7 @@ function media_field_widget_multivalue_form_alter(array &$elements, FormStateInt // Add a section about how to create media if the user has access to do so. if (!empty($create_bundles)) { if (count($create_bundles) === 1) { - $add_url = Url::fromRoute('entity.media.add_form')->setRouteParameter('media_type', $create_bundles[0])->toString(); + $add_url = Url::fromRoute('entity.media.add_form', ['media_type' => $create_bundles[0]])->toString(); } elseif (count($create_bundles) > 1) { $add_url = Url::fromRoute('entity.media.add_page')->toString(); @@ -235,35 +234,35 @@ function media_field_widget_multivalue_form_alter(array &$elements, FormStateInt } $elements['#theme'] = 'media_reference_help'; - $cardinality = $context['items']->getFieldDefinition()->getFieldStorageDefinition()->getCardinality(); // @todo template_preprocess_field_multiple_value_form() assumes this key // exists, but it does not exist in the case of a single widget that // accepts multiple values. This is for some reason necessary to use // our template for the entity_autocomplete_tags widget. - // Research and resolve this in htttps://www.drupal.org/node/2943020. + // Research and resolve this in https://www.drupal.org/node/2943020. if (empty($elements['#cardinality_multiple'])) { $elements['#cardinality_multiple'] = NULL; } + // Use the title set on the element if it exists, otherwise fall back to the + // field label. + $elements['#media_help']['#original_label'] = isset($elements['#title']) ? $elements['#title'] : $context['items']->getFieldDefinition()->getLabel(); + // Customize the label for the field widget. // @todo Research a better approach https://www.drupal.org/node/2943024. $use_existing_label = t('Use existing media'); if (!empty($elements[0]['target_id']['#title'])) { - $elements['#media_help']['#original_label'] = $elements[0]['target_id']['#title']; $elements[0]['target_id']['#title'] = $use_existing_label; } - elseif (!empty($elements['#title'])) { - $elements['#media_help']['#original_label'] = $elements['#title']; + if (!empty($elements['#title'])) { $elements['#title'] = $use_existing_label; } - elseif (!empty($elements['target_id']['#title'])) { - $elements['#media_help']['#original_label'] = $elements['target_id']['#title']; + if (!empty($elements['target_id']['#title'])) { $elements['target_id']['#title'] = $use_existing_label; } - // Only present the help text about autcomplete on autocomplete widgets. - // When the user is presented with options, they don't need to type anything - // or know what types of media are allowed. + // This help text is only relevant for autocomplete widgets. When the user + // is presented with options, they don't need to type anything or know what + // types of media are allowed. if ($is_autocomplete) { $elements['#media_help']['#media_list_help'] = t('Type part of the media name.'); diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php index 8fe84188cf..a6711f1dd8 100644 --- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -205,10 +205,9 @@ public function testRenderedEntityReferencedMedia() { */ public function providerTestMediaReferenceWidget() { return [ - // Single-value fields with a single media type: + // Single-value fields with a single media type and the default widget: // - The user can create and list the media. 'single_value:single_type:create_list' => [1, [TRUE], TRUE], - // - The user can list but not create the media. 'single_value:single_type:list' => [1, [FALSE], TRUE], // - The user can create but not list the media. @@ -216,6 +215,16 @@ public function providerTestMediaReferenceWidget() { // - The user can neither create nor list the media. 'single_value:single_type' => [1, [FALSE], FALSE], + // Single-value fields with the tags-style widget: + // - The user can create and list the media. + 'single_value:single_type:create_list:tags' => [1, [TRUE], TRUE, 'entity_reference_autocomplete_tags'], + // - The user can list but not create the media. + 'single_value:single_type:list:tags' => [1, [FALSE], TRUE, 'entity_reference_autocomplete_tags'], + // - The user can create but not list the media. + 'single_value:single_type:create:tags' => [1, [TRUE], FALSE, 'entity_reference_autocomplete_tags'], + // - The user can neither create nor list the media. + 'single_value:single_type:tags' => [1, [FALSE], FALSE, 'entity_reference_autocomplete_tags'], + // Single-value fields with two media types: // - The user can create both types. 'single_value:two_type:create2_list' => [1, [TRUE, TRUE], TRUE], @@ -227,9 +236,13 @@ public function providerTestMediaReferenceWidget() { // Multiple-value field with a cardinality of 3, with media the user can // create and list. 'multi_value:single_type:create_list' => [3, [TRUE], TRUE], + // The same, with the tags field. + 'multi-value:single_type:create_list:tags' => [3, [TRUE], TRUE, 'entity_reference_autocomplete_tags'], // Unlimited value field. 'unlimited_value:single_type:create_list' => [FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, [TRUE], TRUE], + // Unlimited value field with the tags widget. + 'unlimited_value:single_type:create_list' => [FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, [TRUE], TRUE, 'entity_reference_autocomplete_tags'], ]; } @@ -251,7 +264,7 @@ public function providerTestMediaReferenceWidget() { * * @dataProvider providerTestMediaReferenceWidget */ - public function testMediaReferenceWidget($cardinality, array $media_type_create_access, $list_access) { + public function testMediaReferenceWidget($cardinality, array $media_type_create_access, $list_access, $widget_id = 'entity_reference_autocomplete') { $assert_session = $this->assertSession(); // Create two content types. @@ -309,7 +322,7 @@ public function testMediaReferenceWidget($cardinality, array $media_type_create_ ->getStorage('entity_form_display') ->load('node.' . $non_media_content_type->id() . '.default') ->setComponent('field_not_a_media_field', [ - 'type' => 'entity_reference_autocomplete', + 'type' => $widget_id, ]) ->save(); @@ -341,7 +354,7 @@ public function testMediaReferenceWidget($cardinality, array $media_type_create_ ->getStorage('entity_form_display') ->load('node.' . $content_type->id() . '.default') ->setComponent('field_media_reference', [ - 'type' => 'entity_reference_autocomplete', + 'type' => $widget_id, ]) ->save();