Commit 1e0ad95a authored by alexpott's avatar alexpott

Revert "Issue #3035316 by seanB, andrewmacpherson, oknate, phenaproxima,...

Revert "Issue #3035316 by seanB, andrewmacpherson, oknate, phenaproxima, rainbreaw: ‘Add media’ in MediaLibraryWidget should be a button, not a link"

This reverts commit 58d6b442.
parent 58d6b442
......@@ -27,7 +27,6 @@ widget:
js:
js/media_library.widget.js: {}
dependencies:
- core/drupal.dialog.ajax
- core/jquery.ui.sortable
- core/jquery.once
- media_library/style
......
......@@ -12,6 +12,7 @@
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
use Drupal\media_library\Ajax\UpdateSelectionCommand;
use Drupal\media_library\MediaLibraryState;
use Drupal\media_library\MediaLibraryUiBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -419,7 +420,7 @@ public function updateWidget(array &$form, FormStateInterface $form_state) {
// contains the vertical tabs. Besides that, we also need to force the media
// library to create a new instance of the media add form.
// @see \Drupal\media_library\MediaLibraryUiBuilder::buildMediaTypeAddForm()
$state = $form_state->get('media_library_state');
$state = MediaLibraryState::fromRequest($this->getRequest());
$state->remove('media_library_content');
$state->set('_media_library_form_rebuild', TRUE);
$library_ui = $this->libraryUiBuilder->buildUi($state);
......
......@@ -306,12 +306,6 @@ protected function buildMediaLibraryView(MediaLibraryState $state) {
$args = [$state->getSelectedTypeId()];
// Make sure the state parameters are set in the request so the view can
// pass the parameters along in the pager, filters etc.
$request = $view_executable->getRequest();
$request->query->add($state->all());
$view_executable->setRequest($request);
$view_executable->setDisplay($display_id);
$view_executable->preExecute($args);
$view_executable->execute($display_id);
......
......@@ -2,10 +2,9 @@
namespace Drupal\media_library\Plugin\Field\FieldWidget;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
......@@ -13,6 +12,7 @@
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\media_library\MediaLibraryUiBuilder;
use Drupal\media_library\MediaLibraryState;
......@@ -395,32 +395,23 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$opener_id = static::$openerIdPrefix . $field_name . $id_suffix;
$state = MediaLibraryState::create($opener_id, $allowed_media_type_ids, $selected_type_id, $remaining);
$dialog_options = Json::encode(MediaLibraryUiBuilder::dialogOptions());
// Add a button that will load the Media library in a modal using AJAX.
$element['media_library_open_button'] = [
'#type' => 'submit',
'#value' => $this->t('Add media'),
'#type' => 'link',
'#title' => $this->t('Add media'),
'#name' => $field_name . '-media-library-open-button' . $id_suffix,
'#url' => $url = Url::fromRoute('media_library.ui', [], [
'query' => $state->all(),
]),
'#attributes' => [
'class' => [
'media-library-open-button',
'js-media-library-open-button',
],
// The jQuery UI dialog automatically moves focus to the first :tabbable
// element of the modal, so we need to disable refocus on the button.
'data-disable-refocus' => 'true',
'class' => ['button', 'use-ajax', 'media-library-open-button'],
'data-dialog-type' => 'modal',
'data-dialog-options' => $dialog_options,
],
'#media_library_state' => $state,
'#ajax' => [
'callback' => [static::class, 'openMediaLibrary'],
'progress' => [
'type' => 'throbber',
'message' => $this->t('Opening media library.'),
],
],
'#submit' => [],
// Allow the media library to be opened even if there are form errors.
'#limit_validation_errors' => [],
// Prevent errors in other widgets from preventing addition.
'#limit_validation_errors' => $limit_validation_errors,
'#access' => $cardinality_unlimited || $remaining > 0,
];
......@@ -535,25 +526,6 @@ public static function removeItem(array $form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
/**
* AJAX callback to open the library modal.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* An AJAX response to open the media library.
*/
public static function openMediaLibrary(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
$library_ui = \Drupal::service('media_library.ui_builder')->buildUi($triggering_element['#media_library_state']);
$dialog_options = MediaLibraryUiBuilder::dialogOptions();
return (new AjaxResponse())
->addCommand(new OpenModalDialogCommand($dialog_options['title'], $library_ui, $dialog_options));
}
/**
* Validates that newly selected items can be added to the widget.
*
......
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