You are here

class MediaLibraryFormElementOpener in Media Library Form API Element 8

Same name and namespace in other branches
  1. 2.x src/MediaLibraryFormElementOpener.php \Drupal\media_library_form_element\MediaLibraryFormElementOpener

The media library opener for form elements.

Hierarchy

Expanded class hierarchy of MediaLibraryFormElementOpener

1 string reference to 'MediaLibraryFormElementOpener'
media_library_form_element.services.yml in ./media_library_form_element.services.yml
media_library_form_element.services.yml
1 service uses MediaLibraryFormElementOpener
media_library.opener.form_element in ./media_library_form_element.services.yml
Drupal\media_library_form_element\MediaLibraryFormElementOpener

File

src/MediaLibraryFormElementOpener.php, line 18

Namespace

Drupal\media_library_form_element
View source
class MediaLibraryFormElementOpener implements MediaLibraryOpenerInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * MediaLibraryFormElementOpener constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  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());
  }

  /**
   * {@inheritdoc}
   */
  public function getSelectionResponse(MediaLibraryState $state, array $selected_ids) {
    $response = new AjaxResponse();
    $parameters = $state
      ->getOpenerParameters();

    // Create a comma-separated list of media IDs, insert them in the hidden
    // field of the widget, and trigger the field update via the hidden submit
    // button.
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaLibraryFormElementOpener::$entityTypeManager protected property The entity type manager.
MediaLibraryFormElementOpener::checkAccess public function Checks media library access. Overrides MediaLibraryOpenerInterface::checkAccess
MediaLibraryFormElementOpener::getSelectionResponse public function Generates a response after selecting media items in the media library. Overrides MediaLibraryOpenerInterface::getSelectionResponse
MediaLibraryFormElementOpener::__construct public function MediaLibraryFormElementOpener constructor.