You are here

public function MediaEditForm::buildForm in Media Directories 3.x

Same name and namespace in other branches
  1. 8 modules/media_directories_ui/src/Form/MediaEditForm.php \Drupal\media_directories_ui\Form\MediaEditForm::buildForm()
  2. 2.x modules/media_directories_ui/src/Form/MediaEditForm.php \Drupal\media_directories_ui\Form\MediaEditForm::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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides FormInterface::buildForm

File

modules/media_directories_ui/src/Form/MediaEditForm.php, line 116

Class

MediaEditForm
Class MediaEditForm

Namespace

Drupal\media_directories_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['#prefix'] = '<div id="media-library-add-form-wrapper" class="media-library-add-form-wrapper">';
  $form['#suffix'] = '</div>';
  $form['#attached']['library'][] = 'media_library/style';

  // 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'] = [
    'media-library-add-form',
    'js-media-library-add-form',
    'media-library-add-form--with-input',
  ];
  $form['active_directory'] = [
    '#type' => 'hidden',
    '#value' => $form_state
      ->get('active_directory'),
  ];

  /** @var \Drupal\media\Entity\Media[] $selected_media */
  $selected_media = $form_state
    ->get('media');
  $form['media'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'media-library-add-form__added-media',
      ],
      'aria-label' => $this
        ->t('Added media items'),
      'role' => 'list',
      // 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',
    ],
  ];
  $form['media_items'] = [
    '#tree' => TRUE,
  ];
  foreach ($selected_media as $delta => $media) {

    // $media->set('directory', $this->directoryId);
    $form['media'][$delta] = $this
      ->buildEntityFormElement($media, $form, $form_state, $delta);
    $form['media_items'][$delta] = [
      '#type' => 'hidden',
      '#value' => $media
        ->id(),
    ];
  }
  $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;
}