You are here

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

Create new directory.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response.

Throws

\Drupal\Core\Entity\EntityStorageException

1 string reference to 'MediaDirectoriesController::directoryAdd'
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 203

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

public function directoryAdd(Request $request) {
  $directory_id = (int) $request->request
    ->get('parent_id');
  $directory_id = $directory_id === MEDIA_DIRECTORY_ROOT ? 0 : $directory_id;
  $name = $request->request
    ->get('name');
  $current_language = $this->languageManager
    ->getCurrentLanguage();
  $directory = Term::create([
    'name' => $name,
    'vid' => $this->vocabularyId,
    'parent' => [
      $directory_id,
    ],
    'langcode' => $current_language
      ->getId(),
  ]);
  $data = [];
  if ($directory
    ->access('create')) {
    $directory
      ->save();
    $data = [
      'id' => 'dir-' . $directory
        ->id(),
      'a_attr' => (object) [
        'data-tid' => $directory
          ->id(),
      ],
      'text' => $directory
        ->getName(),
    ];
  }
  return new JsonResponse($data);
}