function EntityOperations::entityFormAlter

Implements hook_form_alter().

Alters entity forms to disallow concurrent editing in multiple workspaces.

Attributes

#[Hook('form_alter', order: Order::First)]

File

core/modules/workspaces/src/Hook/EntityOperations.php, line 300

Class

EntityOperations
Defines a class for reacting to entity runtime hooks.

Namespace

Drupal\workspaces\Hook

Code

public function entityFormAlter(array &$form, FormStateInterface $form_state, string $form_id) : void {
  if (!$form_state->getFormObject() instanceof EntityFormInterface) {
    return;
  }
  $entity = $form_state->getFormObject()
    ->getEntity();
  if (!$this->workspaceInfo
    ->isEntitySupported($entity) && !$this->workspaceInfo
    ->isEntityIgnored($entity)) {
    return;
  }
  // For supported and ignored entity types, signal the fact that this form is
  // safe to use in a workspace.
  // @see \Drupal\workspaces\Hook\FormOperations::formAlter()
  $form_state->set('workspace_safe', TRUE);
  // There is nothing more to do for ignored entity types.
  if ($this->workspaceInfo
    ->isEntityIgnored($entity)) {
    return;
  }
  // Add an entity builder to the form which marks the edited entity object as
  // a pending revision. This is needed so validation constraints like
  // \Drupal\path\Plugin\Validation\Constraint\PathAliasConstraintValidator
  // know in advance (before hook_entity_presave()) that the new revision will
  // be a pending one.
  if ($this->workspaceManager
    ->hasActiveWorkspace()) {
    $form['#entity_builders'][] = [
      static::class,
      'entityFormEntityBuild',
    ];
  }
}

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