function EntityOperations::entityFormAlter
Same name in other branches
- 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityFormAlter()
- 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations::entityFormAlter()
- 11.x 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
File
-
core/
modules/ workspaces/ src/ EntityOperations.php, line 253
Class
- EntityOperations
- Defines a class for reacting to entity events.
Namespace
Drupal\workspacesCode
public function entityFormAlter(array &$form, FormStateInterface $form_state, $form_id) {
/** @var \Drupal\Core\Entity\RevisionableInterface $entity */
$entity = $form_state->getFormObject()
->getEntity();
if (!$this->workspaceManager
->isEntityTypeSupported($entity->getEntityType())) {
return;
}
// For supported entity types, signal the fact that this form is safe to use
// in a non-default workspace.
// @see \Drupal\workspaces\FormOperations::validateForm()
$form_state->set('workspace_safe', TRUE);
// 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'][] = [
get_called_class(),
'entityFormEntityBuild',
];
}
// Run the workspace conflict validation constraint when the entity form is
// being built so we can "disable" it early and display a message to the
// user, instead of allowing them to enter data that can never be saved.
foreach ($entity->validate()
->getEntityViolations() as $violation) {
if ($violation->getConstraint() instanceof EntityWorkspaceConflictConstraint) {
$form['#markup'] = $violation->getMessage();
$form['#access'] = FALSE;
continue;
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.