MediaLibraryFormElementOpener.php in Media Library Form API Element 2.x
File
src/MediaLibraryFormElementOpener.php
View source
<?php
namespace Drupal\media_library_form_element;
use Drupal\media_library\MediaLibraryOpenerInterface;
use Drupal\media_library\MediaLibraryState;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
class MediaLibraryFormElementOpener implements MediaLibraryOpenerInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
$process_result = function ($result) {
if ($result instanceof RefinableCacheableDependencyInterface) {
$result
->addCacheContexts([
'url.query_args',
]);
}
return $result;
};
return $process_result(AccessResult::allowed());
}
public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
$response = new AjaxResponse();
$parameters = $state
->getOpenerParameters();
$widget_id = $parameters['field_widget_id'];
$ids = implode(',', $selected_ids);
$response
->addCommand(new InvokeCommand(NULL, 'setMediaUploadFieldValue', [
$ids,
"[data-media-library-form-element-value=\"{$widget_id}\"]",
]))
->addCommand(new InvokeCommand("[data-media-library-form-element-update=\"{$widget_id}\"]", 'trigger', [
'mousedown',
]))
->addCommand(new CloseModalDialogCommand(TRUE, '#modal-media-library'));
return $response;
}
}