You are here

TwitterMediaLibraryAddForm.php in Media entity Twitter 8.2

File

src/Form/TwitterMediaLibraryAddForm.php
View source
<?php

namespace Drupal\media_entity_twitter\Form;

use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\media\MediaTypeInterface;
use Drupal\media_library\Form\AddFormBase;

/**
 * Creates a form to create Twitter media entities from within Media Library.
 */
class TwitterMediaLibraryAddForm extends AddFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'media_entity_twitter_media_library_add';
  }

  /**
   * {@inheritdoc}
   */
  protected function buildInputElement(array $form, FormStateInterface $form_state) {
    $media_type = $this
      ->getMediaType($form_state);
    $form['container'] = [
      '#type' => 'container',
      '#title' => $this
        ->t('Add @type', [
        '@type' => $media_type
          ->label(),
      ]),
    ];
    $form['container']['url'] = [
      '#type' => 'url',
      '#title' => $this
        ->getSourceFieldDefinition($media_type)
        ->getLabel(),
      '#description' => $this
        ->getSourceFieldDefinition($media_type)
        ->getDescription(),
      '#required' => TRUE,
      '#attributes' => [
        'placeholder' => 'https://',
      ],
    ];
    $form['container']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add'),
      '#button_type' => 'primary',
      '#submit' => [
        '::addButtonSubmit',
      ],
      '#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.
        // Follow along with changes in \Drupal\media_library\Form\OEmbedForm.
        'url' => Url::fromRoute('media_library.ui'),
        'options' => [
          'query' => $this
            ->getMediaLibraryState($form_state)
            ->all() + [
            FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * Submit handler for the add button.
   *
   * @param array $form
   *   The form render array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function addButtonSubmit(array $form, FormStateInterface $form_state) {
    $this
      ->processInputValues([
      $form_state
        ->getValue('url'),
    ], $form, $form_state);
  }

  /**
   * Returns the definition of the source field for a media type.
   *
   * @param \Drupal\media\MediaTypeInterface $media_type
   *   The media type to get the source definition for.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface|null
   *   The field definition.
   */
  protected function getSourceFieldDefinition(MediaTypeInterface $media_type) {
    return $media_type
      ->getSource()
      ->getSourceFieldDefinition($media_type);
  }

}

Classes

Namesort descending Description
TwitterMediaLibraryAddForm Creates a form to create Twitter media entities from within Media Library.