"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/media_library/src/MediaLibraryEditorOpener.php" (26 Nov 2020, 2417 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MediaLibraryEditorOpener.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\media_library;
    4 
    5 use Drupal\Core\Access\AccessResult;
    6 use Drupal\Core\Ajax\AjaxResponse;
    7 use Drupal\Core\Entity\EntityTypeManagerInterface;
    8 use Drupal\Core\Session\AccountInterface;
    9 use Drupal\editor\Ajax\EditorDialogSave;
   10 
   11 /**
   12  * The media library opener for text editors.
   13  *
   14  * @see \Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary
   15  *
   16  * @internal
   17  *   This service is an internal part of Media Library's CKEditor integration.
   18  */
   19 class MediaLibraryEditorOpener implements MediaLibraryOpenerInterface {
   20 
   21   /**
   22    * The text format entity storage.
   23    *
   24    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   25    */
   26   protected $filterStorage;
   27 
   28   /**
   29    * The media storage.
   30    *
   31    * @var \Drupal\Core\Entity\ContentEntityStorageInterface
   32    */
   33   protected $mediaStorage;
   34 
   35   /**
   36    * The MediaLibraryEditorOpener constructor.
   37    *
   38    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   39    *   The entity type manager.
   40    */
   41   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
   42     $this->filterStorage = $entity_type_manager->getStorage('filter_format');
   43     $this->mediaStorage = $entity_type_manager->getStorage('media');
   44   }
   45 
   46   /**
   47    * {@inheritdoc}
   48    */
   49   public function checkAccess(MediaLibraryState $state, AccountInterface $account) {
   50     $filter_format_id = $state->getOpenerParameters()['filter_format_id'];
   51     $filter_format = $this->filterStorage->load($filter_format_id);
   52     if (empty($filter_format)) {
   53       return AccessResult::forbidden()
   54         ->addCacheTags(['filter_format_list'])
   55         ->setReason("The text format '$filter_format_id' could not be loaded.");
   56     }
   57     $filters = $filter_format->filters();
   58     return $filter_format->access('use', $account, TRUE)
   59       ->andIf(AccessResult::allowedIf($filters->has('media_embed') && $filters->get('media_embed')->status === TRUE));
   60   }
   61 
   62   /**
   63    * {@inheritdoc}
   64    */
   65   public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
   66     $selected_media = $this->mediaStorage->load(reset($selected_ids));
   67 
   68     $response = new AjaxResponse();
   69     $values = [
   70       'attributes' => [
   71         'data-entity-type' => 'media',
   72         'data-entity-uuid' => $selected_media->uuid(),
   73         'data-align' => 'center',
   74       ],
   75     ];
   76     $response->addCommand(new EditorDialogSave($values));
   77 
   78     return $response;
   79   }
   80 
   81 }