LinkitMediaLibraryEditorOpener.php in Linkit Media Library 1.0.x
File
src/LinkitMediaLibraryEditorOpener.php
View source
<?php
namespace Drupal\linkit_media_library;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\media_library\MediaLibraryOpenerInterface;
use Drupal\media_library\MediaLibraryState;
class LinkitMediaLibraryEditorOpener implements MediaLibraryOpenerInterface {
protected $filterStorage;
protected $mediaStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->filterStorage = $entity_type_manager
->getStorage('filter_format');
$this->mediaStorage = $entity_type_manager
->getStorage('media');
}
public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
$filter_format_id = $state
->getOpenerParameters()['filter_format_id'];
$filter_format = $this->filterStorage
->load($filter_format_id);
if (empty($filter_format)) {
return AccessResult::forbidden()
->addCacheTags([
'filter_format_list',
])
->setReason("The text format '{$filter_format_id}' could not be loaded.");
}
$filters = $filter_format
->filters();
return $filter_format
->access('use', $account, TRUE)
->andIf(AccessResult::allowedIf($filters
->has('linkit') && $filters
->get('linkit')->status === TRUE));
}
public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
$selected_media = $this->mediaStorage
->load(reset($selected_ids));
$response = new AjaxResponse();
$values = [
'attributes' => [
'data-entity-type' => 'media',
'data-entity-substitution' => 'media',
'data-entity-uuid' => $selected_media
->uuid(),
'href' => '/media/' . $selected_media
->id(),
'target' => '_blank',
],
];
$response
->addCommand(new EditorDialogSave($values));
return $response;
}
}