You are here

class MediaLibraryEditorOpener in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/MediaLibraryEditorOpener.php \Drupal\media_library\MediaLibraryEditorOpener
  2. 10 core/modules/media_library/src/MediaLibraryEditorOpener.php \Drupal\media_library\MediaLibraryEditorOpener

The media library opener for text editors.

@internal This service is an internal part of Media Library's CKEditor integration.

Hierarchy

Expanded class hierarchy of MediaLibraryEditorOpener

See also

\Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary

1 string reference to 'MediaLibraryEditorOpener'
media_library.services.yml in core/modules/media_library/media_library.services.yml
core/modules/media_library/media_library.services.yml
1 service uses MediaLibraryEditorOpener
media_library.opener.editor in core/modules/media_library/media_library.services.yml
Drupal\media_library\MediaLibraryEditorOpener

File

core/modules/media_library/src/MediaLibraryEditorOpener.php, line 19

Namespace

Drupal\media_library
View source
class MediaLibraryEditorOpener implements MediaLibraryOpenerInterface {

  /**
   * The text format entity storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $filterStorage;

  /**
   * The media storage.
   *
   * @var \Drupal\Core\Entity\ContentEntityStorageInterface
   */
  protected $mediaStorage;

  /**
   * The MediaLibraryEditorOpener constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->filterStorage = $entity_type_manager
      ->getStorage('filter_format');
    $this->mediaStorage = $entity_type_manager
      ->getStorage('media');
  }

  /**
   * {@inheritdoc}
   */
  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('media_embed') && $filters
      ->get('media_embed')->status === TRUE));
  }

  /**
   * {@inheritdoc}
   */
  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-uuid' => $selected_media
          ->uuid(),
        'data-align' => 'center',
      ],
    ];
    $response
      ->addCommand(new EditorDialogSave($values));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaLibraryEditorOpener::$filterStorage protected property The text format entity storage.
MediaLibraryEditorOpener::$mediaStorage protected property The media storage.
MediaLibraryEditorOpener::checkAccess public function Checks media library access. Overrides MediaLibraryOpenerInterface::checkAccess
MediaLibraryEditorOpener::getSelectionResponse public function Generates a response after selecting media items in the media library. Overrides MediaLibraryOpenerInterface::getSelectionResponse
MediaLibraryEditorOpener::__construct public function The MediaLibraryEditorOpener constructor.