function OEmbedForm::buildInputElement

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

Overrides AddFormBase::buildInputElement

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.