public function MediaDirectoriesController::mediaMove in Media Directories 2.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaMove()
- 3.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaMove()
Move media to directory.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
Return value
\Drupal\Core\Ajax\AjaxResponse An AJAX response.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 string reference to 'MediaDirectoriesController::mediaMove'
- media_directories_ui.routing.yml in modules/
media_directories_ui/ media_directories_ui.routing.yml - modules/media_directories_ui/media_directories_ui.routing.yml
File
- modules/
media_directories_ui/ src/ Controller/ MediaDirectoriesController.php, line 485
Class
- MediaDirectoriesController
- Main controller class.
Namespace
Drupal\media_directories_ui\ControllerCode
public function mediaMove(Request $request) {
$response = new AjaxResponse();
$media_items = $request->request
->get('media_items', []);
$directory_id = (int) $request->request
->get('directory_id');
/** @var \Drupal\media\Entity\Media $media_entities */
$media_entities = $this
->entityTypeManager()
->getStorage('media')
->loadMultiple($media_items);
foreach ($media_entities as $media_entity) {
if ($media_entity
->hasField('directory')) {
if ($media_entity
->access('update')) {
$new_target_id = $directory_id === MEDIA_DIRECTORY_ROOT ? NULL : $directory_id;
if ($media_entity
->get('directory')->target_id != $new_target_id) {
$media_entity
->get('directory')
->setValue($new_target_id);
$media_entity
->save();
}
else {
$this
->messenger()
->addError($this
->t('The media is already inside the destination directory!'));
}
}
else {
$this
->messenger()
->addError($this
->t('Media %media_label of type %media_type cannot be moved due to lack of permissions.', [
'%media_label' => $media_entity
->label(),
'%media_type' => $media_entity
->bundle(),
]));
}
}
}
$this
->addMessagesToResponse($response);
$response
->addCommand(new LoadDirectoryContent());
return $response;
}