You are here

class MediaDirectoriesController in Media Directories 3.x

Same name and namespace in other branches
  1. 8 modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController
  2. 2.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController

Hierarchy

Expanded class hierarchy of MediaDirectoriesController

File

modules/media_directories_ui/src/Controller/MediaDirectoriesController.php, line 24

Namespace

Drupal\media_directories_ui\Controller
View source
class MediaDirectoriesController extends ControllerBase {

  /**
   * @var \Drupal\taxonomy\TermStorage
   */
  protected $termStorage;

  /**
   * @var FormBuilder
   */
  protected $formBuilder;

  /**
   * @var RendererInterface
   */
  protected $renderer;

  /**
   * The vocabulary id to use.
   *
   * @var string
   */
  protected $vocabulary_id;

  /**
   * MediaDirectoriesController constructor.
   *
   * @param \Drupal\Core\Form\FormBuilder $formBuilder
   * @param \Drupal\Core\Render\RendererInterface $renderer
   */
  public function __construct(FormBuilder $formBuilder, RendererInterface $renderer) {
    $this->formBuilder = $formBuilder;
    $this->renderer = $renderer;
    $config = $this
      ->config('media_directories.settings');
    $this->vocabulary_id = $config
      ->get('directory_taxonomy');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('form_builder'), $container
      ->get('renderer'));
  }

  /**
   * Return directory tree as JSON.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function directoryTree() {

    /** @var \Drupal\media_directories_ui\MediaDirectoriesTreeBuilder $treeBuilder */
    $treeBuilder = \Drupal::service('media_directories_ui.tree_builder');
    $tree = $treeBuilder
      ->tree();
    return new JsonResponse($tree);
  }

  /**
   * Create new directory.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  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');
    $directory = Term::create([
      'name' => $name,
      'vid' => $this->vocabulary_id,
      'parent' => [
        $directory_id,
      ],
    ]);
    $directory
      ->save();
    $data = [
      'id' => 'dir-' . $directory
        ->id(),
      'a_attr' => (object) [
        'data-tid' => $directory
          ->id(),
      ],
      'text' => $directory
        ->getName(),
    ];
    return new JsonResponse($data);
  }

  /**
   * Rename directory.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function directoryRename(Request $request) {
    $directory_id = (int) $request->request
      ->get('directory_id');
    $new_name = $request->request
      ->get('directory_new_name');
    $directory = Term::load($directory_id);
    $directory
      ->setName($new_name);
    $directory
      ->save();
    $response = new AjaxResponse();
    return $response;
  }

  /**
   * Move directory to different directory.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
   */
  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 Term $directory */
    $directory = $this
      ->entityTypeManager()
      ->getStorage('taxonomy_term')
      ->load($move_directory_id);
    $directory
      ->get('parent')
      ->setValue($to_directory_id === MEDIA_DIRECTORY_ROOT ? NULL : $to_directory_id);
    $directory
      ->save();
    return $response;
  }

  /**
   * Delete directory.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   */
  public function directoryDelete(Request $request) {
    $response = new AjaxResponse();
    $directory_id = (int) $request->request
      ->get('directory_id');
    $target_bundles = $request->request
      ->get('target_bundles');
    $directory = Term::load($directory_id);
    if ($directory === NULL) {
      return $response;
    }
    $context = [
      'directory' => $directory,
      'target_bundles' => $target_bundles,
    ];
    $form = $this->formBuilder
      ->getForm(DirectoryDeleteForm::class, $context);
    $response
      ->addCommand(new OpenDialogCommand('#media-dialog', $this
      ->t('Delete directory @name', [
      '@name' => $directory
        ->getName(),
    ]), $form, [
      'width' => '500',
      'modal' => TRUE,
    ]));
    return $response;
  }

  /**
   * New media entity add form.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function mediaAdd(Request $request) {
    $response = new AjaxResponse();
    $state = MediaDirectoriesLibraryState::fromRequest($request);

    /** @var \Drupal\media_directories_ui\MediaDirectoriesLibraryUiBuilder $library_ui */
    $library_ui = \Drupal::service('media_directories_ui.ui_builder');
    $response
      ->addCommand(new ReplaceCommand('#media-library-wrapper', $library_ui
      ->buildUploadUi($state)));
    return $response;
  }

  /**
   * Media entity edit form.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Form\EnforcedResponseException
   * @throws \Drupal\Core\Form\FormAjaxException
   */
  public function mediaEdit(Request $request) {
    $response = new AjaxResponse();
    $media_items = $request->request
      ->get('media_items', []);
    $active_directory = (int) $request->request
      ->get('active_directory', MEDIA_DIRECTORY_ROOT);
    $media_entities = $this
      ->entityTypeManager()
      ->getStorage('media')
      ->loadMultiple($media_items);
    $form_state = new FormState();
    $form_state
      ->set('media', $media_entities);
    $form_state
      ->set('active_directory', $active_directory);
    $media_form = $this
      ->formBuilder()
      ->buildForm(MediaEditForm::class, $form_state);
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Edit media'), $media_form, [
      'width' => '800',
    ]));
    return $response;
  }

  /**
   * Move media to directory.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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')) {
        $media_entity
          ->get('directory')
          ->setValue($directory_id === MEDIA_DIRECTORY_ROOT ? NULL : $directory_id);
        $media_entity
          ->save();
      }
    }
    $response
      ->addCommand(new InvokeCommand('.js-media-library-view', 'trigger', [
      'RefreshView',
    ]));
    return $response;
  }

  /**
   * Media entity delete confirmation form.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function mediaDelete(Request $request) {
    $response = new AjaxResponse();
    $media_items = $request->request
      ->get('media_items', []);
    if (empty($media_items)) {
      return $response;
    }
    $media_entities = $this
      ->entityTypeManager()
      ->getStorage('media')
      ->loadMultiple($media_items);
    $context = [
      'media_items' => $media_entities,
    ];
    $form = $this->formBuilder
      ->getForm(MediaDeleteForm::class, $context);
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Delete media'), $form, [
      'width' => '500',
    ]));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MediaDirectoriesController::$formBuilder protected property Overrides ControllerBase::$formBuilder
MediaDirectoriesController::$renderer protected property
MediaDirectoriesController::$termStorage protected property
MediaDirectoriesController::$vocabulary_id protected property The vocabulary id to use.
MediaDirectoriesController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
MediaDirectoriesController::directoryAdd public function Create new directory.
MediaDirectoriesController::directoryDelete public function Delete directory.
MediaDirectoriesController::directoryMove public function Move directory to different directory.
MediaDirectoriesController::directoryRename public function Rename directory.
MediaDirectoriesController::directoryTree public function Return directory tree as JSON.
MediaDirectoriesController::mediaAdd public function New media entity add form.
MediaDirectoriesController::mediaDelete public function Media entity delete confirmation form.
MediaDirectoriesController::mediaEdit public function Media entity edit form.
MediaDirectoriesController::mediaMove public function Move media to directory.
MediaDirectoriesController::__construct public function MediaDirectoriesController constructor.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.