You are here

protected function OEmbedForm::buildInputElement in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Form/OEmbedForm.php \Drupal\media_library\Form\OEmbedForm::buildInputElement()
  2. 10 core/modules/media_library/src/Form/OEmbedForm.php \Drupal\media_library\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

core/modules/media_library/src/Form/OEmbedForm.php, line 97

Class

OEmbedForm
Creates a form to create media entities from oEmbed URLs.

Namespace

Drupal\media_library\Form

Code

protected function buildInputElement(array $form, FormStateInterface $form_state) {
  $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',
  ];
  $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://',
    ],
  ];
  $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_library.ui'),
      'options' => [
        'query' => $this
          ->getMediaLibraryState($form_state)
          ->all() + [
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ],
  ];
  return $form;
}