You are here

InstagramMediaLibraryAddForm.php in Media entity Instagram 3.x

File

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

namespace Drupal\media_entity_instagram\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 Instagram media entities from within Media Library.
 */
class InstagramMediaLibraryAddForm extends AddFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'media_entity_instagram_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',
        '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
InstagramMediaLibraryAddForm Creates a form to create Instagram media entities from within Media Library.