You are here

public function MediaDirectoriesController::directoryMove 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::directoryMove()
  2. 2.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::directoryMove()

Move directory to different 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

\Drupal\Core\Entity\EntityStorageException

\Drupal\Core\TypedData\Exception\ReadOnlyException

1 string reference to 'MediaDirectoriesController::directoryMove'
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 255

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

public function directoryMove(Request $request) {
  $response = new AjaxResponse();
  $move_directory_id = (int) $request->request
    ->get('move_directory_id');
  $to_directory_id = (int) $request->request
    ->get('directory_id');
  $response
    ->addCommand(new RefreshDirectoryTree($to_directory_id));

  // This shouldn't happen, but might cause issues when it does.
  if ($move_directory_id === $to_directory_id) {
    return $response;
  }

  /** @var \Drupal\taxonomy\Entity\Term $directory */
  $directory = $this
    ->entityTypeManager()
    ->getStorage('taxonomy_term')
    ->load($move_directory_id);
  if ($directory
    ->access('update')) {
    $directory
      ->get('parent')
      ->setValue($to_directory_id === MEDIA_DIRECTORY_ROOT ? NULL : $to_directory_id);
    $directory
      ->save();
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('No permission found to move this directory.'));
    $this
      ->addMessagesToResponse($response);
  }
  return $response;
}