You are here

class LinkitMediaLibraryEditorOpener in Linkit Media Library 1.0.x

The media library opener for text editors.

Hierarchy

Expanded class hierarchy of LinkitMediaLibraryEditorOpener

See also

\Drupal\media_library\Plugin\CKEditorPlugin\LinkitMediaLibrary

1 string reference to 'LinkitMediaLibraryEditorOpener'
linkit_media_library.services.yml in ./linkit_media_library.services.yml
linkit_media_library.services.yml
1 service uses LinkitMediaLibraryEditorOpener
linkit_media_library.opener.editor in ./linkit_media_library.services.yml
Drupal\linkit_media_library\LinkitMediaLibraryEditorOpener

File

src/LinkitMediaLibraryEditorOpener.php, line 18

Namespace

Drupal\linkit_media_library
View source
class LinkitMediaLibraryEditorOpener 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'];

    /** @var \Drupal\filter\FilterFormatInterface $filter_format */
    $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.");
    }

    /** @var Drupal\filter\FilterPluginCollection $filters */
    $filters = $filter_format
      ->filters();
    return $filter_format
      ->access('use', $account, TRUE)
      ->andIf(AccessResult::allowedIf($filters
      ->has('linkit') && $filters
      ->get('linkit')->status === TRUE));
  }

  /**
   * {@inheritdoc}
   */
  public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {

    /** @var \Drupal\media\MediaInterface $selected_media */
    $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;
  }

}

Members

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