Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\workspaces;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@17
|
6 use Drupal\Core\Form\FormStateInterface;
|
Chris@17
|
7 use Drupal\Core\Render\Element;
|
Chris@17
|
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@17
|
9 use Drupal\views\Form\ViewsExposedForm;
|
Chris@17
|
10 use Drupal\workspaces\Form\WorkspaceFormInterface;
|
Chris@17
|
11 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@17
|
12
|
Chris@17
|
13 /**
|
Chris@17
|
14 * Defines a class for reacting to form operations.
|
Chris@17
|
15 *
|
Chris@17
|
16 * @internal
|
Chris@17
|
17 */
|
Chris@17
|
18 class FormOperations implements ContainerInjectionInterface {
|
Chris@17
|
19
|
Chris@17
|
20 /**
|
Chris@17
|
21 * The workspace manager service.
|
Chris@17
|
22 *
|
Chris@17
|
23 * @var \Drupal\workspaces\WorkspaceManagerInterface
|
Chris@17
|
24 */
|
Chris@17
|
25 protected $workspaceManager;
|
Chris@17
|
26
|
Chris@17
|
27 /**
|
Chris@17
|
28 * Constructs a new FormOperations instance.
|
Chris@17
|
29 *
|
Chris@17
|
30 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
|
Chris@17
|
31 * The workspace manager service.
|
Chris@17
|
32 */
|
Chris@17
|
33 public function __construct(WorkspaceManagerInterface $workspace_manager) {
|
Chris@17
|
34 $this->workspaceManager = $workspace_manager;
|
Chris@17
|
35 }
|
Chris@17
|
36
|
Chris@17
|
37 /**
|
Chris@17
|
38 * {@inheritdoc}
|
Chris@17
|
39 */
|
Chris@17
|
40 public static function create(ContainerInterface $container) {
|
Chris@17
|
41 return new static(
|
Chris@17
|
42 $container->get('workspaces.manager')
|
Chris@17
|
43 );
|
Chris@17
|
44 }
|
Chris@17
|
45
|
Chris@17
|
46 /**
|
Chris@17
|
47 * Alters forms to disallow editing in non-default workspaces.
|
Chris@17
|
48 *
|
Chris@17
|
49 * @param array $form
|
Chris@17
|
50 * An associative array containing the structure of the form.
|
Chris@17
|
51 * @param \Drupal\Core\Form\FormStateInterface $form_state
|
Chris@17
|
52 * The current state of the form.
|
Chris@17
|
53 * @param string $form_id
|
Chris@17
|
54 * The form ID.
|
Chris@17
|
55 *
|
Chris@17
|
56 * @see hook_form_alter()
|
Chris@17
|
57 */
|
Chris@17
|
58 public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
|
Chris@17
|
59 // No alterations are needed in the default workspace.
|
Chris@17
|
60 if ($this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
|
Chris@17
|
61 return;
|
Chris@17
|
62 }
|
Chris@17
|
63
|
Chris@17
|
64 // Add an additional validation step for every form if we are in a
|
Chris@17
|
65 // non-default workspace.
|
Chris@17
|
66 $this->addWorkspaceValidation($form);
|
Chris@17
|
67
|
Chris@17
|
68 // If a form has already been marked as safe or not to submit in a
|
Chris@17
|
69 // non-default workspace, we don't have anything else to do.
|
Chris@17
|
70 if ($form_state->has('workspace_safe')) {
|
Chris@17
|
71 return;
|
Chris@17
|
72 }
|
Chris@17
|
73
|
Chris@17
|
74 // No forms are safe to submit in a non-default workspace by default, except
|
Chris@17
|
75 // for the whitelisted ones defined below.
|
Chris@17
|
76 $workspace_safe = FALSE;
|
Chris@17
|
77
|
Chris@17
|
78 // Whitelist a few forms that we know are safe to submit.
|
Chris@17
|
79 $form_object = $form_state->getFormObject();
|
Chris@17
|
80 $is_workspace_form = $form_object instanceof WorkspaceFormInterface;
|
Chris@17
|
81 $is_search_form = in_array($form_object->getFormId(), ['search_block_form', 'search_form'], TRUE);
|
Chris@17
|
82 $is_views_exposed_form = $form_object instanceof ViewsExposedForm;
|
Chris@17
|
83 if ($is_workspace_form || $is_search_form || $is_views_exposed_form) {
|
Chris@17
|
84 $workspace_safe = TRUE;
|
Chris@17
|
85 }
|
Chris@17
|
86
|
Chris@17
|
87 $form_state->set('workspace_safe', $workspace_safe);
|
Chris@17
|
88 }
|
Chris@17
|
89
|
Chris@17
|
90 /**
|
Chris@17
|
91 * Adds our validation handler recursively on each element of a form.
|
Chris@17
|
92 *
|
Chris@17
|
93 * @param array &$element
|
Chris@17
|
94 * An associative array containing the structure of the form.
|
Chris@17
|
95 */
|
Chris@17
|
96 protected function addWorkspaceValidation(array &$element) {
|
Chris@17
|
97 // Recurse through all children and add our validation handler if needed.
|
Chris@17
|
98 foreach (Element::children($element) as $key) {
|
Chris@17
|
99 if (isset($element[$key]) && $element[$key]) {
|
Chris@17
|
100 $this->addWorkspaceValidation($element[$key]);
|
Chris@17
|
101 }
|
Chris@17
|
102 }
|
Chris@17
|
103
|
Chris@17
|
104 if (isset($element['#validate'])) {
|
Chris@17
|
105 $element['#validate'][] = [get_called_class(), 'validateDefaultWorkspace'];
|
Chris@17
|
106 }
|
Chris@17
|
107 }
|
Chris@17
|
108
|
Chris@17
|
109 /**
|
Chris@17
|
110 * Validation handler which sets a validation error for all unsupported forms.
|
Chris@17
|
111 */
|
Chris@17
|
112 public static function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) {
|
Chris@17
|
113 if ($form_state->get('workspace_safe') !== TRUE) {
|
Chris@17
|
114 $form_state->setError($form, new TranslatableMarkup('This form can only be submitted in the default workspace.'));
|
Chris@17
|
115 }
|
Chris@17
|
116 }
|
Chris@17
|
117
|
Chris@17
|
118 }
|