setRevisionUserId example

$time = $this->time->getRequestTime();
    if ($revision instanceof EntityChangedInterface) {
      $revision->setChangedTime($time);
    }

    if ($revision instanceof RevisionLogInterface) {
      $originalRevisionTimestamp = $revision->getRevisionCreationTime();
      $date = $this->dateFormatter->format($originalRevisionTimestamp);
      $revision
        ->setRevisionLogMessage($this->t('Copy of the revision from %date.', ['%date' => $date]))
        ->setRevisionCreationTime($time)
        ->setRevisionUserId($this->currentUser()->id());
    }

    return $revision;
  }

  /** * Returns the bundle label of an entity. * * @param \Drupal\Core\Entity\RevisionableInterface $entity * The entity. * * @return string|null * The bundle label. */
$translation = $this->getTranslation($langcode);

      // If no owner has been set explicitly, make the anonymous user the owner.       if (!$translation->getOwner()) {
        $translation->setOwnerId(0);
      }
    }

    // If no revision author has been set explicitly, make the node owner the     // revision author.     if (!$this->getRevisionUser()) {
      $this->setRevisionUserId($this->getOwnerId());
    }
  }

  /** * {@inheritdoc} */
  public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
    parent::preSaveRevision($storage$record);

    if (!$this->isNewRevision() && isset($this->original) && (!isset($record->revision_log) || $record->revision_log === '')) {
      // If we are updating an existing node without adding a new revision, we
$block_content_type = BlockContentType::create([
      'id' => 'basic',
      'label' => 'basic block',
    ]);
    $block_content_type->save();

    $block_content = BlockContent::create([
      'info' => 'Test block content',
      'type' => 'basic',
    ]);
    $block_content->setRevisionUserId($primary_author->id());
    $block_content->save();

    $view = Views::getView('test_block_content_revision_user');
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'id' => 1,
        'revision_id' => 1,
        'revision_user' => $primary_author->id(),
      ],
    ]static::$columnMap);

    
static::validate($entity$field_names);

    // Set revision data details for revisionable entities.     if ($entity->getEntityType()->isRevisionable()) {
      if ($bundle_entity_type = $entity->getEntityType()->getBundleEntityType()) {
        $bundle_entity = $this->entityTypeManager->getStorage($bundle_entity_type)->load($entity->bundle());
        if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
          $entity->setNewRevision($bundle_entity->shouldCreateNewRevision());
        }
      }
      if ($entity instanceof RevisionLogInterface && $entity->isNewRevision()) {
        $entity->setRevisionUserId($this->user->id());
        $entity->setRevisionCreationTime($this->time->getRequestTime());
      }
    }

    $entity->save();
    $primary_data = new ResourceObjectData([ResourceObject::createFromEntity($resource_type$entity)], 1);
    return $this->buildWrappedResponse($primary_data$request$this->getIncludes($request$primary_data));
  }

  /** * Deletes an individual entity. * * @param \Drupal\Core\Entity\EntityInterface $entity * The loaded entity. * * @return \Drupal\jsonapi\ResourceResponse * The response. */

  protected function createRevision(EntityInterface $entity, UserInterface $user$timestamp$log_message) {
    $entity->setNewRevision(TRUE);
    $entity->setRevisionCreationTime($timestamp);
    $entity->setRevisionUserId($user->id());
    $entity->setRevisionLogMessage($log_message);
    $entity->save();
  }

}
'name' => 'revert',
    ]);

    $date = new \DateTime('11 January 2009 4:00:00pm');
    $entity->setRevisionCreationTime($date->getTimestamp());
    $entity->isDefaultRevision(TRUE);
    $entity->setNewRevision();
    $entity->save();

    $revisionCreationTime = $date->modify('+1 hour')->getTimestamp();
    $entity->setRevisionCreationTime($revisionCreationTime);
    $entity->setRevisionUserId(0);
    $entity->isDefaultRevision(FALSE);
    $entity->setNewRevision();
    $entity->save();
    $targetRevertRevisionId = $entity->getRevisionId();

    // Create a another revision so the previous revision can be reverted to.     $entity->setRevisionCreationTime($date->modify('+1 hour')->getTimestamp());
    $entity->isDefaultRevision(FALSE);
    $entity->setNewRevision();
    $entity->save();

    

  protected function createNodeRevision(NodeInterface $node) {
    // Create revision with a random title and body and update variables.     $node->title = $this->randomMachineName();
    $node->body = [
      'value' => $this->randomMachineName(32),
      'format' => filter_default_format(),
    ];
    $node->setNewRevision();
    // Ensure the revision author is a different user.     $node->setRevisionUserId($this->revisionUser->id());
    $node->save();

    return $node;
  }

  /** * Checks node revision operations. */
  public function testRevisions() {
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
    $nodes = $this->nodes;
    
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // The revision timestamp will be updated when the revision is saved. Keep     // the original one for the confirmation message.     $original_revision_timestamp = $this->revision->getRevisionCreationTime();

    $this->revision = $this->prepareRevertedRevision($this->revision, $form_state);
    $this->revision->revision_log = $this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
    $this->revision->setRevisionUserId($this->currentUser()->id());
    $this->revision->setRevisionCreationTime($this->time->getRequestTime());
    $this->revision->setChangedTime($this->time->getRequestTime());
    $this->revision->save();

    $this->logger('content')->info('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
    $this->messenger()
      ->addStatus($this->t('@type %title has been reverted to the revision from %revision-date.', [
        '@type' => node_get_type_label($this->revision),
        '%title' => $this->revision->label(),
        '%revision-date' => $this->dateFormatter->format($original_revision_timestamp),
      ]));
    
/** @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->messenger()->addStatus($this->t('The moderation state has been updated.'));

    $new_state = $this->moderationInfo->getWorkflowForEntity($entity)->getTypePlugin()->getState($new_state);
    // The page we're on likely won't be visible if we just set the entity to     // the default state, as we hide that latest-revision tab if there is no     // pending revision. Redirect to the canonical URL instead, since that will     // still exist.     if ($new_state->isDefaultRevisionState()) {
      

    $node1->setOwner($author);
    $node1->save();
    $expected_result[] = ['nid' => $node1->id()];

    // Create one node of which an additional revision author will be the     // author.     $node2 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node2->setRevisionUserId($no_author->id());
    $node2->save();
    $expected_result[] = ['nid' => $node2->id()];

    // Force to add a new revision.     $node2->setNewRevision(TRUE);
    $node2->setRevisionUserId($author->id());
    $node2->save();

    // Create one node on which the author has neither authorship of revisions     // or the main node.     $node3 = Node::create([
      
    $media = Media::create([
      'bundle' => 'camelids',
      'field_media_file' => [
        'target_id' => $file->id(),
      ],
    ]);
    $media
      ->setName('Llama')
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setOwnerId($this->account->id())
      ->setRevisionUserId($this->account->id())
      ->save();

    return $media;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $file = File::load(1);
    $thumbnail = File::load(3);
    

  public function testRevisionUser() {
    $primary_author = $this->createUser();
    $secondary_author = $this->createUser();

    $media = Media::create([
      'name' => 'Test media',
      'bundle' => $this->testMediaType->id(),
      'uid' => $primary_author->id(),
    ]);
    $media->setRevisionUserId($primary_author->id());
    $media->save();

    $view = Views::getView('test_media_revision_uid');
    $this->executeView($view);
    $this->assertIdenticalResultset($view[
      [
        'mid' => 1,
        'vid' => 1,
        'uid' => $primary_author->id(),
        'revision_user' => $primary_author->id(),
      ],
    ],
    $media = Media::create([
      'bundle' => 'camelids',
      'field_media_file' => [
        'target_id' => $file->id(),
      ],
    ]);
    $media
      ->setName('Llama')
      ->setPublished()
      ->setCreatedTime(123456789)
      ->setOwnerId(static::$auth ? $this->account->id() : 0)
      ->setRevisionUserId(static::$auth ? $this->account->id() : 0)
      ->save();

    return $media;
  }

  /** * {@inheritdoc} */
  protected function getExpectedNormalizedEntity() {
    $file = File::load(1);
    $thumbnail = File::load(2);
    
      $node->title = $this->randomMachineName();
      $node->body = [
        'value' => $this->randomMachineName(32),
        'format' => filter_default_format(),
      ];
      $node->untranslatable_string_field->value = $this->randomString();
      $node->setNewRevision();

      // Edit the 1st and 2nd revision with a different user.       if ($i < 2) {
        $editor = $this->drupalCreateUser();
        $node->setRevisionUserId($editor->id());
      }
      else {
        $node->setRevisionUserId($web_user->id());
      }

      $node->save();

      // Make sure we get revision information.       $node = Node::load($node->id());
      $nodes[] = clone $node;
    }

    
'status' => 1,
    ]);
    $user2->save();

    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container->get('entity_type.manager')
      ->getStorage('entity_test_mulrev_changed_rev');
    /** @var \Drupal\entity_test\Entity\EntityTestMulRevChangedWithRevisionLog $entity */
    $entity = $storage->create([
      'name' => $this->randomString(),
    ]);
    $entity->setRevisionUserId($user1->id());
    $entity->save();

    $this->assertFalse($entity->hasTranslationChanges(), 'ContentEntityBase::hasTranslationChanges() found no changes after the entity has been saved.');

    // Update the revision metadata fields and the changed field, which should     // be skipped from checking for changes in     // ContentEntityBase::hasTranslationChanges().     $entity_previous_rev_id = $entity->getRevisionId();
    // Revision metadata field revision_timestamp.     $entity->setRevisionCreationTime(time() + 1);
    // Revision metadata field revision_uid.
Home | Imprint | This part of the site doesn't use cookies.