class MediaDirectoriesController in Media Directories 3.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController
- 2.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\media_directories_ui\Controller\MediaDirectoriesController
Expanded class hierarchy of MediaDirectoriesController
File
- modules/
media_directories_ui/ src/ Controller/ MediaDirectoriesController.php, line 24
Namespace
Drupal\media_directories_ui\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MediaDirectoriesController:: |
protected | property |
Overrides ControllerBase:: |
|
MediaDirectoriesController:: |
protected | property | ||
MediaDirectoriesController:: |
protected | property | ||
MediaDirectoriesController:: |
protected | property | The vocabulary id to use. | |
MediaDirectoriesController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
MediaDirectoriesController:: |
public | function | Create new directory. | |
MediaDirectoriesController:: |
public | function | Delete directory. | |
MediaDirectoriesController:: |
public | function | Move directory to different directory. | |
MediaDirectoriesController:: |
public | function | Rename directory. | |
MediaDirectoriesController:: |
public | function | Return directory tree as JSON. | |
MediaDirectoriesController:: |
public | function | New media entity add form. | |
MediaDirectoriesController:: |
public | function | Media entity delete confirmation form. | |
MediaDirectoriesController:: |
public | function | Media entity edit form. | |
MediaDirectoriesController:: |
public | function | Move media to directory. | |
MediaDirectoriesController:: |
public | function | MediaDirectoriesController constructor. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |