View source
<?php
namespace Drupal\ctools\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
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\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ManageConditions extends FormBase {
protected $manager;
protected $formBuilder;
protected $machine_name;
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.condition'), $container
->get('form_builder'));
}
public function __construct(PluginManagerInterface $manager, FormBuilderInterface $form_builder) {
$this->manager = $manager;
$this->formBuilder = $form_builder;
}
public function getFormId() {
return 'ctools_manage_conditions_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$this->machine_name = $cached_values['id'];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$options = [];
$contexts = $this
->getContexts($cached_values);
foreach ($this->manager
->getDefinitionsForContexts($contexts) as $plugin_id => $definition) {
$options[$plugin_id] = (string) $definition['label'];
}
$form['items'] = [
'#type' => 'markup',
'#prefix' => '<div id="configured-conditions">',
'#suffix' => '</div>',
'#theme' => 'table',
'#header' => [
$this
->t('Plugin Id'),
$this
->t('Summary'),
$this
->t('Operations'),
],
'#rows' => $this
->renderRows($cached_values),
'#empty' => $this
->t('No required conditions have been configured.'),
];
$form['conditions'] = [
'#type' => 'select',
'#options' => $options,
];
$form['add'] = [
'#type' => 'submit',
'#name' => 'add',
'#value' => $this
->t('Add Condition'),
'#ajax' => [
'callback' => [
$this,
'add',
],
'event' => 'click',
],
'#submit' => [
'callback' => [
$this,
'submitForm',
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state
->getValue('conditions'));
$form_state
->setRedirect($this
->getAddRoute($cached_values), $route_parameters);
}
public function add(array &$form, FormStateInterface $form_state) {
$condition = $form_state
->getValue('conditions');
$content = $this->formBuilder
->getForm($this
->getConditionClass(), $condition, $this
->getTempstoreId(), $this->machine_name);
$content['#attached']['library'][] = 'core/drupal.dialog.ajax';
$cached_values = $form_state
->getTemporaryValue('wizard');
list(, $route_parameters) = $this
->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state
->getValue('conditions'));
$route_name = $this
->getAddRoute($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 Required Context'), $content, [
'width' => '700',
]));
return $response;
}
public function renderRows($cached_values) {
$configured_conditions = [];
foreach ($this
->getConditions($cached_values) as $row => $condition) {
$instance = $this->manager
->createInstance($condition['id'], $condition);
list($route_name, $route_parameters) = $this
->getOperationsRouteInfo($cached_values, $cached_values['id'], $row);
$build = [
'#type' => 'operations',
'#links' => $this
->getOperations($route_name, $route_parameters),
];
$configured_conditions[] = [
$instance
->getPluginId(),
$instance
->summary(),
'operations' => [
'data' => $build,
],
];
}
return $configured_conditions;
}
protected function getOperations($route_name_base, array $route_parameters = []) {
$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,
]),
],
];
$route_parameters['id'] = $route_parameters['condition'];
$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 getConditionClass();
protected abstract function getAddRoute($cached_values);
protected abstract function getTempstoreId();
protected abstract function getOperationsRouteInfo($cached_values, $machine_name, $row);
protected abstract function getConditions($cached_values);
protected abstract function getContexts($cached_values);
}