class EntityOperations
Same name in this branch
- main core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations
- main core/modules/views/src/Plugin/views/field/EntityOperations.php \Drupal\views\Plugin\views\field\EntityOperations
Same name and namespace in other branches
- 11.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations
- 11.x core/modules/workspaces/src/Hook/EntityOperations.php \Drupal\workspaces\Hook\EntityOperations
- 11.x core/modules/views/src/Plugin/views/field/EntityOperations.php \Drupal\views\Plugin\views\field\EntityOperations
- 10 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations
- 10 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations
- 10 core/modules/views/src/Plugin/views/field/EntityOperations.php \Drupal\views\Plugin\views\field\EntityOperations
- 9 core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations
- 9 core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations
- 9 core/modules/views/src/Plugin/views/field/EntityOperations.php \Drupal\views\Plugin\views\field\EntityOperations
- 8.9.x core/modules/content_moderation/src/EntityOperations.php \Drupal\content_moderation\EntityOperations
- 8.9.x core/modules/workspaces/src/EntityOperations.php \Drupal\workspaces\EntityOperations
- 8.9.x core/modules/views/src/Plugin/views/field/EntityOperations.php \Drupal\views\Plugin\views\field\EntityOperations
Defines a class for reacting to entity runtime hooks.
Hierarchy
- class \Drupal\workspaces\Hook\EntityOperations
Expanded class hierarchy of EntityOperations
File
-
core/
modules/ workspaces/ src/ Hook/ EntityOperations.php, line 26
Namespace
Drupal\workspaces\HookView source
class EntityOperations {
public function __construct(protected EntityTypeManagerInterface $entityTypeManager, protected WorkspaceManagerInterface $workspaceManager, protected WorkspaceTrackerInterface $workspaceTracker, protected WorkspaceInformationInterface $workspaceInfo, protected WorkspaceRepositoryInterface $workspaceRepository) {
}
/**
* Implements hook_entity_preload().
*/
public function entityPreload(array $ids, string $entity_type_id) : array {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if (!$this->workspaceInfo
->isEntityTypeSupported($entity_type)) {
return [];
}
return $this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityPreload($ids, $entity_type_id) ?? [];
}
/**
* Implements hook_entity_presave().
*/
public function entityPresave(EntityInterface $entity) : void {
if ($this->workspaceInfo
->isEntityIgnored($entity)) {
return;
}
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityPresave($entity);
}
/**
* Implements hook_entity_insert().
*/
public function entityInsert(EntityInterface $entity) : void {
if ($this->workspaceInfo
->isEntityIgnored($entity) || !$this->workspaceInfo
->isEntitySupported($entity)) {
return;
}
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityInsert($entity);
}
/**
* Implements hook_entity_update().
*/
public function entityUpdate(EntityInterface $entity) : void {
if ($this->workspaceInfo
->isEntityIgnored($entity) || !$this->workspaceInfo
->isEntitySupported($entity)) {
return;
}
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityUpdate($entity);
}
/**
* Implements hook_entity_translation_insert().
*/
public function entityTranslationInsert(EntityInterface $translation) : void {
if ($this->workspaceInfo
->isEntityIgnored($translation) || !$this->workspaceInfo
->isEntitySupported($translation) || $translation->isSyncing()) {
return;
}
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityTranslationInsert($translation);
}
/**
* Implements hook_entity_predelete().
*/
public function entityPredelete(EntityInterface $entity) : void {
if ($entity->getEntityTypeId() === 'workspace') {
$this->workspaceRepository
->resetCache();
}
if ($this->workspaceInfo
->isEntityIgnored($entity)) {
return;
}
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityPredelete($entity);
}
/**
* Implements hook_entity_delete().
*/
public function entityDelete(EntityInterface $entity) : void {
if (!$this->workspaceInfo
->isEntityTypeSupported($entity->getEntityType())) {
return;
}
$this->workspaceTracker
->deleteTrackedEntities(NULL, $entity->getEntityTypeId(), [
$entity->id(),
]);
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityDelete($entity);
}
/**
* Implements hook_entity_revision_delete().
*/
public function entityRevisionDelete(EntityInterface $entity) : void {
if (!$this->workspaceInfo
->isEntityTypeSupported($entity->getEntityType())) {
return;
}
$this->workspaceTracker
->deleteTrackedEntities(NULL, $entity->getEntityTypeId(), [
$entity->id(),
], [
$entity->getRevisionId(),
]);
$this->workspaceManager
->getActiveWorkspace()?->getProvider()
->entityRevisionDelete($entity);
}
/**
* Implements hook_entity_query_tag__TAG_alter() for 'latest_translated_affected_revision'.
*/
public function entityQueryTagLatestTranslatedAffectedRevisionAlter(QueryInterface $query) : void {
$entity_type = $this->entityTypeManager
->getDefinition($query->getEntityTypeId());
if (!$this->workspaceInfo
->isEntityTypeSupported($entity_type) || !$this->workspaceManager
->hasActiveWorkspace()) {
return;
}
$active_workspace = $this->workspaceManager
->getActiveWorkspace();
$tracked_entities = $this->workspaceTracker
->getTrackedEntities($active_workspace->id());
if (!isset($tracked_entities[$entity_type->id()])) {
return;
}
if ($revision_id = array_search($query->getMetaData('entity_id'), $tracked_entities[$entity_type->id()])) {
$query->condition($entity_type->getKey('revision'), $revision_id, '<=');
$conditions = $query->orConditionGroup();
$conditions->condition($entity_type->getRevisionMetadataKey('workspace'), $active_workspace->id());
$conditions->condition($entity_type->getRevisionMetadataKey('revision_default'), TRUE);
$query->condition($conditions);
}
}
/**
* Implements hook_form_alter().
*
* Alters entity forms to disallow concurrent editing in multiple workspaces.
*/
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',
];
}
}
/**
* Entity builder that marks all supported entities as pending revisions.
*/
public static function entityFormEntityBuild(string $entity_type_id, RevisionableInterface $entity, array &$form, FormStateInterface &$form_state) : void {
// Ensure that all entity forms are signaling that a new revision will be
// created.
$entity->setNewRevision(TRUE);
// Set the non-default revision flag so that validation constraints are also
// aware that a pending revision is about to be created.
$entity->isDefaultRevision(FALSE);
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary |
|---|---|---|---|
| EntityOperations::entityDelete | public | function | Implements hook_entity_delete(). |
| EntityOperations::entityFormAlter | public | function | Implements hook_form_alter(). |
| EntityOperations::entityFormEntityBuild | public static | function | Entity builder that marks all supported entities as pending revisions. |
| EntityOperations::entityInsert | public | function | Implements hook_entity_insert(). |
| EntityOperations::entityPredelete | public | function | Implements hook_entity_predelete(). |
| EntityOperations::entityPreload | public | function | Implements hook_entity_preload(). |
| EntityOperations::entityPresave | public | function | Implements hook_entity_presave(). |
| EntityOperations::entityQueryTagLatestTranslatedAffectedRevisionAlter | public | function | Implements hook_entity_query_tag__TAG_alter() for 'latest_translated_affected_revision'. |
| EntityOperations::entityRevisionDelete | public | function | Implements hook_entity_revision_delete(). |
| EntityOperations::entityTranslationInsert | public | function | Implements hook_entity_translation_insert(). |
| EntityOperations::entityUpdate | public | function | Implements hook_entity_update(). |
| EntityOperations::__construct | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.