createRevision example

$content_moderation_state->addTranslation($entity_langcode);
        }
        if ($content_moderation_state->language()->getId() !== $entity_langcode) {
          $content_moderation_state = $content_moderation_state->getTranslation($entity_langcode);
        }
      }
    }

    // If a new revision of the content has been created, add a new content     // moderation state revision.     if (!$content_moderation_state->isNew() && $content_moderation_state->content_entity_revision_id->value != $entity_revision_id) {
      $content_moderation_state = $storage->createRevision($content_moderation_state$entity->isDefaultRevision());
    }

    // Create the ContentModerationState entity for the inserted entity.     $moderation_state = $entity->moderation_state->value;
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if (!$moderation_state) {
      $moderation_state = $workflow->getTypePlugin()->getInitialState($entity)->id();
    }

    $content_moderation_state->set('content_entity_revision_id', $entity_revision_id);
    $content_moderation_state->set('moderation_state', $moderation_state);
    


    return $form;
  }

  /** * {@inheritdoc} */
  protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state) {
    $revert_untranslated_fields = (bool) $form_state->getValue('revert_untranslated_fields');
    $translation = $revision->getTranslation($this->langcode);
    return $this->nodeStorage->createRevision($translation, TRUE, $revert_untranslated_fields);
  }

}
'vid' => $vocabulary_id,
      'parent' => $parent1->id(),
    ]);
    $violations = $child1->validate();
    $this->assertEmpty($violations);
    $child1->save();

    $validation_message = 'You can only change the hierarchy for the published version of this term.';

    // Add a pending revision without changing the term parent.     $pending_name = $this->randomMachineName();
    $child_pending = $term_storage->createRevision($child1, FALSE);
    $child_pending->name = $pending_name;
    $violations = $child_pending->validate();
    $this->assertEmpty($violations);

    // Add a pending revision and change the parent.     $child_pending = $term_storage->createRevision($child1, FALSE);
    $child_pending->parent = $parent2;
    $violations = $child_pending->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals($validation_message$violations[0]->getMessage());
    $this->assertEquals('parent', $violations[0]->getPropertyPath());

    
$this->assertSame($entity$active);

    // Check that the correct active variant is returned for a non-translatable     // revisionable entity.     $entity_type_id = 'entity_test_rev';
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
    $values = ['name' => $this->randomString()];
    $entity = $storage->create($values);
    $storage->save($entity);

    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $storage->createRevision($entity, FALSE);
    $revision->save();
    $active = $this->entityRepository->getActive($entity_type_id$entity->id()$en_contexts);
    $this->assertEntityType($active$entity_type_id);
    $this->assertSame($revision->getLoadedRevisionId()$active->getLoadedRevisionId());

    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision2 */
    $revision2 = $storage->createRevision($revision);
    $revision2->save();
    $active = $this->entityRepository->getActive($entity_type_id$entity->id()$en_contexts);
    $this->assertSame($revision2->getLoadedRevisionId()$active->getLoadedRevisionId());

    

  public function testAccess(string $operation, bool $published, bool $reusable, array $permissions, bool $isLatest, ?string $parent_access, string $expected_access, ?string $expected_access_message = NULL) {
    /** @var \Drupal\Core\Entity\RevisionableStorageInterface $entityStorage */
    $entityStorage = \Drupal::entityTypeManager()->getStorage('block_content');

    $loadRevisionId = NULL;
    if (!$isLatest) {
      // Save a historical revision, then setup for a new revision to be saved.       $this->blockEntity->save();
      $loadRevisionId = $this->blockEntity->getRevisionId();
      $this->blockEntity = $entityStorage->createRevision($this->blockEntity);
    }

    $published ? $this->blockEntity->setPublished() : $this->blockEntity->setUnpublished();
    $reusable ? $this->blockEntity->setReusable() : $this->blockEntity->setNonReusable();

    $user = User::create([
      'name' => 'Someone',
      'mail' => 'hi@example.com',
    ]);

    if ($permissions) {
      

  public function entityPrepareForm(EntityInterface $entity$operation, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state->getFormObject();

    if ($this->isModeratedEntityEditForm($form_object) && !$entity->isNew()) {
      // Generate a proper revision object for the current entity. This allows       // to correctly handle translatable entities having pending revisions.       /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      /** @var \Drupal\Core\Entity\ContentEntityInterface $new_revision */
      $new_revision = $storage->createRevision($entity, FALSE);

      // Restore the revision ID as other modules may expect to find it still       // populated. This will reset the "new revision" flag, however the entity       // object will be marked as a new revision again on submit.       // @see \Drupal\Core\Entity\ContentEntityForm::buildEntity()       $revision_key = $new_revision->getEntityType()->getKey('revision');
      $new_revision->set($revision_key$new_revision->getLoadedRevisionId());
      $form_object->setEntity($new_revision);
    }
  }

  
return $form;
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_state->get('entity');
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
    $entity = $storage->createRevision($entity$entity->isDefaultRevision());

    $new_state = $form_state->getValue('new_state');

    $entity->set('moderation_state', $new_state);

    if ($entity instanceof RevisionLogInterface) {
      $entity->setRevisionCreationTime($this->time->getRequestTime());
      $entity->setRevisionLogMessage($form_state->getValue('revision_log'));
      $entity->setRevisionUserId($this->currentUser()->id());
    }
    $entity->save();

    
$this->assertFalse($method->invoke($storage$entity));

    // Check that a revisionable not translated entity is handled correctly.     $entity = EntityTestMulRev::create();
    $entity->save();
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertFalse($method->invoke($storage$entity));

    // Check that a revisionable translated pending revision is handled     // correctly.     /** @var \Drupal\Core\Entity\ContentEntityInterface $new_revision */
    $new_revision = $storage->createRevision($entity, FALSE);
    $new_revision->addTranslation('it');
    $new_revision->save();
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $storage->loadUnchanged($entity->id());
    $this->assertEmpty($entity->getTranslationLanguages(FALSE));
    $this->assertNotEmpty($new_revision->getTranslationLanguages(FALSE));
    $this->assertTrue($method->invoke($storage$entity));

    // Check that a revisionable translated default revision is handled     // correctly.     $new_revision->isDefaultRevision(TRUE);
    
$default_root_2_title = $this->randomMachineName(8);
    $root_2 = $storage->create([
      'title' => $default_root_2_title,
      'link' => [['uri' => 'internal:/#root_2']],
      'menu_name' => 'menu_test',
    ]);
    $root_2->save();

    // Check that changing the title and the link in a pending revision is     // 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());
    
$child_2 = $this->addMenuLink($root_2->getPluginId(), '/', $menu_2->id());

    $this->drupalGet('admin/structure/menu/manage/' . $menu_2->id());
    $assert_session->pageTextNotContains($menu_2->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');

    $this->drupalGet('admin/structure/menu/manage/' . $menu_1->id());
    $assert_session->pageTextNotContains($menu_1->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');

    // Create a pending revision for one of the menu links and check that it can     // no longer be re-parented in the UI. We can not create pending revisions     // through the UI yet so we have to use API calls.     \Drupal::entityTypeManager()->getStorage('menu_link_content')->createRevision($child_2, FALSE)->save();

    $this->drupalGet('admin/structure/menu/manage/' . $menu_2->id());
    $assert_session->pageTextContains($menu_2->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');

    // Check that the 'Enabled' checkbox is hidden for a pending revision.     $this->assertNotEmpty($this->cssSelect('input[name="links[menu_plugin_id:' . $root_2->getPluginId() . '][enabled]"]'), 'The publishing status of a default revision can be changed.');
    $this->assertEmpty($this->cssSelect('input[name="links[menu_plugin_id:' . $child_2->getPluginId() . '][enabled]"]'), 'The publishing status of a pending revision can not be changed.');

    $this->drupalGet('admin/structure/menu/manage/' . $menu_1->id());
    $assert_session->pageTextNotContains($menu_1->label() . ' contains 1 menu link with pending revisions. Manipulation of a menu tree having links with pending revisions is not supported, but you can re-enable manipulation by getting each menu link to a published state.');

    
$field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
    // Enable overrides.     $this->drupalGet("{$field_ui_prefix}/display/default");
    $this->submitForm(['layout[enabled]' => TRUE], 'Save');
    $this->drupalGet("{$field_ui_prefix}/display/default");
    $this->submitForm(['layout[allow_custom]' => TRUE], 'Save');

    $storage = $this->container->get('entity_type.manager')->getStorage('node');
    $node = $storage->load(1);
    // Create a pending revision.     $pending_revision = $storage->createRevision($node, FALSE);
    $pending_revision->set('title', 'The pending title of the first node');
    $pending_revision->save();

    // The original node title is available when viewing the node, but the     // pending title is visible within the Layout Builder UI.     $this->drupalGet('node/1');
    $assert_session->pageTextContains('The first node title');
    $page->clickLink('Layout');
    $assert_session->pageTextNotContains('The first node title');
    $assert_session->pageTextContains('The pending title of the first node');
  }

  
$account->save();

    $title = $this->randomMachineName();
    $node = Node::create([
      'type' => 'article',
      'title' => $title,
      'uid' => $account->id(),
    ]);
    $node->save();

    /** @var \Drupal\node\NodeInterface $pending_revision */
    $pending_revision = $this->storage->createRevision($node, FALSE);
    $draft_title = $title . ' draft';
    $pending_revision->setTitle($draft_title);
    $pending_revision->save();

    $build = $this->viewBuilder->view($node, 'teaser');
    $output = (string) $this->renderer->renderPlain($build);
    $this->assertStringContainsString("title=\"$title\"", $output);

    $build = $this->viewBuilder->view($pending_revision, 'teaser');
    $output = (string) $this->renderer->renderPlain($build);
    $this->assertStringContainsString("title=\"$draft_title\"", $output);
  }
'value1' => 'dedicated table - ' . $i . ' - delta 1 - value 1 - ro',
              'value2' => 'dedicated table - ' . $i . ' - delta 1 - value 2 - ro',
            ],
          ],
        ]);
        $translation->save();
      }
      $this->testEntities[$entity->id()] = $entity;

      if ($revisionable) {
        // Create a new pending revision.         $revision_2 = $storage->createRevision($entity, FALSE);
        $revision_2->name = 'test entity - ' . $i . ' - en - rev2';
        $revision_2->new_bundle_field = 'bundle field - ' . $i . ' - en - rev2';
        $revision_2->test_multiple_properties->value1 = 'shared table - ' . $i . ' - value 1 - en - rev2';
        $revision_2->test_multiple_properties->value2 = 'shared table - ' . $i . ' - value 2 - en - rev2';
        $revision_2->test_multiple_properties_multiple_values[0]->value1 = 'dedicated table - ' . $i . ' - delta 0 - value 1 - en - rev2';
        $revision_2->test_multiple_properties_multiple_values[0]->value2 = 'dedicated table - ' . $i . ' - delta 0 - value 2 - en - rev2';
        $revision_2->test_multiple_properties_multiple_values[1]->value1 = 'dedicated table - ' . $i . ' - delta 1 - value 1 - en - rev2';
        $revision_2->test_multiple_properties_multiple_values[1]->value2 = 'dedicated table - ' . $i . ' - delta 1 - value 2 - en - rev2';
        $revision_2->save();

        if ($translatable) {
          
        // to test, thus "masking" possible flaws. To avoid that, we simply         // pretend we are starting from an earlier non translated revision.         // This ensures that the we can check that the merging logic is applied         // also when adding a new translation.         $latest_affected_revision_id = 1;
      }
      $previous_revision_id = (int) $entity->getLoadedRevisionId();
      /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_affected_revision */
      $latest_affected_revision = $this->storage->loadRevision($latest_affected_revision_id);
      $translation = $latest_affected_revision->hasTranslation($active_langcode) ?
        $latest_affected_revision->getTranslation($active_langcode) : $latest_affected_revision->addTranslation($active_langcode);
      $entity = $this->storage->createRevision($translation$default_revision);
      $this->assertEquals($default_revision$entity->isDefaultRevision());
      $this->assertEquals($translation->getLoadedRevisionId()$entity->getLoadedRevisionId());
      $this->assertEquals($previous_label$entity->label()$this->formatMessage('Loaded translatable field value does not match the previous one.'));
    }

    // Check that the previous untranslatable field value is loaded in the new     // revision as expected. When we are dealing with a non default translation     // the expected value is always the one stored in the default revision, as     // untranslatable fields can only be changed in the default translation or     // in the default revision, depending on the configured mode.     $value = $entity->get('non_mul_field')->value;
    
'type' => $entity_type,
    ]);

    // Save the entity, this creates the first revision.     $entity->save();
    $revision_ids[] = $entity->getRevisionId();
    $this->assertItemsTableCount(1, $definition);

    // Create the second revision.     $entity->setNewRevision(TRUE);
    $random_timestamp = rand(1e8, 2e8);
    $this->createRevision($entity$user$random_timestamp, 'This is my log message');

    $revision_id = $entity->getRevisionId();
    $revision_ids[] = $revision_id;

    $storage = \Drupal::entityTypeManager()->getStorage('entity_test_mul_revlog');
    $entity = $storage->loadRevision($revision_id);
    $this->assertEquals($random_timestamp$entity->getRevisionCreationTime());
    $this->assertEquals($user->id()$entity->getRevisionUserId());
    $this->assertEquals($user->id()$entity->getRevisionUser()->id());
    $this->assertEquals('This is my log message', $entity->getRevisionLogMessage());

    
Home | Imprint | This part of the site doesn't use cookies.