public function MediaDirectoriesController::mediaAdd in Media Directories 2.x
Same name and namespace in other branches
- 8 modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaAdd()
- 3.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaAdd()
New media entity add form.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request.
Return value
\Drupal\Core\Ajax\AjaxResponse An AJAX response.
1 string reference to 'MediaDirectoriesController::mediaAdd'
- 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 353
Class
- MediaDirectoriesController
- Main controller class.
Namespace
Drupal\media_directories_ui\ControllerCode
public function mediaAdd(Request $request) {
$response = new AjaxResponse();
$ui_config = $this
->config('media_directories_ui.settings');
$combined_media_types = $ui_config
->get('combined_upload_media_types', []);
$active_directory = (int) $request
->get('active_directory', MEDIA_DIRECTORY_ROOT);
$target_bundles = $request
->get('target_bundles', []);
$selection_mode = $request
->get('selection_mode');
$cardinality = $request
->get('cardinality', -1);
// Find the types combined upload should handle.
// If types are limited and these are not allowed in combined upload,
// then the tab is hidden.
$combined_media_types_diff = array_intersect($target_bundles, $combined_media_types);
if (!empty($combined_media_types_diff) && $ui_config
->get('enable_combined_upload')) {
$first_media_type = 'combined_upload';
}
else {
$first_media_type = reset($target_bundles);
}
if (count($target_bundles) > 0) {
if ($first_media_type != 'combined_upload' && $target_bundles) {
// Here we land if no file is present.
// Only list media types where the user has permission for.
foreach ($target_bundles as $delta => $bundle) {
if (!$this
->entityTypeManager()
->getAccessControlHandler('media')
->createAccess($bundle)) {
unset($target_bundles[$delta]);
}
}
$first_media_type = reset($target_bundles);
$selected_type = $request
->get('media_type', $first_media_type);
}
else {
// Here we land when a file was just picked by the user.
$selected_type = $request
->get('media_type', $first_media_type);
}
if (count($target_bundles) > 0) {
$build = [
'#theme' => 'media_directories_add',
'#selected_type' => $selected_type,
'#active_directory' => $active_directory,
'#target_bundles' => $target_bundles,
'#cardinality' => $cardinality,
'#selection_mode' => $selection_mode,
];
$response
->addCommand(new OpenModalDialogCommand($this
->t('Add media'), $build, [
'width' => '800',
]));
}
else {
$this
->messenger()
->addError($this
->t('No permission found for the creation of any media type.'));
$this
->addMessagesToResponse($response);
}
}
else {
$this
->messenger()
->addError($this
->t('No media type seems to exist. <a href=":url">Add a new media type.</a>', [
':url' => Url::fromRoute('entity.media_type.add_form')
->toString(),
]));
$this
->addMessagesToResponse($response);
}
return $response;
}