You are here

public function MediaDirectoriesController::mediaEdit in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaEdit()
  2. 2.x modules/media_directories_ui/src/Controller/MediaDirectoriesController.php \Drupal\media_directories_ui\Controller\MediaDirectoriesController::mediaEdit()

Media entity edit 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

\Drupal\Core\Form\EnforcedResponseException

\Drupal\Core\Form\FormAjaxException

1 string reference to 'MediaDirectoriesController::mediaEdit'
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 396

Class

MediaDirectoriesController
Main controller class.

Namespace

Drupal\media_directories_ui\Controller

Code

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);
  if (count($media_items) == 0) {

    // We're probably in the AJAX Form-Callback of the MediaEditForm (inheriting AddMediaFormBase)
    $media_items = $request->request
      ->get('media', []);
    $media_ids = [];
    foreach ($media_items as $mid => $data) {

      // The nested array needs to be removed as nothing will not be loaded like this.
      $media_ids[$mid] = $mid;
    }
    $media_items = $media_ids;
  }
  $media_entities = $this
    ->entityTypeManager()
    ->getStorage('media')
    ->loadMultiple($media_items);
  foreach ($media_entities as $mid => $media_entity) {
    if (!$media_entity
      ->access('update')) {
      $this
        ->messenger()
        ->addError($this
        ->t('Media %media_label of type %media_type cannot be edited due to lack of permissions.', [
        '%media_label' => $media_entity
          ->label(),
        '%media_type' => $media_entity
          ->bundle(),
      ]));
      unset($media_entities[$mid]);
    }
  }
  if (count($media_entities) > 0) {
    $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);
    $this
      ->addMessagesToForm($media_form);
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Edit media'), $media_form, [
      'width' => '800',
    ]));
  }
  else {
    $this
      ->addMessagesToResponse($response);
  }
  return $response;
}