Skip to content

Commit 7154acd

Browse files
committed
Issue #3020707 by seanB, phenaproxima, xjm, webchick, larowlan: Streamline buttons in the media library widget
1 parent 38bb516 commit 7154acd

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1515
use Drupal\Core\Url;
1616
use Drupal\media\Entity\Media;
17-
use Drupal\media_library\Form\MediaLibraryUploadForm;
1817
use Symfony\Component\DependencyInjection\ContainerInterface;
1918
use Symfony\Component\Validator\ConstraintViolationInterface;
2019

@@ -42,13 +41,6 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
4241
*/
4342
protected $entityTypeManager;
4443

45-
/**
46-
* Indicates whether or not the add button should be shown.
47-
*
48-
* @var bool
49-
*/
50-
protected $addAccess = FALSE;
51-
5244
/**
5345
* Constructs a MediaLibraryWidget widget.
5446
*
@@ -64,30 +56,23 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
6456
* Any third party settings.
6557
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
6658
* Entity type manager service.
67-
* @param bool $add_access
68-
* Indicates whether or not the add button should be shown.
6959
*/
70-
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, $add_access) {
60+
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
7161
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
7262
$this->entityTypeManager = $entity_type_manager;
73-
$this->addAccess = $add_access;
7463
}
7564

7665
/**
7766
* {@inheritdoc}
7867
*/
7968
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80-
$settings = $configuration['field_definition']->getSettings()['handler_settings'];
81-
$target_bundles = isset($settings['target_bundles']) ? $settings['target_bundles'] : NULL;
8269
return new static(
8370
$plugin_id,
8471
$plugin_definition,
8572
$configuration['field_definition'],
8673
$configuration['settings'],
8774
$configuration['third_party_settings'],
88-
$container->get('entity_type.manager'),
89-
// @todo Use URL access in https://www.drupal.org/node/2956747
90-
MediaLibraryUploadForm::create($container)->access($target_bundles)->isAllowed()
75+
$container->get('entity_type.manager')
9176
);
9277
}
9378

@@ -247,7 +232,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
247232
// Add a button that will load the Media library in a modal using AJAX.
248233
$element['media_library_open_button'] = [
249234
'#type' => 'link',
250-
'#title' => $this->t('Browse media'),
235+
'#title' => $this->t('Add media'),
251236
'#name' => $field_name . '-media-library-open-button' . $id_suffix,
252237
// @todo Make the view configurable in https://www.drupal.org/project/drupal/issues/2971209
253238
'#url' => Url::fromRoute('view.media_library.widget', [], [
@@ -263,23 +248,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
263248
'#access' => $cardinality_unlimited || $remaining > 0,
264249
];
265250

266-
$element['media_library_add_button'] = [
267-
'#type' => 'link',
268-
'#title' => $this->t('Add media'),
269-
'#name' => $field_name . '-media-library-add-button' . $id_suffix,
270-
'#url' => Url::fromRoute('media_library.upload', [], [
271-
'query' => $query,
272-
]),
273-
'#attributes' => [
274-
'class' => ['button', 'use-ajax', 'media-library-add-button'],
275-
'data-dialog-type' => 'modal',
276-
'data-dialog-options' => $dialog_options,
277-
],
278-
// Prevent errors in other widgets from preventing addition.
279-
'#limit_validation_errors' => $limit_validation_errors,
280-
'#access' => $this->addAccess && ($cardinality_unlimited || $remaining > 0),
281-
];
282-
283251
// This hidden field and button are used to add new items to the widget.
284252
$element['media_library_selection'] = [
285253
'#type' => 'hidden',

core/modules/media_library/tests/src/FunctionalJavascript/MediaLibraryTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ public function testWidgetUpload() {
341341
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
342342
$file_system = $this->container->get('file_system');
343343

344-
// Ensure that the add button is not present if no media can be created.
345-
$assert_session->elementNotExists('css', '.media-library-add-button[href*="field_noadd_media"]');
346-
347-
// Add to the twin media field using the add button directly on the widget.
348-
$twin_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_twin_media"]');
344+
// Add to the twin media field.
345+
$twin_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_twin_media"]');
349346
$twin_button->click();
350347
$assert_session->assertWaitOnAjaxRequest();
348+
$assert_session->pageTextContains('Media library');
349+
$assert_session->elementExists('css', '#drupal-modal')->clickLink('Add media');
350+
$assert_session->assertWaitOnAjaxRequest();
351351

352352
$page->attachFileToField('Upload', $this->container->get('file_system')->realpath($png_image->uri));
353353
$assert_session->assertWaitOnAjaxRequest();
@@ -376,9 +376,12 @@ public function testWidgetUpload() {
376376
$assert_session->pageTextContains($png_image->filename);
377377

378378
// Also make sure that we can upload to the unlimited cardinality field.
379-
$unlimited_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_unlimited_media"]');
379+
$unlimited_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]');
380380
$unlimited_button->click();
381381
$assert_session->assertWaitOnAjaxRequest();
382+
$assert_session->pageTextContains('Media library');
383+
$assert_session->elementExists('css', '#drupal-modal')->clickLink('Add media');
384+
$assert_session->assertWaitOnAjaxRequest();
382385

383386
// Multiple uploads should be allowed.
384387
// @todo Add test when https://github.com/minkphp/Mink/issues/358 is closed

0 commit comments

Comments
 (0)