3 namespace Drupal\workspaces;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8 use Drupal\Core\StringTranslation\TranslatableMarkup;
9 use Drupal\views\Form\ViewsExposedForm;
10 use Drupal\workspaces\Form\WorkspaceFormInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Defines a class for reacting to form operations.
18 class FormOperations implements ContainerInjectionInterface {
21 * The workspace manager service.
23 * @var \Drupal\workspaces\WorkspaceManagerInterface
25 protected $workspaceManager;
28 * Constructs a new FormOperations instance.
30 * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
31 * The workspace manager service.
33 public function __construct(WorkspaceManagerInterface $workspace_manager) {
34 $this->workspaceManager = $workspace_manager;
40 public static function create(ContainerInterface $container) {
42 $container->get('workspaces.manager')
47 * Alters forms to disallow editing in non-default workspaces.
50 * An associative array containing the structure of the form.
51 * @param \Drupal\Core\Form\FormStateInterface $form_state
52 * The current state of the form.
53 * @param string $form_id
56 * @see hook_form_alter()
58 public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
59 // No alterations are needed in the default workspace.
60 if ($this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
64 // Add an additional validation step for every form if we are in a
65 // non-default workspace.
66 $this->addWorkspaceValidation($form);
68 // If a form has already been marked as safe or not to submit in a
69 // non-default workspace, we don't have anything else to do.
70 if ($form_state->has('workspace_safe')) {
74 // No forms are safe to submit in a non-default workspace by default, except
75 // for the whitelisted ones defined below.
76 $workspace_safe = FALSE;
78 // Whitelist a few forms that we know are safe to submit.
79 $form_object = $form_state->getFormObject();
80 $is_workspace_form = $form_object instanceof WorkspaceFormInterface;
81 $is_search_form = in_array($form_object->getFormId(), ['search_block_form', 'search_form'], TRUE);
82 $is_views_exposed_form = $form_object instanceof ViewsExposedForm;
83 if ($is_workspace_form || $is_search_form || $is_views_exposed_form) {
84 $workspace_safe = TRUE;
87 $form_state->set('workspace_safe', $workspace_safe);
91 * Adds our validation handler recursively on each element of a form.
93 * @param array &$element
94 * An associative array containing the structure of the form.
96 protected function addWorkspaceValidation(array &$element) {
97 // Recurse through all children and add our validation handler if needed.
98 foreach (Element::children($element) as $key) {
99 if (isset($element[$key]) && $element[$key]) {
100 $this->addWorkspaceValidation($element[$key]);
104 if (isset($element['#validate'])) {
105 $element['#validate'][] = [get_called_class(), 'validateDefaultWorkspace'];
110 * Validation handler which sets a validation error for all unsupported forms.
112 public static function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) {
113 if ($form_state->get('workspace_safe') !== TRUE) {
114 $form_state->setError($form, new TranslatableMarkup('This form can only be submitted in the default workspace.'));