FormOperations.php 3.71 KB
Newer Older
1 2
<?php

3
namespace Drupal\workspaces;
4 5 6 7 8 9

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\views\Form\ViewsExposedForm;
10
use Drupal\workspaces\Form\WorkspaceFormInterface;
11 12 13 14 15 16 17 18 19 20 21 22 23 24
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a class for reacting to form operations.
 *
 * @internal
 */
class FormOperations implements ContainerInjectionInterface {

  use StringTranslationTrait;

  /**
   * The workspace manager service.
   *
25
   * @var \Drupal\workspaces\WorkspaceManagerInterface
26 27 28 29 30 31
   */
  protected $workspaceManager;

  /**
   * Constructs a new FormOperations instance.
   *
32
   * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
33 34 35 36 37 38 39 40 41 42 43
   *   The workspace manager service.
   */
  public function __construct(WorkspaceManagerInterface $workspace_manager) {
    $this->workspaceManager = $workspace_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
44
      $container->get('workspaces.manager')
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    );
  }

  /**
   * Alters forms to disallow editing in non-default workspaces.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param string $form_id
   *   The form ID.
   *
   * @see hook_form_alter()
   */
  public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
    // No alterations are needed in the default workspace.
    if ($this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
      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);
  }

  /**
   * 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 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.'));
    }
  }

}