View source
<?php
namespace Drupal\page_manager_ui\Form;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Display\VariantManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\page_manager\PageVariantInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PageGeneralForm extends FormBase {
protected $variantManager;
protected $entityQuery;
public function __construct(VariantManager $variant_manager, QueryFactory $entity_query) {
$this->variantManager = $variant_manager;
$this->entityQuery = $entity_query;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.display_variant'), $container
->get('entity.query'));
}
public function getFormId() {
return 'page_manager_general_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page = $cached_values['page'];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Administrative description'),
'#default_value' => $page
->getDescription(),
];
$form['path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path'),
'#maxlength' => 255,
'#default_value' => $page
->getPath(),
'#required' => TRUE,
'#element_validate' => [
[
$this,
'validatePath',
],
],
];
$form['use_admin_theme'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use admin theme'),
'#default_value' => $page
->usesAdminTheme(),
];
if ($page
->isNew()) {
$variant_plugin_options = [];
foreach ($this->variantManager
->getDefinitions() as $plugin_id => $definition) {
if (in_array($plugin_id, [
'simple_page',
'block_page',
])) {
continue;
}
$variant_plugin_options[$plugin_id] = $definition['admin_label'];
}
$form['variant_plugin_id'] = [
'#title' => $this
->t('Variant type'),
'#type' => 'select',
'#options' => $variant_plugin_options,
'#default_value' => !empty($cached_values['variant_plugin_id']) ? $cached_values['variant_plugin_id'] : '',
];
$form['wizard_options'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Optional features'),
'#description' => $this
->t('Check any optional features you need to be presented with forms for configuring them. If you do not check them here you will still be able to utilize these features once the new page is created. If you are not sure, leave these unchecked.'),
'#options' => [
'access' => $this
->t('Page access'),
'contexts' => $this
->t('Variant contexts'),
'selection' => $this
->t('Variant selection criteria'),
],
'#default_value' => !empty($cached_values['wizard_options']) ? $cached_values['wizard_options'] : [],
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page = $cached_values['page'];
$page
->set('description', $form_state
->getValue('description'));
$page
->set('path', $form_state
->getValue('path'));
$page
->set('use_admin_theme', $form_state
->getValue('use_admin_theme'));
if ($page
->isNew()) {
$page
->set('id', $form_state
->getValue('id'));
$page
->set('label', $form_state
->getValue('label'));
if (empty($cached_values['variant_plugin_id'])) {
$variant_plugin_id = $cached_values['variant_plugin_id'] = $form_state
->getValue('variant_plugin_id');
$page_variant = \Drupal::entityManager()
->getStorage('page_variant')
->create([
'variant' => $form_state
->getValue('variant_plugin_id'),
'page' => $page
->id(),
'id' => "{$page->id()}-{$variant_plugin_id}-0",
'label' => $form['variant_plugin_id']['#options'][$variant_plugin_id],
]);
$page_variant
->setPageEntity($page);
$page
->addVariant($page_variant);
$cached_values['page_variant'] = $page_variant;
}
if ($cached_values['variant_plugin_id'] != $form_state
->getValue('variant_plugin_id') && !empty($cached_values['page_variant'])) {
$page_variant = $cached_values['page_variant'];
$page_variant
->set('variant', $form_state
->getValue('variant_plugin_id'));
$page_variant
->set('variant_settings', []);
$cached_values['variant_plugin_id'] = $form_state
->getValue('variant_plugin_id');
}
$cached_values['wizard_options'] = $form_state
->getValue('wizard_options');
$form_state
->setTemporaryValue('wizard', $cached_values);
}
}
public function validatePath(&$element, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$page = $cached_values['page'];
$value = '/' . trim($element['#value'], '/');
$form_state
->setValueForElement($element, $value);
$path_query = $this->entityQuery
->get('page')
->condition('path', $value);
if (!$page
->isNew()) {
$path_query
->condition('id', $page
->id(), '<>');
}
$path = $path_query
->execute();
if ($path) {
$form_state
->setErrorByName('path', $this
->t('The page path must be unique.'));
}
}
}