class MediaLibraryFormElementOpener in Media Library Form API Element 8
Same name and namespace in other branches
- 2.x src/MediaLibraryFormElementOpener.php \Drupal\media_library_form_element\MediaLibraryFormElementOpener
The media library opener for form elements.
Hierarchy
- class \Drupal\media_library_form_element\MediaLibraryFormElementOpener implements MediaLibraryOpenerInterface
Expanded class hierarchy of MediaLibraryFormElementOpener
1 string reference to 'MediaLibraryFormElementOpener'
1 service uses MediaLibraryFormElementOpener
File
- src/
MediaLibraryFormElementOpener.php, line 18
Namespace
Drupal\media_library_form_elementView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaLibraryFormElementOpener:: |
protected | property | The entity type manager. | |
MediaLibraryFormElementOpener:: |
public | function |
Checks media library access. Overrides MediaLibraryOpenerInterface:: |
|
MediaLibraryFormElementOpener:: |
public | function |
Generates a response after selecting media items in the media library. Overrides MediaLibraryOpenerInterface:: |
|
MediaLibraryFormElementOpener:: |
public | function | MediaLibraryFormElementOpener constructor. |