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;
class TwitterMediaLibraryAddForm extends AddFormBase {
public function getFormId() {
return 'media_entity_twitter_media_library_add';
}
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;
}
public function addButtonSubmit(array $form, FormStateInterface $form_state) {
$this
->processInputValues([
$form_state
->getValue('url'),
], $form, $form_state);
}
protected function getSourceFieldDefinition(MediaTypeInterface $media_type) {
return $media_type
->getSource()
->getSourceFieldDefinition($media_type);
}
}