Commit 7154acda authored by xjm's avatar xjm
Browse files

Issue #3020707 by seanB, phenaproxima, xjm, webchick, larowlan: Streamline...

Issue #3020707 by seanB, phenaproxima, xjm, webchick, larowlan: Streamline buttons in the media library widget
parent 38bb516f
......@@ -14,7 +14,6 @@
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\media_library\Form\MediaLibraryUploadForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
......@@ -42,13 +41,6 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
*/
protected $entityTypeManager;
/**
* Indicates whether or not the add button should be shown.
*
* @var bool
*/
protected $addAccess = FALSE;
/**
* Constructs a MediaLibraryWidget widget.
*
......@@ -64,30 +56,23 @@ class MediaLibraryWidget extends WidgetBase implements ContainerFactoryPluginInt
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager service.
* @param bool $add_access
* Indicates whether or not the add button should be shown.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager, $add_access) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
$this->addAccess = $add_access;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$settings = $configuration['field_definition']->getSettings()['handler_settings'];
$target_bundles = isset($settings['target_bundles']) ? $settings['target_bundles'] : NULL;
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('entity_type.manager'),
// @todo Use URL access in https://www.drupal.org/node/2956747
MediaLibraryUploadForm::create($container)->access($target_bundles)->isAllowed()
$container->get('entity_type.manager')
);
}
......@@ -247,7 +232,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// Add a button that will load the Media library in a modal using AJAX.
$element['media_library_open_button'] = [
'#type' => 'link',
'#title' => $this->t('Browse media'),
'#title' => $this->t('Add media'),
'#name' => $field_name . '-media-library-open-button' . $id_suffix,
// @todo Make the view configurable in https://www.drupal.org/project/drupal/issues/2971209
'#url' => Url::fromRoute('view.media_library.widget', [], [
......@@ -263,23 +248,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#access' => $cardinality_unlimited || $remaining > 0,
];
$element['media_library_add_button'] = [
'#type' => 'link',
'#title' => $this->t('Add media'),
'#name' => $field_name . '-media-library-add-button' . $id_suffix,
'#url' => Url::fromRoute('media_library.upload', [], [
'query' => $query,
]),
'#attributes' => [
'class' => ['button', 'use-ajax', 'media-library-add-button'],
'data-dialog-type' => 'modal',
'data-dialog-options' => $dialog_options,
],
// Prevent errors in other widgets from preventing addition.
'#limit_validation_errors' => $limit_validation_errors,
'#access' => $this->addAccess && ($cardinality_unlimited || $remaining > 0),
];
// This hidden field and button are used to add new items to the widget.
$element['media_library_selection'] = [
'#type' => 'hidden',
......
......@@ -341,13 +341,13 @@ public function testWidgetUpload() {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = $this->container->get('file_system');
// Ensure that the add button is not present if no media can be created.
$assert_session->elementNotExists('css', '.media-library-add-button[href*="field_noadd_media"]');
// Add to the twin media field using the add button directly on the widget.
$twin_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_twin_media"]');
// Add to the twin media field.
$twin_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_twin_media"]');
$twin_button->click();
$assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextContains('Media library');
$assert_session->elementExists('css', '#drupal-modal')->clickLink('Add media');
$assert_session->assertWaitOnAjaxRequest();
$page->attachFileToField('Upload', $this->container->get('file_system')->realpath($png_image->uri));
$assert_session->assertWaitOnAjaxRequest();
......@@ -376,9 +376,12 @@ public function testWidgetUpload() {
$assert_session->pageTextContains($png_image->filename);
// Also make sure that we can upload to the unlimited cardinality field.
$unlimited_button = $assert_session->elementExists('css', '.media-library-add-button[href*="field_unlimited_media"]');
$unlimited_button = $assert_session->elementExists('css', '.media-library-open-button[href*="field_unlimited_media"]');
$unlimited_button->click();
$assert_session->assertWaitOnAjaxRequest();
$assert_session->pageTextContains('Media library');
$assert_session->elementExists('css', '#drupal-modal')->clickLink('Add media');
$assert_session->assertWaitOnAjaxRequest();
// Multiple uploads should be allowed.
// @todo Add test when https://github.com/minkphp/Mink/issues/358 is closed
......
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