You are here

public function EntityOperations::formAlter in Workspace 8.2

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

src/EntityOperations.php, line 269

Class

EntityOperations
Defines a class for reacting to entity events.

Namespace

Drupal\workspace

Code

public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state
    ->getFormObject();
  if (!$form_object instanceof EntityFormInterface) {
    return;
  }
  $entity = $form_object
    ->getEntity();
  if (!$this->workspaceManager
    ->isEntityTypeSupported($entity
    ->getEntityType())) {
    return;
  }

  /** @var \Drupal\workspace\WorkspaceAssociationStorageInterface $workspace_association_storage */
  $workspace_association_storage = $this->entityTypeManager
    ->getStorage('workspace_association');
  if ($workspace_ids = $workspace_association_storage
    ->getEntityTrackingWorkspaceIds($entity)) {

    // An entity can only be edited in one workspace.
    $workspace_id = reset($workspace_ids);
    if ($workspace_id !== $this->workspaceManager
      ->getActiveWorkspace()
      ->id()) {
      $workspace = $this->entityTypeManager
        ->getStorage('workspace')
        ->load($workspace_id);
      $form['#markup'] = $this
        ->t('The content is being edited in the %label workspace.', [
        '%label' => $workspace
          ->label(),
      ]);
      $form['#access'] = FALSE;
    }
  }
}