3 namespace Drupal\ctools\Form;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\OpenModalDialogCommand;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormBuilderInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\TypedData\TypedDataManagerInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
15 abstract class ManageContext extends FormBase {
18 * The machine name of the wizard we're working with.
22 protected $machine_name;
25 * The typed data manager.
27 * @var \Drupal\Core\TypedData\TypedDataManagerInterface
29 protected $typedDataManager;
34 * @var \Drupal\Core\Form\FormBuilderInterface
36 protected $formBuilder;
39 * An array of property types that are eligible as relationships.
43 protected $property_types = [];
46 * A property for controlling usage of relationships in an implementation.
50 protected $relationships = TRUE;
55 public static function create(ContainerInterface $container) {
56 return new static($container->get('typed_data_manager'), $container->get('form_builder'));
60 * ManageContext constructor.
62 * @param \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data_manager
63 * The typed data manager.
64 * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
67 public function __construct(TypedDataManagerInterface $typed_data_manager, FormBuilderInterface $form_builder) {
68 $this->typedDataManager = $typed_data_manager;
69 $this->formBuilder = $form_builder;
76 public function getFormId() {
77 return 'ctools_manage_context_form';
83 public function buildForm(array $form, FormStateInterface $form_state) {
84 $cached_values = $form_state->getTemporaryValue('wizard');
85 $this->machine_name = $cached_values['id'];
86 $form['items'] = array(
88 '#prefix' => '<div id="configured-contexts">',
89 '#suffix' => '</div>',
91 '#header' => array($this->t('Context ID'), $this->t('Label'), $this->t('Data Type'), $this->t('Options')),
92 '#rows' => $this->renderRows($cached_values),
93 '#empty' => $this->t('No contexts or relationships have been added.')
95 foreach ($this->typedDataManager->getDefinitions() as $type => $definition) {
96 $types[$type] = $definition['label'];
98 if (isset($types['entity'])) {
99 unset($types['entity']);
104 '#options' => $types,
109 '#value' => $this->t('Add new context'),
111 'callback' => [$this, 'addContext'],
115 'callback' => [$this, 'submitForm'],
119 $form['relationships'] = [
121 '#title' => $this->t('Add a relationship'),
122 '#options' => $this->getAvailableRelationships($cached_values),
123 '#access' => $this->relationships,
125 $form['add_relationship'] = [
127 '#name' => 'add_relationship',
128 '#value' => $this->t('Add Relationship'),
130 'callback' => [$this, 'addRelationship'],
134 'callback' => [$this, 'submitForm'],
136 '#access' => $this->relationships,
144 public function submitForm(array &$form, FormStateInterface $form_state) {
145 if ($form_state->getTriggeringElement()['#name'] == 'add') {
146 $cached_values = $form_state->getTemporaryValue('wizard');
147 list(, $route_parameters) = $this->getContextOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('context'));
148 $form_state->setRedirect($this->getContextAddRoute($cached_values), $route_parameters);
150 if ($form_state->getTriggeringElement()['#name'] == 'add_relationship') {
151 $cached_values = $form_state->getTemporaryValue('wizard');
152 list(, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $form_state->getValue('relationships'));
153 $form_state->setRedirect($this->getRelationshipAddRoute($cached_values), $route_parameters);
157 public function addContext(array &$form, FormStateInterface $form_state) {
158 $context = $form_state->getValue('context');
159 $content = $this->formBuilder->getForm($this->getContextClass(), $context, $this->getTempstoreId(), $this->machine_name);
160 $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
161 $cached_values = $form_state->getTemporaryValue('wizard');
162 list(, $route_parameters) = $this->getContextOperationsRouteInfo($cached_values, $this->machine_name, $context);
163 $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getContextAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]);
164 $response = new AjaxResponse();
165 $response->addCommand(new OpenModalDialogCommand($this->t('Add new context'), $content, array('width' => '700')));
169 public function addRelationship(array &$form, FormStateInterface $form_state) {
170 $relationship = $form_state->getValue('relationships');
171 $content = $this->formBuilder->getForm($this->getRelationshipClass(), $relationship, $this->getTempstoreId(), $this->machine_name);
172 $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
173 $cached_values = $form_state->getTemporaryValue('wizard');
174 list(, $route_parameters) = $this->getRelationshipOperationsRouteInfo($cached_values, $this->machine_name, $relationship);
175 $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $this->url($this->getRelationshipAddRoute($cached_values), $route_parameters, ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]]);
176 $response = new AjaxResponse();
177 $response->addCommand(new OpenModalDialogCommand($this->t('Configure Relationship'), $content, array('width' => '700')));
181 protected function getAvailableRelationships($cached_values) {
182 /** @var \Drupal\ctools\TypedDataResolver $resolver */
183 $resolver = \Drupal::service('ctools.typed_data.resolver');
184 return $resolver->getTokensForContexts($this->getContexts($cached_values));
188 * @param $cached_values
192 protected function renderRows($cached_values) {
194 foreach ($this->getContexts($cached_values) as $row => $context) {
195 list($route_name, $route_parameters) = $this->getContextOperationsRouteInfo($cached_values, $this->machine_name, $row);
197 '#type' => 'operations',
198 '#links' => $this->getOperations($cached_values, $row, $route_name, $route_parameters),
200 $contexts[$row] = array(
202 $context->getContextDefinition()->getLabel(),
203 $context->getContextDefinition()->getDataType(),
213 * @param array $cached_values
215 * @param string $route_name_base
216 * @param array $route_parameters
220 protected function getOperations($cached_values, $row, $route_name_base, array $route_parameters = array()) {
222 if ($this->isEditableContext($cached_values, $row)) {
223 $operations['edit'] = array(
224 'title' => $this->t('Edit'),
225 'url' => new Url($route_name_base . '.edit', $route_parameters),
227 'attributes' => array(
228 'class' => ['use-ajax'],
229 'data-dialog-type' => 'modal',
230 'data-dialog-options' => Json::encode([
235 $operations['delete'] = array(
236 'title' => $this->t('Delete'),
237 'url' => new Url($route_name_base . '.delete', $route_parameters),
239 'attributes' => array(
240 'class' => array('use-ajax'),
241 'data-dialog-type' => 'modal',
242 'data-dialog-options' => Json::encode([
252 * Return a subclass of '\Drupal\ctools\Form\ContextConfigure'.
254 * The ContextConfigure class is designed to be subclassed with custom
255 * route information to control the modal/redirect needs of your use case.
259 abstract protected function getContextClass($cached_values);
262 * Return a subclass of '\Drupal\ctools\Form\RelationshipConfigure'.
264 * The RelationshipConfigure class is designed to be subclassed with custom
265 * route information to control the modal/redirect needs of your use case.
269 abstract protected function getRelationshipClass($cached_values);
272 * The route to which context 'add' actions should submit.
276 abstract protected function getContextAddRoute($cached_values);
279 * The route to which relationship 'add' actions should submit.
283 abstract protected function getRelationshipAddRoute($cached_values);
286 * Provide the tempstore id for your specified use case.
290 abstract protected function getTempstoreId();
293 * Returns the contexts already available in the wizard.
295 * @param mixed $cached_values
297 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
299 abstract protected function getContexts($cached_values);
302 * @param mixed $cached_values
303 * @param string $machine_name
308 abstract protected function getContextOperationsRouteInfo($cached_values, $machine_name, $row);
311 * @param mixed $cached_values
312 * @param string $machine_name
317 abstract protected function getRelationshipOperationsRouteInfo($cached_values, $machine_name, $row);
320 * @param mixed $cached_values
325 abstract protected function isEditableContext($cached_values, $row);