function EntityOperations::entityFormAlter

Same name in other branches
  1. 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityFormAlter()
  2. 8.9.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityFormAlter()
  3. 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityFormAlter()

Alters entity forms to disallow concurrent editing in multiple 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/EntityOperations.php, line 297

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspaces

Code

public function entityFormAlter(array &$form, FormStateInterface $form_state, $form_id) {
    $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\FormOperations::validateForm()
    $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.