class FormOperations

Same name in this branch
  1. 11.x core/modules/workspaces/src/FormOperations.php \Drupal\workspaces\FormOperations
Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/FormOperations.php \Drupal\workspaces\FormOperations
  2. 8.9.x core/modules/workspaces/src/FormOperations.php \Drupal\workspaces\FormOperations
  3. 10 core/modules/workspaces/src/FormOperations.php \Drupal\workspaces\FormOperations

Defines a class for reacting to form operations.

Hierarchy

Expanded class hierarchy of FormOperations

File

core/modules/workspaces/src/Hook/FormOperations.php, line 16

Namespace

Drupal\workspaces\Hook
View source
class FormOperations {
  public function __construct(protected WorkspaceManagerInterface $workspaceManager) {
  }
  
  /**
   * Implements hook_form_alter().
   */
  public function formAlter(array &$form, FormStateInterface $form_state, $form_id) : void {
    // No alterations are needed if we're not in a workspace context.
    if (!$this->workspaceManager
      ->hasActiveWorkspace()) {
      return;
    }
    // If a form hasn't already been marked as safe or not to submit in a
    // workspace, check the generic interfaces.
    if (!$form_state->has('workspace_safe')) {
      $form_object = $form_state->getFormObject();
      $workspace_safe = $form_object instanceof WorkspaceSafeFormInterface || $form_object instanceof WorkspaceDynamicSafeFormInterface && $form_object->isWorkspaceSafeForm($form, $form_state);
      $form_state->set('workspace_safe', $workspace_safe);
    }
    // Add a validation step for every other form.
    if ($form_state->get('workspace_safe') !== TRUE) {
      $form['workspace_safe'] = [
        '#type' => 'value',
        '#value' => FALSE,
      ];
      $this->addWorkspaceValidation($form);
    }
  }
  
  /**
   * 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) : void {
    // 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['#submit'])) {
      $element['#validate'][] = [
        static::class,
        'validateDefaultWorkspace',
      ];
      // Ensure that the workspace validation is always shown, even when the
      // form element is limiting validation errors.
      if (isset($element['#limit_validation_errors']) && $element['#limit_validation_errors'] !== FALSE) {
        $element['#limit_validation_errors'][] = [
          'workspace_safe',
        ];
      }
    }
  }
  
  /**
   * Validation handler which sets a validation error for all unsupported forms.
   */
  public static function validateDefaultWorkspace(array &$form, FormStateInterface $form_state) : void {
    if ($form_state->get('workspace_safe') !== TRUE && isset($form_state->getCompleteForm()['workspace_safe'])) {
      $form_state->setErrorByName('workspace_safe', new TranslatableMarkup('This form can only be submitted in the default workspace.'));
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary
FormOperations::addWorkspaceValidation protected function Adds our validation handler recursively on each element of a form.
FormOperations::formAlter public function Implements hook_form_alter().
FormOperations::validateDefaultWorkspace public static function Validation handler which sets a validation error for all unsupported forms.
FormOperations::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.