You are here

protected function OEmbedForm::buildInputElement in Media Directories 3.x

Same name and namespace in other branches
  1. 8 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()

Builds the element for submitting source field value(s).

The input element needs to have a submit handler to create media items from the user input and store them in the form state using ::processInputValues().

Parameters

array $form: The complete form.

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

Return value

array The complete form, with the element added.

Overrides AddFormBase::buildInputElement

See also

::processInputValues()

File

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

Class

OEmbedForm

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',
    ],
    '#attributes' => [
      'class' => [
        'media-library-add-form-oembed-submit',
      ],
    ],
  ];
  return $form;
}