You are here

protected function OEmbedForm::buildInputElement in Media Directories 8

Same name and namespace in other branches
  1. 3.x modules/media_directories_ui/src/Form/OEmbedForm.php \Drupal\media_directories_ui\Form\OEmbedForm::buildInputElement()
  2. 2.x modules/media_directories_ui/src/Form/OEmbedForm.php \Drupal\media_directories_ui\Form\OEmbedForm::buildInputElement()

Inheriting classes need to build the desired input element.

Overrides AddMediaFormBase::buildInputElement

File

modules/media_directories_ui/src/Form/OEmbedForm.php, line 87

Class

OEmbedForm
A form to add remote content using OEmbed resources.

Namespace

Drupal\media_directories_ui\Form

Code

protected function buildInputElement(array $form, FormStateInterface $form_state) {
  $form['#attributes']['class'][] = 'media-library-add-form--oembed';
  $media_type = $this
    ->getMediaType($form_state);
  $providers = $media_type
    ->getSource()
    ->getProviders();

  // Add a container to group the input elements for styling purposes.
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'media-library-add-form__input-wrapper',
      ],
    ],
  ];
  $form['container']['url'] = [
    '#type' => 'url',
    '#title' => $this
      ->t('Add @type via URL', [
      '@type' => $this
        ->getMediaType($form_state)
        ->label(),
    ]),
    '#description' => $this
      ->t('Allowed providers: @providers.', [
      '@providers' => implode(', ', $providers),
    ]),
    '#required' => TRUE,
    '#attributes' => [
      'placeholder' => 'https://',
      'class' => [
        'media-library-add-form-oembed-url',
      ],
    ],
  ];
  $form['container']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add'),
    '#button_type' => 'primary',
    '#validate' => [
      '::validateUrl',
    ],
    '#submit' => [
      '::addButtonSubmit',
    ],
    // @todo Move validation in https://www.drupal.org/node/2988215
    '#ajax' => [
      'callback' => '::updateFormCallback',
      'wrapper' => 'media-library-wrapper',
      // Add a fixed URL to post the form since AJAX forms are automatically
      // posted to <current> instead of $form['#action'].
      // @todo Remove when https://www.drupal.org/project/drupal/issues/2504115
      //   is fixed.
      'url' => Url::fromRoute('media_directories_ui.media.add'),
      'options' => [
        'query' => [
          'media_type' => $form_state
            ->get('media_type') ? $form_state
            ->get('media_type')
            ->id() : $form_state
            ->get('selected_type'),
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ],
    '#attributes' => [
      'class' => [
        'media-library-add-form-oembed-submit',
      ],
    ],
  ];
  return $form;
}