You are here

public function MediaDirectoriesController::directoryMove in Media Directories 2.x

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

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')) {

    /** @var \Drupal\taxonomy\Entity\Term $new_parent_directory */
    $new_parent_directory = $this
      ->entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load($to_directory_id);
    if ($to_directory_id !== MEDIA_DIRECTORY_ROOT && $this->mediaDirectoriesUiHelper
      ->termIsAnAnchestorOf($new_parent_directory, $directory)) {
      $this
        ->messenger()
        ->addError($this
        ->t("You cannot move a directory to one of it's sub-directories!"));
      $this
        ->addMessagesToResponse($response);
    }
    elseif ($this->mediaDirectoriesUiHelper
      ->termIsAChildOf($directory, $new_parent_directory)) {
      $this
        ->messenger()
        ->addError($this
        ->t('The directory is already a child of the destination directory!'));
      $this
        ->addMessagesToResponse($response);
    }
    else {
      $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;
}