You are here

public function MediaDirectoriesController::mediaMove in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaMove()
  2. 2.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 451

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

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')) {
        $media_entity
          ->get('directory')
          ->setValue($directory_id === MEDIA_DIRECTORY_ROOT ? NULL : $directory_id);
        $media_entity
          ->save();
      }
      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;
}