View source
<?php
namespace Drupal\ctools\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ContextConfigure extends FormBase {
protected $tempstore;
protected $entityTypeManager;
protected $tempstore_id;
protected $machine_name;
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.shared'), $container
->get('entity_type.manager'));
}
public function __construct(SharedTempStoreFactory $tempstore, EntityTypeManagerInterface $entity_type_manager) {
$this->tempstore = $tempstore;
$this->entityTypeManager = $entity_type_manager;
}
public function getFormId() {
return 'ctools_context_configure';
}
public function buildForm(array $form, FormStateInterface $form_state, $context_id = NULL, $tempstore_id = NULL, $machine_name = NULL) {
$this->tempstore_id = $tempstore_id;
$this->machine_name = $machine_name;
$cached_values = $this->tempstore
->get($this->tempstore_id)
->get($this->machine_name);
$contexts = $this
->getContexts($cached_values);
$edit = FALSE;
if (!empty($contexts[$context_id])) {
$context = $contexts[$context_id];
$machine_name = $context_id;
$edit = TRUE;
}
else {
if (strpos($context_id, 'entity:') === 0) {
$context_definition = new EntityContextDefinition($context_id);
}
else {
$context_definition = new ContextDefinition($context_id);
}
$context = new Context($context_definition);
$machine_name = '';
}
$label = $context
->getContextDefinition()
->getLabel();
$description = $context
->getContextDefinition()
->getDescription();
$data_type = $context
->getContextDefinition()
->getDataType();
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['context_id'] = [
'#type' => 'value',
'#value' => $context_id,
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#default_value' => $label,
'#required' => TRUE,
];
$form['machine_name'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Machine Name'),
'#default_value' => $machine_name,
'#required' => TRUE,
'#maxlength' => 128,
'#machine_name' => [
'source' => [
'label',
],
'exists' => [
$this,
'contextExists',
],
],
'#disabled' => $this
->disableMachineName($cached_values, $machine_name),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#default_value' => $description,
];
if (strpos($data_type, 'entity:') === 0) {
list(, $entity_type) = explode(':', $data_type);
$entity = $edit ? $context
->getContextValue() : NULL;
$form['context_value'] = [
'#type' => 'entity_autocomplete',
'#required' => TRUE,
'#target_type' => $entity_type,
'#default_value' => $entity,
'#title' => $this
->t('Select entity'),
];
}
else {
$value = $context
->getContextData()
->getValue();
$form['context_value'] = [
'#title' => $this
->t('Set a context value'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $value,
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#ajax' => [
'callback' => [
$this,
'ajaxSave',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('machine_name') != $form_state
->getValue('context_id')) {
$machine_name = $form_state
->getValue('machine_name');
$cached_values = $this->tempstore
->get($this->tempstore_id)
->get($this->machine_name);
if (!empty($this
->getContexts($cached_values)[$machine_name])) {
$form_state
->setError($form['machine_name'], $this
->t('That machine name is in use by another context definition.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $this->tempstore
->get($this->tempstore_id)
->get($this->machine_name);
$contexts = $this
->getContexts($cached_values);
if ($form_state
->getValue('machine_name') != $form_state
->getValue('context_id')) {
$data_type = $form_state
->getValue('context_id');
if (strpos($data_type, 'entity:') === 0) {
$context_definition = new EntityContextDefinition($data_type, $form_state
->getValue('label'), TRUE, FALSE, $form_state
->getValue('description'));
}
else {
$context_definition = new ContextDefinition($data_type, $form_state
->getValue('label'), TRUE, FALSE, $form_state
->getValue('description'));
}
}
else {
$context = $contexts[$form_state
->getValue('machine_name')];
$context_definition = $context
->getContextDefinition();
$context_definition
->setLabel($form_state
->getValue('label'));
$context_definition
->setDescription($form_state
->getValue('description'));
}
if (strpos($context_definition
->getDataType(), 'entity:') === 0) {
list(, $entity_type) = explode(':', $context_definition
->getDataType());
if (is_numeric($form_state
->getValue('context_value'))) {
$value = $this->entityTypeManager
->getStorage($entity_type)
->load($form_state
->getValue('context_value'));
}
}
else {
$value = $form_state
->getValue('context_value');
}
$context = new Context($context_definition, $value);
$cached_values = $this
->addContext($cached_values, $form_state
->getValue('machine_name'), $context);
$this->tempstore
->get($this->tempstore_id)
->set($this->machine_name, $cached_values);
list($route_name, $route_parameters) = $this
->getParentRouteInfo($cached_values);
$form_state
->setRedirect($route_name, $route_parameters);
}
public function ajaxSave(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$cached_values = $this->tempstore
->get($this->tempstore_id)
->get($this->machine_name);
list($route_name, $route_parameters) = $this
->getParentRouteInfo($cached_values);
$url = new Url($route_name, $route_parameters);
$response
->addCommand(new RedirectCommand($url
->toString()));
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
protected abstract function getParentRouteInfo($cached_values);
protected abstract function getContexts($cached_values);
protected abstract function addContext($cached_values, $context_id, ContextInterface $context);
public abstract function contextExists($value, $element, $form_state);
protected abstract function disableMachineName($cached_values, $machine_name);
}