MessageCommand example

    $state = MediaLibraryState::fromRequest($request);

    $current_selection = $form_state->getValue('media_library_select_form_selection');
    $available_slots = $state->getAvailableSlots();
    $selected_count = count(explode(',', $current_selection));
    if ($available_slots > 0 && $selected_count > $available_slots) {
      $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} */
$response = new AjaxResponse();
    $response->addCommand(new UpdateSelectionCommand($media_ids));
    $media_id_to_focus = array_pop($media_ids);
    $response->addCommand(new ReplaceCommand('#media-library-add-form-wrapper', $this->buildMediaLibraryUi($form_state)));
    $response->addCommand(new InvokeCommand("#media-library-content [value=$media_id_to_focus]", 'focus'));
    $available_slots = $this->getMediaLibraryState($form_state)->getAvailableSlots();
    if ($available_slots > 0 && $selected_count > $available_slots) {
      $warning = $this->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($warning, '#media-library-messages', ['type' => 'warning']));
    }

    return $response;
  }

  /** * Build the render array of the media library UI. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current form state. * * @return array * The render array for the media library. */


  /** * Callback for testing MessageCommand with default settings. * * @return \Drupal\Core\Ajax\AjaxResponse * The AJAX response. */
  public function makeMessageDefault() {
    $response = new AjaxResponse();
    return $response->addCommand(new MessageCommand('I am a message in the default location.'));
  }

  /** * Callback for testing MessageCommand using an alternate message location. * * @return \Drupal\Core\Ajax\AjaxResponse * The AJAX response. */
  public function makeMessageAlternate() {
    $response = new AjaxResponse();
    return $response->addCommand(new MessageCommand('I am a message in an alternate location.', '#alternate-message-container', [], FALSE));
  }
Home | Imprint | This part of the site doesn't use cookies.