View source  
  <?php
namespace Drupal\media_directories_ui\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\media\OEmbed\ResourceException;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Drupal\media_directories_ui\MediaDirectoriesLibraryUiBuilder;
use Drupal\media_library\OpenerResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OEmbedForm extends AddMediaFormBase {
  
  protected $urlResolver;
  
  protected $resourceFetcher;
  
  public function __construct(EntityTypeManagerInterface $entity_type_manager, MediaDirectoriesLibraryUiBuilder $library_ui_builder, OpenerResolverInterface $opener_resolver, UrlResolverInterface $url_resolver, ResourceFetcherInterface $resource_fetcher) {
    parent::__construct($entity_type_manager, $library_ui_builder, $opener_resolver);
    $this->urlResolver = $url_resolver;
    $this->resourceFetcher = $resource_fetcher;
  }
  
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('media_directories_ui.ui_builder'), $container
      ->get('media_library.opener_resolver'), $container
      ->get('media.oembed.url_resolver'), $container
      ->get('media.oembed.resource_fetcher'));
  }
  
  protected function getMediaType(FormStateInterface $form_state) {
    $media_type = parent::getMediaType($form_state);
    if (!$media_type
      ->getSource() instanceof OEmbedInterface) {
      throw new \InvalidArgumentException('Can only add media types which use an oEmbed source plugin.');
    }
    return $media_type;
  }
  
  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();
    
    $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',
      ],
      
      '#ajax' => [
        'callback' => '::updateFormCallback',
        'wrapper' => 'media-library-wrapper',
      ],
      '#attributes' => [
        'class' => [
          'media-library-add-form-oembed-submit',
        ],
      ],
    ];
    return $form;
  }
  
  public function validateUrl(array &$form, FormStateInterface $form_state) {
    $url = $form_state
      ->getValue('url');
    if ($url) {
      try {
        $resource_url = $this->urlResolver
          ->getResourceUrl($url);
        $this->resourceFetcher
          ->fetchResource($resource_url);
      } catch (ResourceException $e) {
        $form_state
          ->setErrorByName('url', $e
          ->getMessage());
      }
    }
  }
  
  public function addButtonSubmit(array $form, FormStateInterface $form_state) {
    $this
      ->processInputValues([
      $form_state
        ->getValue('url'),
    ], $form, $form_state);
  }
}