You are here

public function AddFormBase::buildForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::buildForm()
  2. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/media_library/src/Form/AddFormBase.php, line 132

Class

AddFormBase
Provides a base class for creating media items from within the media library.

Namespace

Drupal\media_library\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // @todo Remove the ID when we can use selectors to replace content via
  //   AJAX in https://www.drupal.org/project/drupal/issues/2821793.
  $form['#prefix'] = '<div id="media-library-add-form-wrapper">';
  $form['#suffix'] = '</div>';

  // The media library is loaded via AJAX, which means that the form action
  // URL defaults to the current URL. However, to add media, we always need to
  // submit the form to the media library URL, not whatever the current URL
  // may be.
  $form['#action'] = Url::fromRoute('media_library.ui', [], [
    'query' => $this
      ->getMediaLibraryState($form_state)
      ->all(),
  ])
    ->toString();

  // The form is posted via AJAX. When there are messages set during the
  // validation or submission of the form, the messages need to be shown to
  // the user.
  $form['status_messages'] = [
    '#type' => 'status_messages',
  ];
  $form['#attributes']['class'] = [
    'js-media-library-add-form',
  ];
  $added_media = $this
    ->getAddedMediaItems($form_state);
  if (empty($added_media)) {
    $form = $this
      ->buildInputElement($form, $form_state);
  }
  else {
    $form['#attributes']['data-input'] = 'true';

    // This deserves to be themeable, but it doesn't need to be its own "real"
    // template.
    $form['description'] = [
      '#type' => 'inline_template',
      '#template' => '<p>{{ text }}</p>',
      '#context' => [
        'text' => $this
          ->formatPlural(count($added_media), 'The media item has been created but has not yet been saved. Fill in any required fields and save to add it to the media library.', 'The media items have been created but have not yet been saved. Fill in any required fields and save to add them to the media library.'),
      ],
    ];
    $form['media'] = [
      '#pre_render' => [
        [
          $this,
          'preRenderAddedMedia',
        ],
      ],
      '#attributes' => [
        'class' => [
          // This needs to be focus-able by an AJAX response.
          // @see ::updateFormCallback()
          'js-media-library-add-form-added-media',
        ],
        'aria-label' => $this
          ->t('Added media items'),
        // Add the tabindex '-1' to allow the focus to be shifted to the added
        // media wrapper when items are added. We set focus to the container
        // because a media item does not necessarily have required fields and
        // we do not want to set focus to the remove button automatically.
        // @see ::updateFormCallback()
        'tabindex' => '-1',
      ],
    ];
    foreach ($added_media as $delta => $media) {
      $form['media'][$delta] = $this
        ->buildEntityFormElement($media, $form, $form_state, $delta);
    }
    $form['selection'] = $this
      ->buildCurrentSelectionArea($form, $form_state);
    $form['actions'] = $this
      ->buildActions($form, $form_state);
  }

  // Allow the current selection to be set in a hidden field so the selection
  // can be passed between different states of the form. This field is filled
  // via JavaScript so the default value should be empty.
  // @see Drupal.behaviors.MediaLibraryItemSelection
  $form['current_selection'] = [
    '#type' => 'hidden',
    '#default_value' => '',
    '#attributes' => [
      'class' => [
        'js-media-library-add-form-current-selection',
      ],
    ],
  ];
  return $form;
}