trackEntity example

try {
      $transaction = $this->database->startTransaction();
      foreach ($this->getDifferringRevisionIdsOnSource() as $entity_type_id => $revision_difference) {
        $revisions_on_source = $this->entityTypeManager->getStorage($entity_type_id)
          ->loadMultipleRevisions(array_keys($revision_difference));

        /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
        foreach ($revisions_on_source as $revision) {
          // Track all the differing revisions from the source workspace in           // the context of the target workspace. This will automatically           // update all the descendants of the target workspace as well.           $this->workspaceAssociation->trackEntity($revision$this->targetWorkspace);
        }

        // Since we're not saving entity objects, we need to invalidate the list         // cache tags manually.         $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
        $this->cacheTagsInvalidator->invalidateTags($entity_type->getListCacheTags());
      }
    }
    catch (\Exception $e) {
      if (isset($transaction)) {
        $transaction->rollBack();
      }

  public function entityInsert(EntityInterface $entity) {
    /** @var \Drupal\Core\Entity\RevisionableInterface|\Drupal\Core\Entity\EntityPublishedInterface $entity */
    // Only run if the entity type can belong to a workspace and we are in a     // non-default workspace.     if (!$this->workspaceManager->shouldAlterOperations($entity->getEntityType())) {
      return;
    }

    $this->workspaceAssociation->trackEntity($entity$this->workspaceManager->getActiveWorkspace());

    // When an entity is newly created in a workspace, it should be published in     // that workspace, but not yet published on the live workspace. It is first     // saved as unpublished for the default revision, then immediately a second     // revision is created which is published and attached to the workspace.     // This ensures that the published version of the entity does not 'leak'     // into the live site. This differs from edits to existing entities where     // there is already a valid default revision for the live workspace.     if (isset($entity->_initialPublished)) {
      // Operate on a clone to avoid changing the entity prior to subsequent       // hook_entity_insert() implementations.
Home | Imprint | This part of the site doesn't use cookies.