Unverified Commit ba4ed741 authored by alexpott's avatar alexpott

Issue #3102402 by Hardik_Patel_12, sathish.redcrackle, hash6,...

Issue #3102402 by Hardik_Patel_12, sathish.redcrackle, hash6, himanshu_sindhwani, swatichouhan012, shaktik, idebr, mrinalini9, kishor_kolekar, priyanka.sahni, ambuj_gupta, lauriii, mayurgajar, snehalgaikwad, alexpott, phenaproxima: Remove weight field from Media Library widget when only single media can be attached

(cherry picked from commit da8e7f68)
parent 08b33b57
......@@ -333,6 +333,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
return $element;
}
$multiple_items = FALSE;
if (empty($referenced_entities)) {
$element['#field_prefix']['empty_selection'] = [
'#markup' => $this->t('No media items are selected.'),
......@@ -342,10 +343,12 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// @todo Use a <button> link here, and delete
// seven_preprocess_fieldset__media_library_widget(), when
// https://www.drupal.org/project/drupal/issues/2999549 lands.
$multiple_items = count($referenced_entities) > 1;
$element['#field_prefix']['weight_toggle'] = [
'#type' => 'html_tag',
'#tag' => 'button',
'#value' => $this->t('Show media item weights'),
'#access' => $multiple_items,
'#attributes' => [
'class' => [
'link',
......@@ -416,6 +419,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#type' => 'number',
'#theme' => 'input__number__media_library_item_weight',
'#title' => $this->t('Weight'),
'#access' => $multiple_items,
'#default_value' => $delta,
'#attributes' => [
'class' => [
......
......@@ -191,11 +191,10 @@ public function testWidget() {
// Assert the focus is set back on the open button of the media field.
$this->assertJsCondition('jQuery("#field_twin_media-media-library-wrapper .js-media-library-open-button").is(":focus")');
// Assert that we can toggle the visibility of the weight inputs.
// The toggle for weight inputs' visibility should not be available when the
// field contains a single item.
$wrapper = $assert_session->elementExists('css', '.field--name-field-twin-media');
$wrapper->pressButton('Show media item weights');
$assert_session->fieldExists('Weight', $wrapper)->click();
$wrapper->pressButton('Hide media item weights');
$assert_session->elementNotExists('named', ['button', 'Show media item weights'], $wrapper);
// Remove the selected item.
$button = $assert_session->buttonExists('Remove', $wrapper);
......@@ -212,6 +211,14 @@ public function testWidget() {
$this->openMediaLibraryForField('field_twin_media');
$page->checkField('Select Dog');
$this->pressInsertSelected('Added one media item.');
// Assert that we can toggle the visibility of the weight inputs when the
// field contains more than one item.
$wrapper = $assert_session->elementExists('css', '.field--name-field-twin-media');
$wrapper->pressButton('Show media item weights');
// Ensure that the styling doesn't accidentally render the weight field
// unusable.
$assert_session->fieldExists('Weight', $wrapper)->click();
$wrapper->pressButton('Hide media item weights');
// Assert the same has been added twice and remove the items again.
$this->waitForElementsCount('css', '.field--name-field-twin-media [data-media-library-item-delta]', 2);
......
......@@ -21,6 +21,7 @@ protected function setUp(): void {
// Create a few example media items for use in selection.
$this->createMediaItems([
'type_one' => [
'Cat',
'Dog',
],
]);
......@@ -60,12 +61,23 @@ public function testWidgetAnonymous() {
// Ensure that the selection completed successfully.
$this->waitForText('Dog');
$assert_session->fieldNotExists('Weight');
// Add to the unlimited cardinality field.
$this->openMediaLibraryForField('field_unlimited_media');
// Select the second media item (should be Cat).
$this->selectMediaItem(1);
$this->pressInsertSelected('Added one media item.');
// Ensure that the selection completed successfully.
$this->waitForText('Cat');
// Save the form.
$assert_session->elementExists('css', '.js-media-library-widget-toggle-weight')->click();
$this->submitForm([
'title[0][value]' => 'My page',
'field_unlimited_media[selection][0][weight]' => '0',
'field_unlimited_media[selection][1][weight]' => '1',
], 'Save');
$assert_session->pageTextContains('Basic Page My page has been created');
$assert_session->pageTextContains('Dog');
......
......@@ -478,9 +478,16 @@
.media-library-item__preview {
padding-bottom: 34px;
}
.field--widget-media-library-widget .media-library-item__preview {
cursor: move;
}
.field--widget-media-library-widget .js-media-library-item:only-child .media-library-item__preview {
cursor: inherit;
}
.media-library-item__status {
position: absolute;
top: 40px;
......
......@@ -440,9 +440,16 @@
/* Media library entity view display styles. */
.media-library-item__preview {
padding-bottom: 34px;
}
.field--widget-media-library-widget .media-library-item__preview {
cursor: move;
}
.field--widget-media-library-widget .js-media-library-item:only-child .media-library-item__preview {
cursor: inherit;
}
.media-library-item__status {
position: absolute;
top: 40px;
......
......@@ -456,9 +456,16 @@
/* Media library entity view display styles. */
.media-library-item__preview {
padding-bottom: 34px;
}
.field--widget-media-library-widget .media-library-item__preview {
cursor: move;
}
.field--widget-media-library-widget .js-media-library-item:only-child .media-library-item__preview {
cursor: inherit;
}
.media-library-item__status {
position: absolute;
top: 40px;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment