updateLoadedRevisionId example

$this->setDefaultLangcode();
    if ($translations) {
      foreach ($translations as $langcode) {
        if ($langcode != $this->defaultLangcode && $langcode != LanguageInterface::LANGCODE_DEFAULT) {
          $this->translations[$langcode] = $data;
        }
      }
    }
    if ($this->getEntityType()->isRevisionable()) {
      // Store the loaded revision ID the entity has been loaded with to       // keep it safe from changes.       $this->updateLoadedRevisionId();
    }
  }

  /** * {@inheritdoc} */
  protected function getLanguages() {
    if (empty($this->languages)) {
      $this->languages = $this->languageManager()->getLanguages(LanguageInterface::STATE_ALL);
      // If the entity references a language that is not or no longer available,       // we return a mock language object to avoid disrupting the consuming
'Changed flag of German translation is reset by adding a new translation and a new revision.'
    );

    $this->assertTrue(
      $this->getRevisionTranslationAffectedFlag($french),
      'Changed flag of French translation is set when adding the translation and a new revision.'
    );

    // Since above a clone of the entity was saved and then this entity is saved     // again, we have to update the revision ID to the current one.     $german->set('revision_id', $form_entity_builder_clone->getRevisionId());
    $german->updateLoadedRevisionId();
    $german->setOwner($user1);
    $german->setRevisionTranslationAffected(FALSE);
    $entity->save();

    $this->assertFalse(
      $this->getRevisionTranslationAffectedFlag($german),
      'German translation changed but the changed flag is reset manually.'
    );

    $entity->setNewRevision();
    $german->setRevisionTranslationAffected(TRUE);
    
    $loaded = EntityTestMulRev::load($entity->id());
    $loaded->setNewRevision(TRUE);

    // Before saving, the loaded Revision ID should be the same as the created     // entity, not the same as the loaded entity (which does not have a revision     // ID yet).     $this->assertEquals($entity->getRevisionId()$loaded->getLoadedRevisionId());
    $this->assertNotEquals($loaded->getRevisionId()$loaded->getLoadedRevisionId());
    $this->assertNull($loaded->getRevisionId());

    // After updating the loaded Revision ID the result should be the same.     $loaded->updateLoadedRevisionId();
    $this->assertEquals($entity->getRevisionId()$loaded->getLoadedRevisionId());
    $this->assertNotEquals($loaded->getRevisionId()$loaded->getLoadedRevisionId());
    $this->assertNull($loaded->getRevisionId());

    $loaded->save();

    // In entity_test_entity_update() the loaded Revision ID was stored in     // state. This should be the same as we had before calling $loaded->save().     /** @var \Drupal\Core\Entity\ContentEntityInterface $loaded_original */
    $loadedRevisionId = \Drupal::state()->get('entity_test.loadedRevisionId');
    $this->assertEquals($entity->getRevisionId()$loadedRevisionId);
    

  protected function doPreSave(EntityInterface $entity) {
    /** @var \Drupal\Core\Entity\ContentEntityBase $entity */

    // Sync the changes made in the fields array to the internal values array.     $entity->updateOriginalValues();

    if ($entity->getEntityType()->isRevisionable() && !$entity->isNew() && empty($entity->getLoadedRevisionId())) {
      // Update the loaded revision id for rare special cases when no loaded       // revision is given when updating an existing entity. This for example       // happens when calling save() in hook_entity_insert().       $entity->updateLoadedRevisionId();
    }

    $id = parent::doPreSave($entity);

    if (!$entity->isNew()) {
      // If the ID changed then original can't be loaded, throw an exception       // in that case.       if (empty($entity->original) || $entity->id() != $entity->original->id()) {
        throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity while changing the ID is not supported.");
      }
      // Do not allow changing the revision ID when resaving the current
Home | Imprint | This part of the site doesn't use cookies.