getLatestRevisionId example


  public function validate($entity, Constraint $constraint) {
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    if (isset($entity) && !$entity->isNew()) {
      $active_workspace = $this->workspaceManager->getActiveWorkspace();

      // Get the latest revision of the entity in order to check if it's being       // edited in a different workspace.       $latest_revision = $this->workspaceManager->executeOutsideWorkspace(function D) use ($entity) {
        $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
        return $storage->loadRevision($storage->getLatestRevisionId($entity->id()));
      });

      // If the latest revision of the entity is tracked in a workspace, it can       // only be edited in that workspace or one of its descendants.       if ($latest_revision_workspace = $latest_revision->workspace->entity) {
        $descendants_and_self = $this->workspaceRepository->getDescendantsAndSelf($latest_revision_workspace->id());

        if (!$active_workspace || !in_array($active_workspace->id()$descendants_and_self, TRUE)) {
          $this->context->buildViolation($constraint->message)
            ->setParameter('%label', $latest_revision_workspace->label())
            ->addViolation();
        }


  /** * {@inheritdoc} */
  public function getLatestTranslationAffectedRevisionId($entity_id$langcode) {
    if (!$this->entityType->isRevisionable()) {
      return NULL;
    }

    if (!$this->entityType->isTranslatable()) {
      return $this->getLatestRevisionId($entity_id);
    }

    if (!isset($this->latestRevisionIds[$entity_id][$langcode])) {
      $result = $this->getQuery()
        ->allRevisions()
        ->condition($this->entityType->getKey('id')$entity_id)
        ->condition($this->entityType->getKey('revision_translation_affected'), 1, '=', $langcode)
        ->range(0, 1)
        ->sort($this->entityType->getKey('revision'), 'DESC')
        ->accessCheck(FALSE)
        ->execute();

      

  protected function isFirstTimeModeration(ContentEntityInterface $entity) {
    $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
    $original_entity = $storage->loadRevision($storage->getLatestRevisionId($entity->id()));

    if ($original_entity) {
      $original_id = $original_entity->moderation_state;
    }

    return !($entity->moderation_state && $original_entity && $original_id);
  }

}
$workflow = $this->createEditorialWorkflow();
    $this->addEntityTypeAndBundleToWorkflow($workflow$entity->getEntityTypeId()$entity->bundle());

    $entity = $this->reloadEntity($entity);
    $entity->moderation_state = 'draft';
    $entity->save();

    $storage->deleteRevision($entity->getRevisionId());

    $entity = $this->reloadEntity($entity);
    $this->assertEquals('published', $entity->moderation_state->value);
    $this->assertEquals($original_revision_id$storage->getLatestRevisionId($entity->id()));
  }

  /** * Tests removal of content moderation state translations. * * @dataProvider basicModerationTestCases */
  public function testContentModerationStateTranslationDataRemoval($entity_type_id) {
    // Test content moderation state translation deletion.     if ($this->entityTypeManager->getDefinition($entity_type_id)->isTranslatable()) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      
$value = $this->isNew() || $this->get($revision_default_key)->value;
    return $value;
  }

  /** * {@inheritdoc} */
  public function isLatestRevision() {
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());

    return $this->getLoadedRevisionId() == $storage->getLatestRevisionId($this->id());
  }

  /** * {@inheritdoc} */
  public function isLatestTranslationAffectedRevision() {
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());

    return $this->getLoadedRevisionId() == $storage->getLatestTranslationAffectedRevisionId($this->id()$this->language()->getId());
  }

  
const LATEST_VERSION = 'latest-version';

  /** * {@inheritdoc} */
  protected function getRevisionId(EntityInterface $entity$version_argument) {
    assert($entity instanceof RevisionableInterface);
    switch ($version_argument) {
      case static::WORKING_COPY:
        /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */
        $entity_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
        return static::ensureVersionExists($entity_storage->getLatestRevisionId($entity->id()));

      case static::LATEST_VERSION:
        // The already loaded revision will be the latest version by default.         // @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::buildQuery().         return $entity->getLoadedRevisionId();

      default:
        $message = sprintf('The version specifier must be either `%s` or `%s`, `%s` given.', static::LATEST_VERSION, static::WORKING_COPY, $version_argument);
        throw new InvalidVersionIdentifierException($message);
    }
  }

}

  protected function generateNewEntityLabel(ContentEntityInterface $revision$previous_revision_id$next = FALSE) {
    $language_label = $revision->language()->getName();
    $revision_type = $revision->isDefaultRevision() ? 'Default' : 'Pending';
    $revision_id = $next ? $this->storage->getLatestRevisionId($revision->id()) + 1 : $revision->getLoadedRevisionId();
    return sprintf('%s (%s %d -> %d)', $language_label$revision_type$previous_revision_id$revision_id);
  }

  /** * Formats an assertion message. * * @param string $message * The human-readable message. * * @return string * The formatted message. */

  protected function assertLatestRevisionFieldValues(int $entity_id, array $expected_values): void {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->storage->loadRevision($this->storage->getLatestRevisionId($entity_id));
    @[$revision_id$target_id_en$target_id_it$alt_en$alt_it] = $expected_values;
    $this->assertEquals($revision_id$entity->getRevisionId());
    $this->assertEquals($target_id_en$entity->get($this->fieldName)->target_id);
    $this->assertEquals($alt_en$entity->get($this->fieldName)->alt);
    if ($entity->hasTranslation('it')) {
      $it_translation = $entity->getTranslation('it');
      $this->assertEquals($target_id_it$it_translation->get($this->fieldName)->target_id);
      $this->assertEquals($alt_it$it_translation->get($this->fieldName)->alt);
    }
  }

}
// Load the created entity and create a new pending revision.     $pending_revision = EntityTestMulRev::load($entity->id());
    $pending_revision->setName($this->randomString());
    $pending_revision->setNewRevision(TRUE);
    $pending_revision->isDefaultRevision(FALSE);

    // Check that no revision affecting Italian is available, given that no     // Italian translation has been created yet.     /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
    $this->assertNull($storage->getLatestTranslationAffectedRevisionId($entity->id(), 'it'));
    $this->assertEquals($pending_revision->getLoadedRevisionId()$storage->getLatestRevisionId($entity->id()));

    // The pending revision should still be marked as the latest affected one     // before it is saved.     $this->assertTrue($pending_revision->isLatestTranslationAffectedRevision());
    $pending_revision->save();
    $this->assertTrue($pending_revision->isLatestTranslationAffectedRevision());

    // Load the default revision and check that it is not marked as the latest     // (translation-affected) revision.     $default_revision = EntityTestMulRev::load($entity->id());
    $this->assertFalse($default_revision->isLatestRevision());
    
$node->save();

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'unmoderated');
    $workflow->save();

    $translation = $node->addTranslation('de');
    $translation->moderation_state = 'draft';
    $translation->save();

    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $node = $node_storage->loadRevision($node_storage->getLatestRevisionId($node->id()));

    $this->assertEquals('published', $node->moderation_state->value);
    $this->assertEquals('draft', $translation->moderation_state->value);
    $this->assertTrue($node->isPublished());
    $this->assertFalse($translation->isPublished());
  }

}
$this->drupalGet('node/1/layout');

    // Add an inline block.     $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body');
    $this->assertSaveLayout();
    $this->drupalGet('node/1');

    $assert_session->pageTextContains('The DEFAULT block body');

    /** @var \Drupal\node\NodeStorageInterface $node_storage */
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $original_revision_id = $node_storage->getLatestRevisionId(1);

    // Create a new revision.     $this->drupalGet('node/1/edit');
    $page->findField('title[0][value]')->setValue('Node updated');
    $page->pressButton('Save');

    $this->drupalGet('node/1');
    $assert_session->pageTextContains('The DEFAULT block body');

    $assert_session->linkExists('Revisions');

    

  protected function loadTranslation(NodeInterface $node$langcode) {
    /** @var \Drupal\node\NodeStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')->getStorage('node');
    // Explicitly invalidate the cache for that node, as the call below is     // statically cached.     $storage->resetCache([$node->id()]);
    /** @var \Drupal\node\NodeInterface $node */
    $node = $storage->loadRevision($storage->getLatestRevisionId($node->id()));
    return $node->getTranslation($langcode);
  }

  /** * Asserts that this is the "latest version" page for the specified node. * * @param \Drupal\node\NodeInterface $node * A node object. * * @internal */
  
      $revision = $revision_id ? $this->loadRevision($entity$revision_id) : NULL;
      if (!$revision || ($revision->wasDefaultRevision() && !$revision->isDefaultRevision())) {
        $revision = NULL;
      }
    }

    // Fall back to the latest revisions if no affected revision for the current     // content language could be found. This is acceptable as it means the     // entity is not translated. This is the correct logic also on monolingual     // sites.     if (!isset($revision)) {
      $revision_id = $storage->getLatestRevisionId($entity->id());
      $revision = $this->loadRevision($entity$revision_id);
    }

    return $revision;
  }

  /** * Loads the specified entity revision. * * @param \Drupal\Core\Entity\RevisionableInterface $entity * The default revision of the entity being converted. * @param string $revision_id * The identifier of the revision to be loaded. * * @return \Drupal\Core\Entity\RevisionableInterface * An entity revision object. */
Home | Imprint | This part of the site doesn't use cookies.