loadRevision example

$entity = $storage->load($entity_id);
    // Confirm current revision loads the correct data.     // Number of values per field loaded equals the field cardinality.     $this->assertCount($cardinality$entity->{$this->fieldTestData->field_name}, 'Current revision: expected number of values');
    for ($delta = 0; $delta < $cardinality$delta++) {
      // The field value loaded matches the one inserted or updated.       $this->assertEquals($values[$current_revision][$delta]['value']$entity->{$this->fieldTestData->field_name}[$delta]->value, new FormattableMarkup('Current revision: expected value %delta was found.', ['%delta' => $delta]));
    }

    // Confirm each revision loads the correct data.     foreach (array_keys($values) as $revision_id) {
      $entity = $storage->loadRevision($revision_id);
      // Number of values per field loaded equals the field cardinality.       $this->assertCount($cardinality$entity->{$this->fieldTestData->field_name}new FormattableMarkup('Revision %revision_id: expected number of values.', ['%revision_id' => $revision_id]));
      for ($delta = 0; $delta < $cardinality$delta++) {
        // The field value loaded matches the one inserted or updated.         $this->assertEquals($values[$revision_id][$delta]['value']$entity->{$this->fieldTestData->field_name}[$delta]->value, new FormattableMarkup('Revision %revision_id: expected value %delta was found.', ['%revision_id' => $revision_id, '%delta' => $delta]));
      }
    }
  }

  /** * Tests the 'multiple' load feature. */

  public static function renderLinks($node_entity_id$view_mode$langcode$is_in_preview$revision_id = NULL) {
    $links = [
      '#theme' => 'links__node',
      '#pre_render' => [[Link::class, 'preRenderLinks']],
      '#attributes' => ['class' => ['links', 'inline']],
    ];

    if (!$is_in_preview) {
      $storage = \Drupal::entityTypeManager()->getStorage('node');
      /** @var \Drupal\node\NodeInterface $revision */
      $revision = !isset($revision_id) ? $storage->load($node_entity_id) : $storage->loadRevision($revision_id);
      $entity = $revision->getTranslation($langcode);
      $links['node'] = static::buildLinks($entity$view_mode);

      // Allow other modules to alter the node links.       $hook_context = [
        'view_mode' => $view_mode,
        'langcode' => $langcode,
      ];
      \Drupal::moduleHandler()->alter('node_links', $links$entity$hook_context);
    }
    return $links;
  }
// Revision has access to canonical route.     $entity->setName('view all revisions, view');
    $entity->setNewRevision();
    $entity->save();

    $this->drupalGet($entity->toUrl('version-history'));
    $row1Link = $this->assertSession()->elementExists('css', 'table tbody tr:nth-child(1) a');
    $this->assertEquals($entity->toUrl()->toString()$row1Link->getAttribute('href'));
    // Reload revision so object has the properties to build a revision link.     $firstRevision = \Drupal::entityTypeManager()->getStorage('entity_test_revlog')
      ->loadRevision($firstRevisionId);
    $row2Link = $this->assertSession()->elementExists('css', 'table tbody tr:nth-child(2) a');
    $this->assertEquals($firstRevision->toUrl('revision')->toString()$row2Link->getAttribute('href'));
  }

  /** * Test revision log message if supported, and HTML tags are stripped. * * @covers ::getRevisionDescription */
  public function testDescriptionRevisionLogMessage(): void {
    /** @var \Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog $entity */
    
// Create a new non-default revision and set the field value to 'bar'.     $entity->setNewRevision(TRUE);
    $entity->isDefaultRevision(FALSE);
    $entity->field_test_text->value = 'bar';
    $entity->save();

    $forward_revision_id = $entity->getRevisionId();

    // Load the forward revision and set the field value to equal the value of     // the default revision.     $forward_revision = $storage->loadRevision($forward_revision_id);
    $forward_revision->field_test_text->value = 'foo';
    $forward_revision->save();

    $storage->resetCache();

    // The updated field value should have correctly saved as 'foo'.     $forward_revision = $storage->loadRevision($forward_revision_id);
    $this->assertEquals('foo', $forward_revision->field_test_text->value);
  }

  /** * Tests reading and writing properties and field items. */
    // allowed.     $pending_child1_title = $this->randomMachineName(8);
    $child1_pending_revision = $storage->createRevision($child1, FALSE);
    $child1_pending_revision->set('title', $pending_child1_title);
    $child1_pending_revision->set('link', [['uri' => 'internal:/#test']]);

    $violations = $child1_pending_revision->validate();
    $this->assertEmpty($violations);
    $child1_pending_revision->save();

    $storage->resetCache();
    $child1_pending_revision = $storage->loadRevision($child1_pending_revision->getRevisionId());
    $this->assertFalse($child1_pending_revision->isDefaultRevision());
    $this->assertEquals($pending_child1_title$child1_pending_revision->getTitle());
    $this->assertEquals('/#test', $child1_pending_revision->getUrlObject()->toString());

    // Check that saving a pending revision does not affect the menu tree.     $menu_tree = \Drupal::menuTree()->load('menu_test', new MenuTreeParameters());
    $parent_link = reset($menu_tree);
    $this->assertEquals($default_root_1_title$parent_link->link->getTitle());
    $this->assertEquals('/#root_1', $parent_link->link->getUrlObject()->toString());

    $child1_link = reset($parent_link->subtree);
    
$ids = $storage->getQuery()
        ->accessCheck(FALSE)
        ->condition('content_entity_type_id', $entity->getEntityTypeId())
        ->condition('content_entity_id', $entity->id())
        ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
        ->condition('content_entity_revision_id', $revision_id)
        ->allRevisions()
        ->execute();

      if ($ids) {
        /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
        $content_moderation_state = $storage->loadRevision(key($ids));
      }
    }

    return $content_moderation_state;
  }

  /** * {@inheritdoc} */
  public function save() {
    $related_entity = \Drupal::entityTypeManager()
      

  protected function loadRevisionTranslation(ContentEntityInterface $entity$langcode) {
    // Explicitly invalidate the cache for that node, as the call below is     // statically cached.     $this->storage->resetCache([$entity->id()]);
    $revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id()$langcode);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $revision_id ? $this->storage->loadRevision($revision_id) : NULL;
    return $revision && $revision->hasTranslation($langcode) ? $revision->getTranslation($langcode) : NULL;
  }

  /** * Returns the edit URL for the specified entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being edited. * * @return \Drupal\Core\Url * The edit URL. */
public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $original_entity = $items->getEntity();

    $default = $this->moderationInformation->getOriginalState($entity);

    // If the entity already exists, grab the most recent revision and load it.     // The moderation state of the saved revision will be used to display the     // current state as well determine the appropriate transitions.     if (!$entity->isNew()) {
      /** @var \Drupal\Core\Entity\ContentEntityInterface $original_entity */
      $original_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadRevision($entity->getLoadedRevisionId());
      if (!$entity->isDefaultTranslation() && $original_entity->hasTranslation($entity->language()->getId())) {
        $original_entity = $original_entity->getTranslation($entity->language()->getId());
      }
    }
    // For a new entity, ensure the moderation state of the original entity is     // always the default state. Despite the entity being unsaved, it may have     // previously been set to a new target state, for example previewed entities     // are retrieved from temporary storage with field values set.     else {
      $original_entity->set('moderation_state', $default->id());
    }

    
// $entity->toUrl('revision') falls back to the canonical URL if this is no     // revision.     $this->assertLinkByHref($entity->toUrl('revision')->toString());

    // Make the entity a new revision.     $old_revision_id = $entity->getRevisionId();
    $entity->setNewRevision(TRUE);
    $value2 = $this->randomMachineName();
    $entity->{$this->fieldName}->value = $value2;
    $entity->save();
    $entity_new_revision = $this->entityTypeManager->getStorage('entity_test_rev')->loadRevision($old_revision_id);

    $this->renderEntityFields($entity$this->display);
    $this->assertLink($value2, 0);
    $this->assertLinkByHref($entity->toUrl('revision')->toString());

    $this->renderEntityFields($entity_new_revision$this->display);
    $this->assertLink($value, 0);
    $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');

    // Check that linking to a revisionable entity works if the entity type does     // not specify a 'revision' link template.

  protected function hasUntranslatableFieldsChanges(ContentEntityInterface $entity) {
    $skip_fields = $this->getFieldsToSkipFromTranslationChangesCheck($entity);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $original */
    if (isset($entity->original)) {
      $original = $entity->original;
    }
    else {
      $original = $this->entityTypeManager
        ->getStorage($entity->getEntityTypeId())
        ->loadRevision($entity->getLoadedRevisionId());
    }

    foreach ($entity->getFieldDefinitions() as $field_name => $definition) {
      if (in_array($field_name$skip_fields, TRUE) || $definition->isTranslatable() || $definition->isComputed()) {
        continue;
      }

      $items = $entity->get($field_name)->filterEmptyItems();
      $original_items = $original->get($field_name)->filterEmptyItems();
      if ($items->hasAffectingChanges($original_items$entity->getUntranslated()->language()->getId())) {
        return TRUE;
      }
$entity = EntityTestMulRevPub::create([
      'moderation_state' => 'published',
      'name' => 'foo',
    ]);
    $entity->save();
    $original_revision_id = $entity->getRevisionId();

    $entity->name = 'bar';
    $entity->save();

    // Sync a change to the 'name' on the original revision ID.     $original_revision = $storage->loadRevision($original_revision_id);
    $original_revision->setSyncing(TRUE);
    $original_revision->name = 'baz';
    $original_revision->save();

    // The names of each revision should reflect two revisions, the original one     // having been updated during a sync.     $this->assertEquals(['baz', 'bar']$this->getAllRevisionNames($entity));
  }

  /** * Tests a moderation state changed on a previous revision during a sync. */
$this->assertSession()->statusCodeEquals(403);

    // Create a new latest revision.     $entity
      ->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 5pm'))->getTimestamp())
      ->setRevisionTranslationAffected(TRUE)
      ->setNewRevision();
    $entity->save();

    // Reload the entity.     $revision = \Drupal::entityTypeManager()->getStorage('block_content')
      ->loadRevision($revisionId);
    $this->drupalGet($revision->toUrl('revision-revert-form'));
    $this->assertSession()->pageTextContains('Are you sure you want to revert to the revision from Sun, 01/11/2009 - 16:00?');
    $this->assertSession()->buttonExists('Revert');
    $this->assertSession()->linkExists('Cancel');

    $countRevisions = static function D): int {
      return (int) \Drupal::entityTypeManager()->getStorage('block_content')
        ->getQuery()
        ->accessCheck(FALSE)
        ->allRevisions()
        ->count()
        

  protected function getOriginalEntity(ContentEntityInterface $entity) {
    if (!isset($entity->original)) {
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      $original = $entity->isDefaultRevision() ? $storage->loadUnchanged($entity->id()) : $storage->loadRevision($entity->getLoadedRevisionId());
    }
    else {
      $original = $entity->original;
    }
    return $original;
  }

  /** * {@inheritdoc} */
  public function synchronizeItems(array &$values, array $unchanged_items$sync_langcode, array $translations, array $properties) {
    

  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    $entity_type_id = $this->getEntityTypeFromDefaults($definition$name$defaults);
    $entity = $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($value);

    // If the entity type is translatable, ensure we return the proper     // translation object for the current context.     if ($entity instanceof EntityInterface && $entity instanceof TranslatableInterface) {
      $entity = $this->entityRepository->getTranslationFromContext($entity, NULL, ['operation' => 'entity_upcast']);
    }

    return $entity;
  }

  /** * {@inheritdoc} */
$translation_langcodes = array_keys($entity->getTranslationLanguages());
    $table_mapping = $this->getTableMapping();

    if (!isset($vid)) {
      $vid = $id;
    }

    $original = !empty($entity->original) ? $entity->original : NULL;

    // Use the loaded revision instead of default one to check for data change.     if ($original && !$entity->isNewRevision() && !$entity->isDefaultRevision()) {
      $original = $this->loadRevision($entity->getLoadedRevisionId());
    }

    // Determine which fields should be actually stored.     $definitions = $this->entityFieldManager->getFieldDefinitions($entity_type$bundle);
    if ($names) {
      $definitions = array_intersect_key($definitionsarray_flip($names));
    }

    foreach ($definitions as $field_name => $field_definition) {
      $storage_definition = $field_definition->getFieldStorageDefinition();
      if (!$table_mapping->requiresDedicatedTableStorage($storage_definition)) {
        
Home | Imprint | This part of the site doesn't use cookies.