3 namespace Drupal\ctools\Form;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\OpenModalDialogCommand;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormStateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
13 abstract class RequiredContext extends FormBase {
16 * @var \Drupal\Core\TypedData\TypedDataManager
18 protected $typedDataManager;
23 protected $machine_name;
28 public static function create(ContainerInterface $container) {
29 return new static($container->get('typed_data_manager'));
32 public function __construct(PluginManagerInterface $typed_data_manager) {
33 $this->typedDataManager = $typed_data_manager;
39 public function getFormId() {
40 return 'ctools_required_context_form';
46 public function buildForm(array $form, FormStateInterface $form_state) {
47 $cached_values = $form_state->getTemporaryValue('wizard');
48 $this->machine_name = $cached_values['id'];
49 $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
51 foreach ($this->typedDataManager->getDefinitions() as $plugin_id => $definition) {
52 $options[$plugin_id] = (string) $definition['label'];
54 $form['items'] = array(
56 '#prefix' => '<div id="configured-contexts">',
57 '#suffix' => '</div>',
59 '#header' => array($this->t('Information'), $this->t('Description'), $this->t('Operations')),
60 '#rows' => $this->renderContexts($cached_values),
61 '#empty' => $this->t('No required contexts have been configured.')
65 '#options' => $options,
70 '#value' => $this->t('Add required context'),
72 'callback' => [$this, 'add'],
76 'callback' => [$this, 'submitform'],
85 public function submitForm(array &$form, FormStateInterface $form_state) {
86 $cached_values = $form_state->getTemporaryValue('wizard');
87 list($route_name, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('contexts'));
88 $form_state->setRedirect($route_name . '.edit', $route_parameters);
92 * Custom ajax form submission handler.
95 * @param \Drupal\Core\Form\FormStateInterface $form_state
97 * @return \Drupal\Core\Ajax\AjaxResponse
99 public function add(array &$form, FormStateInterface $form_state) {
100 $context = $form_state->getValue('contexts');
101 $content = \Drupal::formBuilder()->getForm($this->getContextClass(), $context, $this->getTempstoreId(), $this->machine_name);
102 $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
103 $response = new AjaxResponse();
104 $response->addCommand(new OpenModalDialogCommand($this->t('Configure Required Context'), $content, array('width' => '700')));
109 * @param $cached_values
113 public function renderContexts($cached_values) {
114 $configured_contexts = array();
115 foreach ($this->getContexts($cached_values) as $row => $context) {
116 list($plugin_id, $label, $machine_name, $description) = array_values($context);
117 list($route_name, $route_parameters) = $this->getOperationsRouteInfo($cached_values, $cached_values['id'], $row);
119 '#type' => 'operations',
120 '#links' => $this->getOperations($route_name, $route_parameters),
122 $configured_contexts[] = array(
123 $this->t('<strong>Label:</strong> @label<br /> <strong>Type:</strong> @type', ['@label' => $label, '@type' => $plugin_id]),
124 $this->t('@description', ['@description' => $description]),
130 return $configured_contexts;
133 protected function getOperations($route_name_base, array $route_parameters = array()) {
134 $operations['edit'] = array(
135 'title' => $this->t('Edit'),
136 'url' => new Url($route_name_base . '.edit', $route_parameters),
138 'attributes' => array(
139 'class' => array('use-ajax'),
140 'data-accepts' => 'application/vnd.drupal-modal',
141 'data-dialog-options' => json_encode(array(
149 $route_parameters['id'] = $route_parameters['context'];
150 $operations['delete'] = array(
151 'title' => $this->t('Delete'),
152 'url' => new Url($route_name_base . '.delete', $route_parameters),
154 'attributes' => array(
155 'class' => array('use-ajax'),
156 'data-accepts' => 'application/vnd.drupal-modal',
157 'data-dialog-options' => json_encode(array(
166 * Return a subclass of '\Drupal\ctools\Form\ContextConfigure'.
168 * The ContextConfigure class is designed to be subclassed with custom route
169 * information to control the modal/redirect needs of your use case.
173 abstract protected function getContextClass();
176 * Provide the tempstore id for your specified use case.
180 abstract protected function getTempstoreId();
183 * Document the route name and parameters for edit/delete context operations.
185 * The route name returned from this method is used as a "base" to which
186 * ".edit" and ".delete" are appeneded in the getOperations() method.
187 * Subclassing '\Drupal\ctools\Form\ContextConfigure' and
188 * '\Drupal\ctools\Form\RequiredContextDelete' should set you up for using
189 * this approach quite seamlessly.
191 * @param mixed $cached_values
193 * @param string $machine_name
199 * return ['route.base.name', ['machine_name' => $machine_name, 'context' => $row]];
201 abstract protected function getOperationsRouteInfo($cached_values, $machine_name, $row);
204 * Custom logic for retrieving the contexts array from cached_values.
206 * @param $cached_values
210 abstract protected function getContexts($cached_values);