View source
<?php
namespace Drupal\ctools\Form;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Core\Url;
use Drupal\ctools\TypedDataResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ManageContext extends FormBase {
protected $machine_name;
protected $typedDataManager;
protected $formBuilder;
protected $typedDataResolver;
protected $property_types = [];
protected $relationships = TRUE;
public function __construct(TypedDataManagerInterface $typed_data_manager, FormBuilderInterface $form_builder, TypedDataResolver $ctools_typed_data_resolver) {
$this->typedDataManager = $typed_data_manager;
$this->formBuilder = $form_builder;
$this->typedDataResolver = $ctools_typed_data_resolver;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('typed_data_manager'), $container
->get('form_builder'), $container
->get('ctools.typed_data.resolver'));
}
public function getFormId() {
return 'ctools_manage_context_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$this->machine_name = $cached_values['id'];
$form['items'] = [
'#type' => 'markup',
'#prefix' => '<div id="configured-contexts">',
'#suffix' => '</div>',
'#theme' => 'table',
'#header' => [
$this
->t('Context ID'),
$this
->t('Label'),
$this
->t('Data Type'),
$this
->t('Options'),
],
'#rows' => $this
->renderRows($cached_values),
'#empty' => $this
->t('No contexts or relationships have been added.'),
];
foreach ($this->typedDataManager
->getDefinitions() as $type => $definition) {
$types[$type] = $definition['label'];
}
if (isset($types['entity'])) {
unset($types['entity']);
}
asort($types);
$form['context'] = [
'#type' => 'select',
'#options' => $types,
];
$form['add'] = [
'#type' => 'submit',
'#name' => 'add',
'#value' => $this
->t('Add new context'),
'#ajax' => [
'callback' => [
$this,
'addContext',
],
'event' => 'click',
],
'#submit' => [
'callback' => [
$this,
'submitForm',
],
],
];
$form['relationships'] = [
'#type' => 'select',
'#title' => $this
->t('Add a relationship'),
'#options' => $this
->getAvailableRelationships($cached_values),
'#access' => $this->relationships,
];
$form['add_relationship'] = [
'#type' => 'submit',
'#name' => 'add_relationship',
'#value' => $this
->t('Add Relationship'),
'#ajax' => [
'callback' => [
$this,
'addRelationship',
],
'event' => 'click',
],
'#submit' => [
'callback' => [
$this,
'submitForm',
],
],
'#access' => $this->relationships,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#name'] == 'add') {
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getContextOperationsRouteInfo($cached_values, $this->machine_name, $form_state
->getValue('context'));
$form_state
->setRedirect($this
->getContextAddRoute($cached_values), $route_parameters);
}
if ($form_state
->getTriggeringElement()['#name'] == 'add_relationship') {
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $form_state
->getValue('relationships'));
$form_state
->setRedirect($this
->getRelationshipAddRoute($cached_values), $route_parameters);
}
}
public function addContext(array &$form, FormStateInterface $form_state) {
$context = $form_state
->getValue('context');
$content = $this->formBuilder
->getForm($this
->getContextClass(), $context, $this
->getTempstoreId(), $this->machine_name);
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getContextOperationsRouteInfo($cached_values, $this->machine_name, $context);
$route_name = $this
->getContextAddRoute($cached_values);
$route_options = [
'query' => [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
];
$url = Url::fromRoute($route_name, $route_parameters, $route_options);
$content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url
->toString();
$response = new AjaxResponse();
$response
->addCommand(new OpenModalDialogCommand($this
->t('Add new context'), $content, [
'width' => '700',
]));
return $response;
}
public function addRelationship(array &$form, FormStateInterface $form_state) {
$relationship = $form_state
->getValue('relationships');
$content = $this->formBuilder
->getForm($this
->getRelationshipClass(), $relationship, $this
->getTempstoreId(), $this->machine_name);
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $relationship);
$route_name = $this
->getRelationshipAddRoute($cached_values);
$route_options = [
'query' => [
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
],
];
$url = Url::fromRoute($route_name, $route_parameters, $route_options);
$content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url
->toString();
$response = new AjaxResponse();
$response
->addCommand(new OpenModalDialogCommand($this
->t('Configure Relationship'), $content, [
'width' => '700',
]));
return $response;
}
protected function getAvailableRelationships($cached_values) {
$resolver = $this->typedDataResolver;
return $resolver
->getTokensForContexts($this
->getContexts($cached_values));
}
protected function renderRows($cached_values) {
$contexts = [];
foreach ($this
->getContexts($cached_values) as $row => $context) {
list($route_name, $route_parameters) = $this
->getContextOperationsRouteInfo($cached_values, $this->machine_name, $row);
$build = [
'#type' => 'operations',
'#links' => $this
->getOperations($cached_values, $row, $route_name, $route_parameters),
];
$contexts[$row] = [
$row,
$context
->getContextDefinition()
->getLabel(),
$context
->getContextDefinition()
->getDataType(),
'operations' => [
'data' => $build,
],
];
}
return $contexts;
}
protected function getOperations($cached_values, $row, $route_name_base, array $route_parameters = []) {
$operations = [];
if ($this
->isEditableContext($cached_values, $row)) {
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => new Url($route_name_base . '.edit', $route_parameters),
'weight' => 10,
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => new Url($route_name_base . '.delete', $route_parameters),
'weight' => 100,
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
}
return $operations;
}
protected abstract function getContextClass($cached_values);
protected abstract function getRelationshipClass($cached_values);
protected abstract function getContextAddRoute($cached_values);
protected abstract function getRelationshipAddRoute($cached_values);
protected abstract function getTempstoreId();
protected abstract function getContexts($cached_values);
protected abstract function getContextOperationsRouteInfo($cached_values, $machine_name, $row);
protected abstract function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row);
protected abstract function isEditableContext($cached_values, $row);
}