View source
<?php
namespace Drupal\video_embed_wysiwyg\Form;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\image\Entity\ImageStyle;
use Drupal\video_embed_field\Plugin\Field\FieldFormatter\Video;
use Drupal\video_embed_field\ProviderManager;
use Drupal\video_embed_field\ProviderPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class VideoEmbedDialog extends FormBase {
protected $providerManager;
protected $renderer;
public function __construct(ProviderManager $provider_manager, RendererInterface $renderer) {
$this->providerManager = $provider_manager;
$this->render = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('video_embed_field.provider_manager'), $container
->get('renderer'));
}
public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
$form['#prefix'] = '<div id="video-embed-dialog-form">';
$form['#suffix'] = '</div>';
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
$form['video_url'] = [
'#type' => 'textfield',
'#title' => $this
->t('Video URL'),
'#required' => TRUE,
'#default_value' => $this
->getUserInput($form_state, 'video_url'),
];
$settings = $this
->getUserInput($form_state, 'settings');
if (empty($settings) && ($editor = Editor::load($filter_format
->id()))) {
$editor_settings = $editor
->getSettings();
$plugin_settings = NestedArray::getValue($editor_settings, [
'plugins',
'video_embed',
'defaults',
'children',
]);
$settings = $plugin_settings ? $plugin_settings : [];
}
$form['settings'] = Video::mockInstance($settings)
->settingsForm([], new FormState());
$form['settings']['#type'] = 'fieldset';
$form['settings']['#title'] = $this
->t('Settings');
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => [],
'#ajax' => [
'callback' => '::ajaxSubmit',
'event' => 'click',
'wrapper' => 'video-embed-dialog-form',
],
];
return $form;
}
protected function getUserInput(FormStateInterface $form_state, $key) {
return isset($form_state
->getUserInput()['editor_object'][$key]) ? $form_state
->getUserInput()['editor_object'][$key] : '';
}
protected function getClientValues(FormStateInterface $form_state, ProviderPluginInterface $provider) {
$video_formatter_settings = Video::defaultSettings();
foreach ($video_formatter_settings as $key => $default) {
$video_formatter_settings[$key] = $form_state
->getValue($key);
}
$provider
->downloadThumbnail();
$thumbnail_preview = ImageStyle::load('video_embed_wysiwyg_preview')
->buildUrl($provider
->getLocalThumbnailUri());
$thumbnail_preview_parts = parse_url($thumbnail_preview);
return [
'preview_thumbnail' => $thumbnail_preview_parts['path'] . (!empty($thumbnail_preview_parts['query']) ? '?' : '') . $thumbnail_preview_parts['query'],
'video_url' => $form_state
->getValue('video_url'),
'settings' => $video_formatter_settings,
'settings_summary' => Video::mockInstance($video_formatter_settings)
->settingsSummary(),
];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$provider = $this
->getProvider($form_state
->getValue('video_url'));
if (FALSE == $provider) {
$form_state
->setError($form['video_url'], $this
->t('Could not find a video provider to handle the given URL.'));
return;
}
}
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if (!$form_state
->getErrors()) {
$provider = $this
->getProvider($form_state
->getValue('video_url'));
$response
->addCommand(new EditorDialogSave($this
->getClientValues($form_state, $provider)));
$response
->addCommand(new CloseModalDialogCommand());
}
else {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new HtmlCommand(NULL, $form));
}
return $response;
}
protected function getProvider($input) {
return $this->providerManager
->loadProviderFromInput($input);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function getFormId() {
return 'video_embed_dialog';
}
}