You are here

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

Media entity delete confirmation form.

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

1 string reference to 'MediaDirectoriesController::mediaDelete'
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 532

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

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);
  foreach ($media_entities as $mid => $media_entity) {
    if (!$media_entity
      ->access('delete')) {
      $this
        ->messenger()
        ->addError($this
        ->t('Media %media_label of type %media_type cannot be deleted due to lack of permissions.', [
        '%media_label' => $media_entity
          ->label(),
        '%media_type' => $media_entity
          ->bundle(),
      ]));
      unset($media_entities[$mid]);
    }
  }
  if (count($media_entities) > 0) {
    $context = [
      'media_items' => $media_entities,
    ];
    $form = $this->formBuilder
      ->getForm(MediaDeleteForm::class, $context);
    $this
      ->addMessagesToForm($form);
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Delete media'), $form, [
      'width' => '500',
    ]));
  }
  else {
    $this
      ->addMessagesToResponse($response);
  }
  return $response;
}