getSelectionResponse example

$response = new AjaxResponse();
      $error = \Drupal::translation()->formatPlural($selected_count - $available_slots, 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count item from the selection.', 'There are currently @total items selected. The maximum number of items for the field is @max. Please remove @count items from the selection.', [
        '@total' => $selected_count,
        '@max' => $available_slots,
      ]);
      $response->addCommand(new MessageCommand($error, '#media-library-messages', ['type' => 'error']));
      return $response;
    }

    return \Drupal::service('media_library.opener_resolver')
      ->get($state)
      ->getSelectionResponse($state$selected_ids)
      ->addCommand(new CloseDialogCommand());
  }

  /** * {@inheritdoc} */
  public function viewsFormValidate(array &$form, FormStateInterface $form_state) {
    $selected = array_filter($form_state->getValue($this->options['id']));
    if (empty($selected)) {
      $form_state->setErrorByName('', $this->t('No items selected.'));
    }
  }
// Allow the opener service to respond to the selection.     $state = $this->getMediaLibraryState($form_state);
    $selected_count = $this->getSelectedMediaItemCount($media_ids$form_state);

    $available_slots = $this->getMediaLibraryState($form_state)->getAvailableSlots();
    if ($available_slots > 0 && $selected_count > $available_slots) {
      // Return to library where we display a warning about the overage.       return $this->updateLibrary($form$form_state);
    }

    return $this->openerResolver->get($state)
      ->getSelectionResponse($state$media_ids)
      ->addCommand(new CloseDialogCommand());
  }

  /** * Get the number of selected media. * * @param array $media_ids * Array with the media IDs. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current form state. * * @return int * The number of media currently selected. */
Home | Imprint | This part of the site doesn't use cookies.