You are here

public function FormOperations::formAlter in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/FormOperations.php \Drupal\workspaces\FormOperations::formAlter()

Alters forms to disallow editing in non-default workspaces.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $form_id: The form ID.

See also

hook_form_alter()

File

core/modules/workspaces/src/FormOperations.php, line 58

Class

FormOperations
Defines a class for reacting to form operations.

Namespace

Drupal\workspaces

Code

public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {

  // No alterations are needed if we're not in a workspace context.
  if (!$this->workspaceManager
    ->hasActiveWorkspace()) {
    return;
  }

  // Add an additional validation step for every form if we are in a
  // non-default workspace.
  $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.
  if ($form_state
    ->has('workspace_safe')) {
    return;
  }

  // No forms are safe to submit in a non-default workspace by default, except
  // for the whitelisted ones defined below.
  $workspace_safe = FALSE;

  // Whitelist a few forms that we know are safe to submit.
  $form_object = $form_state
    ->getFormObject();
  $is_workspace_form = $form_object instanceof WorkspaceFormInterface;
  $is_search_form = in_array($form_object
    ->getFormId(), [
    'search_block_form',
    'search_form',
  ], TRUE);
  $is_views_exposed_form = $form_object instanceof ViewsExposedForm;
  if ($is_workspace_form || $is_search_form || $is_views_exposed_form) {
    $workspace_safe = TRUE;
  }
  $form_state
    ->set('workspace_safe', $workspace_safe);
}