diff --git a/core/modules/workspace/src/FormOperations.php b/core/modules/workspace/src/FormOperations.php index d660ea6..95d6c87 100644 --- a/core/modules/workspace/src/FormOperations.php +++ b/core/modules/workspace/src/FormOperations.php @@ -65,10 +65,7 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id // Add an additional validation step for every form if we are in a // non-default workspace. - $form['#validate'][] = [$this, 'validateForm']; - foreach (Element::children($form['actions']) as $action) { - $form['actions'][$action]['#validate'][] = [$this, 'validateForm']; - } + $this->addWorkspaceValidation($form); // If a form has already been marked as safe or not to submit in a // non-default workspace, we don't have anything else to do. @@ -93,9 +90,28 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id } /** + * Adds our validation handler recursively on each element of a form. + * + * @param array &$element + * An associative array containing the structure of the form. + */ + protected function addWorkspaceValidation(array &$element) { + // Recurse through all children and add our validation handler if needed. + foreach (Element::children($element) as $key) { + if (isset($element[$key]) && $element[$key]) { + $this->addWorkspaceValidation($element[$key]); + } + } + + if (isset($element['#validate'])) { + $element['#validate'][] = [$this, 'validateDefaultWorkspace']; + } + } + + /** * Validation handler which sets a validation error for all unsupported forms. */ - public function validateForm(array &$form, FormStateInterface $form_state) { + public function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) { if ($form_state->get('workspace_safe') !== TRUE) { $form_state->setError($form, $this->t('This form can only be submitted in the default workspace.')); }