You are here

public function AddMediaFormBase::buildForm in Media Directories 2.x

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

1 call to AddMediaFormBase::buildForm()
MediaEditForm::buildForm in modules/media_directories_ui/src/Form/MediaEditForm.php
Form constructor.
1 method overrides AddMediaFormBase::buildForm()
MediaEditForm::buildForm in modules/media_directories_ui/src/Form/MediaEditForm.php
Form constructor.

File

modules/media_directories_ui/src/Form/AddMediaFormBase.php, line 251

Class

AddMediaFormBase
Class AddMediaFormBase.

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>';
  $theme_name = $this->themeManager
    ->getActiveTheme()
    ->getName();
  if (in_array($theme_name, [
    'claro',
    'gin',
  ])) {
    $form['#attached']['library'][] = 'claro/media_library.theme';
  }
  else {

    // By default we add the seven styles.
    $form['#attached']['library'][] = 'seven/media_library';
  }

  // 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',
  ];

  /** @var \Drupal\media\Entity\Media[] $added_media */
  $added_media = $form_state
    ->get('media');
  $form['active_directory'] = [
    '#type' => 'hidden',
    '#value' => $this
      ->getDirectory($form_state),
  ];
  $media_type = $form_state
    ->get('media_type');
  $form['media_type'] = [
    '#type' => 'hidden',
    '#value' => is_object($media_type) ? $media_type
      ->id() : $media_type,
  ];
  $target_bundles = $this
    ->getTargetBundles($form_state);
  $form['target_bundles'] = [
    '#tree' => TRUE,
  ];
  foreach ($target_bundles as $bundle) {
    $form['target_bundles'][$bundle] = [
      '#type' => 'hidden',
      '#value' => $bundle,
    ];
  }
  $form['selection_mode'] = [
    '#type' => 'hidden',
    '#value' => $this
      ->getSelectionMode($form_state),
  ];
  $form['cardinality'] = [
    '#type' => 'hidden',
    '#value' => $this
      ->getCardinality($form_state),
  ];
  if (empty($added_media)) {
    $form['#attributes']['class'][] = 'media-library-add-form--without-input';
    $form = $this
      ->buildInputElement($form, $form_state);
  }
  else {
    $form['#attributes']['class'][] = 'media-library-add-form--with-input';
    $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']['description'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $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.'),
      '#attributes' => [
        'class' => [
          'media-library-add-form__description',
        ],
      ],
    ];
    foreach ($added_media as $delta => $media) {

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