View source
<?php
namespace Drupal\embed\Form;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\embed\EmbedType\EmbedTypeManager;
use Drupal\embed\Entity\EmbedButton;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EmbedButtonForm extends EntityForm {
protected $entityTypeManager;
protected $embedTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EmbedTypeManager $embed_type_manager, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->embedTypeManager = $embed_type_manager;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('plugin.manager.embed.type'), $container
->get('config.factory'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$button = $this->entity;
$form_state
->setTemporaryValue('embed_button', $button);
$form['label'] = [
'#title' => $this
->t('Label'),
'#type' => 'textfield',
'#default_value' => $button
->label(),
'#description' => $this
->t('The human-readable name of this embed button. This text will be displayed when the user hovers over the CKEditor button. This name must be unique.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $button
->id(),
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#disabled' => !$button
->isNew(),
'#machine_name' => [
'exists' => [
EmbedButton::class,
'load',
],
],
'#description' => $this
->t('A unique machine-readable name for this embed button. It must only contain lowercase letters, numbers, and underscores.'),
];
$form['type_id'] = [
'#type' => 'select',
'#title' => $this
->t('Embed type'),
'#options' => $this->embedTypeManager
->getDefinitionOptions(),
'#default_value' => $button
->getTypeId(),
'#required' => TRUE,
'#ajax' => [
'callback' => '::updateTypeSettings',
'effect' => 'fade',
],
'#disabled' => !$button
->isNew(),
];
if (empty($form['type_id']['#options'])) {
$this
->messenger()
->addWarning($this
->t('No embed types found.'));
}
$form['type_settings'] = [
'#type' => 'container',
'#tree' => TRUE,
'#prefix' => '<div id="embed-type-settings-wrapper">',
'#suffix' => '</div>',
];
try {
if ($plugin = $button
->getTypePlugin()) {
$form['type_settings'] = $plugin
->buildConfigurationForm($form['type_settings'], $form_state);
}
} catch (PluginNotFoundException $exception) {
$this
->messenger()
->addError($exception
->getMessage());
watchdog_exception('embed', $exception);
$form['type_id']['#disabled'] = FALSE;
}
$config = $this
->config('embed.settings');
$upload_location = $config
->get('file_scheme') . '://' . $config
->get('upload_directory') . '/';
$form['icon_file'] = [
'#type' => 'managed_file',
'#title' => $this
->t('Button icon'),
'#upload_location' => $upload_location,
'#upload_validators' => [
'file_validate_extensions' => [
'gif png jpg jpeg svg',
],
'file_validate_image_resolution' => [
'32x32',
'16x16',
],
],
];
if (!$button
->isNew()) {
$form['icon_reset'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Reset to default icon'),
'#access' => $button
->getIconUrl() !== $button
->getTypePlugin()
->getDefaultIconUrl(),
];
$form['icon_preview'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Current icon preview'),
];
$form['icon_preview']['image'] = [
'#theme' => 'image',
'#uri' => $button
->getIconUrl(),
'#alt' => $this
->t('Preview of @label button icon', [
'@label' => $button
->label(),
]),
];
if ($this->moduleHandler
->moduleExists('ckeditor')) {
$form['icon_preview']['image']['#prefix'] = '<div data-toolbar="active" role="form" class="ckeditor-toolbar ckeditor-toolbar-active clearfix"><ul class="ckeditor-active-toolbar-configuration" role="presentation" aria-label="CKEditor toolbar and button configuration."><li class="ckeditor-row" role="group" aria-labelledby="ckeditor-active-toolbar"><ul class="ckeditor-toolbar-groups clearfix js-sortable"><li class="ckeditor-toolbar-group" role="presentation" data-drupal-ckeditor-type="group" data-drupal-ckeditor-toolbar-group-name="Embed button icon preview" tabindex="0"><h3 class="ckeditor-toolbar-group-name" id="ckeditor-toolbar-group-aria-label-for-formatting">Embed button icon preview</h3><ul class="ckeditor-buttons ckeditor-toolbar-group-buttons js-sortable" role="toolbar" data-drupal-ckeditor-button-sorting="target" aria-labelledby="ckeditor-toolbar-group-aria-label-for-formatting"><li data-drupal-ckeditor-button-name="Bold" class="ckeditor-button"><a href="#" role="button" title="' . $button
->label() . '" aria-label="' . $button
->label() . '"><span class="cke_button_icon">';
$form['icon_preview']['image']['#suffix'] = '</span></a></li></ul></li></ul></div>';
$form['icon_preview']['#attached']['library'][] = 'ckeditor/drupal.ckeditor.admin';
}
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$button = $this->entity;
if ($plugin = $button
->getTypePlugin()) {
$plugin_form_state = clone $form_state;
$plugin_form_state
->setValues($button
->getTypeSettings());
$plugin
->validateConfigurationForm($form['type_settings'], $plugin_form_state);
if ($errors = $plugin_form_state
->getErrors()) {
foreach ($errors as $name => $error) {
$form_state
->setErrorByName($name, $error);
}
}
$form_state
->setValue('type_settings', $plugin_form_state
->getValues());
}
}
public function save(array $form, FormStateInterface $form_state) {
$button = $this->entity;
$plugin = $button
->getTypePlugin();
$plugin_form_state = clone $form_state;
$plugin_form_state
->setValues($button
->getTypeSettings());
$plugin
->submitConfigurationForm($form['type_settings'], $plugin_form_state);
$form_state
->setValue('type_settings', $plugin
->getConfiguration());
$button
->set('type_settings', $plugin
->getConfiguration());
$icon_fid = $form_state
->getValue([
'icon_file',
'0',
]);
if (!empty($icon_fid) && ($file = $this->entityTypeManager
->getStorage('file')
->load($icon_fid))) {
$file
->setPermanent();
$file
->save();
$button
->set('icon', EmbedButton::convertImageToEncodedData($file
->getFileUri()));
}
elseif ($form_state
->getValue('icon_reset')) {
$button
->set('icon', NULL);
}
$status = $button
->save();
$t_args = [
'%label' => $button
->label(),
];
if ($status === SAVED_UPDATED) {
$this
->messenger()
->addStatus($this
->t('The embed button %label has been updated.', $t_args));
$this
->logger('embed')
->info('Updated embed button %label.', $t_args);
}
elseif ($status === SAVED_NEW) {
$this
->messenger()
->addStatus($this
->t('The embed button %label has been added.', $t_args));
$this
->logger('embed')
->info('Added embed button %label.', $t_args);
}
$form_state
->setRedirectUrl($button
->toUrl());
}
public function updateTypeSettings(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand('#embed-type-settings-wrapper', $form['type_settings']));
return $response;
}
}