isNewRevision example

      // entity revision. See https://www.drupal.org/node/2509360.       $return = $entity->isDefaultRevision() ? SAVED_UPDATED : FALSE;
    }

    $this->populateAffectedRevisionTranslations($entity);

    // Populate the "revision_default" flag. Skip this when we are resaving     // the revision, and the flag is set to FALSE, since it is not possible to     // set a previously default revision to non-default. However, setting a     // previously non-default revision to default is allowed for advanced     // use-cases.     if ($this->entityType->isRevisionable() && ($entity->isNewRevision() || $entity->isDefaultRevision())) {
      $revision_default_key = $this->entityType->getRevisionMetadataKey('revision_default');
      $entity->set($revision_default_key$entity->isDefaultRevision());
    }

    $this->doSaveFieldItems($entity);

    return $return;
  }

  /** * Writes entity field values to the storage. * * This method is responsible for allocating entity and revision identifiers * and updating the entity object with their values. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity object. * @param string[] $names * (optional) The name of the fields to be written to the storage. If an * empty value is passed all field values are saved. */
    // creating one, then there is nothing to synchronize.     if (empty($sync_langcode) || $entity->isNew() || count($translations) < 2) {
      return;
    }

    // If the entity language is being changed there is nothing to synchronize.     $entity_unchanged = $this->getOriginalEntity($entity);
    if ($entity->getUntranslated()->language()->getId() != $entity_unchanged->getUntranslated()->language()->getId()) {
      return;
    }

    if ($entity->isNewRevision()) {
      if ($entity->isDefaultTranslationAffectedOnly()) {
        // If changes to untranslatable fields are configured to affect only the         // default translation, we need to skip synchronization in pending         // revisions, otherwise multiple translations would be affected.         if (!$entity->isDefaultRevision()) {
          return;
        }
        // When this mode is enabled, changes to synchronized properties are         // allowed only in the default translation, thus we need to make sure this         // is always used as source for the synchronization process.         else {
          


  /** * {@inheritdoc} */
  public function hasPendingRevision(ContentEntityInterface $entity) {
    $result = FALSE;
    if ($this->isModeratedEntity($entity)) {
      /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
      $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id()$entity->language()->getId());
      $default_revision_id = $entity->isDefaultRevision() && !$entity->isNewRevision() && ($revision_id = $entity->getRevisionId()) ?
        $revision_id : $this->getDefaultRevisionId($entity->getEntityTypeId()$entity->id());
      if ($latest_revision_id !== NULL && $latest_revision_id != $default_revision_id) {
        /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
        $latest_revision = $storage->loadRevision($latest_revision_id);
        $result = !$latest_revision->wasDefaultRevision();
      }
    }
    return $result;
  }

  /** * {@inheritdoc} */
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. */

  public function testNewRevisionRevert() {
    $entity = EntityTestMulRev::create(['name' => 'EntityLoadedRevisionTest']);
    $entity->save();

    // Check that revision ID field is reset while the loaded revision ID is     // preserved when flagging a new revision.     $revision_id = $entity->getRevisionId();
    $entity->setNewRevision();
    $this->assertNull($entity->getRevisionId());
    $this->assertEquals($revision_id$entity->getLoadedRevisionId());
    $this->assertTrue($entity->isNewRevision());

    // Check that after manually restoring the original revision ID, the entity     // is stored without creating a new revision.     $key = $entity->getEntityType()->getKey('revision');
    $entity->set($key$revision_id);
    $entity->save();
    $this->assertEquals($revision_id$entity->getRevisionId());
    $this->assertEquals($revision_id$entity->getLoadedRevisionId());

    // Check that manually restoring the original revision ID causes the "new     // revision" state to be reverted.
$field_item = $this->getMockBuilder('\Drupal\Core\Field\FieldItemBase')
      ->disableOriginalConstructor()
      ->getMockForAbstractClass();

    $this->fieldTypePluginManager->expects($this->any())
      ->method('createFieldItemList')
      ->with($this->entity, 'revision_id', NULL)
      ->willReturn($field_item_list);

    $this->fieldDefinitions['revision_id']->getItemDefinition()->setClass(get_class($field_item));

    $this->assertFalse($this->entity->isNewRevision());
    $this->assertTrue($this->entity->isNewRevision());
    $this->entity->setNewRevision(TRUE);
    $this->assertTrue($this->entity->isNewRevision());
  }

  /** * @covers ::setNewRevision */
  public function testSetNewRevisionException() {
    $this->entityType->expects($this->once())
      ->method('hasKey')
      
$translation->set('name', 'revision_create_test_it');
    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $this->storage->createRevision($translation, FALSE, TRUE);

    // Assert that the alter hook can alter the new revision.     $this->assertEquals('revision_create_test_it_altered', $revision->get('name')->value);

    // Assert the data passed to the hook.     $data = $this->state->get('entity_test.hooks');
    $this->assertEquals('revision_create_test_it', $data['entity_test_mulrev_revision_create']['entity']->get('name')->value);
    $this->assertEquals('revision_create_test_it_altered', $data['entity_test_mulrev_revision_create']['new_revision']->get('name')->value);
    $this->assertFalse($data['entity_test_mulrev_revision_create']['entity']->isNewRevision());
    $this->assertTrue($data['entity_test_mulrev_revision_create']['new_revision']->isNewRevision());
    $this->assertTrue($data['entity_test_mulrev_revision_create']['entity']->isDefaultRevision());
    $this->assertFalse($data['entity_test_mulrev_revision_create']['new_revision']->isDefaultRevision());
    $this->assertTrue($data['entity_test_mulrev_revision_create']['keep_untranslatable_fields']);

    $this->assertEquals('revision_create_test_it', $data['entity_revision_create']['entity']->get('name')->value);
    $this->assertEquals('revision_create_test_it_altered', $data['entity_revision_create']['new_revision']->get('name')->value);
    $this->assertFalse($data['entity_revision_create']['entity']->isNewRevision());
    $this->assertTrue($data['entity_revision_create']['new_revision']->isNewRevision());
    $this->assertTrue($data['entity_revision_create']['entity']->isDefaultRevision());
    $this->assertFalse($data['entity_revision_create']['new_revision']->isDefaultRevision());
    

  public function getInstances() {
    return \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['plugin' => 'block_content:' . $this->uuid()]);
  }

  /** * {@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 block_content without adding a new       // revision and the user did not supply a revision log, keep the existing       // one.       $record->revision_log = $this->original->getRevisionLogMessage();
    }
  }

  /** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    

      }
    }
  }

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

    $is_new_revision = $this->isNewRevision();
    if (!$is_new_revision && isset($this->original) && empty($record->revision_log_message)) {
      // If we are updating an existing media item without adding a       // new revision, we need to make sure $entity->revision_log_message is       // reset whenever it is empty.       // Therefore, this code allows us to avoid clobbering an existing log       // entry with an empty one.       $record->revision_log_message = $this->original->revision_log_message->value;
    }

    if ($is_new_revision) {
      $record->revision_created = self::getRequestTime();
    }
$this->database
              ->update($this->revisionTable)
              ->fields((array) $record)
              ->condition($this->revisionKey, $entity->getRevisionId())
              ->execute();
          }
        }
        if ($default_revision && $this->dataTable) {
          $this->saveToSharedTables($entity);
        }
        if ($this->revisionDataTable) {
          $new_revision = $full_save && $entity->isNewRevision();
          $this->saveToSharedTables($entity$this->revisionDataTable, $new_revision);
        }
      }
      else {
        // @todo Remove the 'return' option in Drupal 11.         // @see https://www.drupal.org/project/drupal/issues/3256524         $insert_id = $this->database
          ->insert($this->baseTable, ['return' => Database::RETURN_INSERT_ID])
          ->fields((array) $record)
          ->execute();
        // Even if this is a new entity the ID key might have been set, in which

  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);
    $entity = $this->entity;

    // @todo: Is there a better way to check if an entity type is revisionable?     if ($entity->getEntityType()->hasKey('revision') && !$entity->isNew()) {
      $form['revision'] = [
        '#type' => 'checkbox',
        '#title' => t('Create new revision'),
        '#default_value' => $entity->isNewRevision(),
      ];
    }

    return $form;
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    try {
      

  protected function loadContentModerationStateRevision(ContentEntityInterface $entity) {
    $moderation_info = \Drupal::service('content_moderation.moderation_information');
    $content_moderation_storage = \Drupal::entityTypeManager()->getStorage('content_moderation_state');

    $revisions = $content_moderation_storage->getQuery()
      ->accessCheck(FALSE)
      ->condition('content_entity_type_id', $entity->getEntityTypeId())
      ->condition('content_entity_id', $entity->id())
      // Ensure the correct revision is loaded in scenarios where a revision is       // being reverted.       ->condition('content_entity_revision_id', $entity->isNewRevision() ? $entity->getLoadedRevisionId() : $entity->getRevisionId())
      ->condition('workflow', $moderation_info->getWorkflowForEntity($entity)->id())
      ->condition('langcode', $entity->language()->getId())
      ->allRevisions()
      ->sort('revision_id', 'DESC')
      ->execute();
    if (empty($revisions)) {
      return NULL;
    }

    /** @var \Drupal\content_moderation\Entity\ContentModerationStateInterface $content_moderation_state */
    $content_moderation_state = $content_moderation_storage->loadRevision(key($revisions));
    

    $entity->save();
    $entity->addTranslation('de');
    $entity->save();

    // Reload the entity as ContentEntityBase::postCreate() forces the entity to     // be a new revision.     $entity = EntityTestMulRev::load($entity->id());
    $entity_translation = $entity->getTranslation('de');

    // The entity is not set to be a new revision.     $this->assertFalse($entity_translation->isNewRevision());

    // The clone should not be set to be a new revision either.     $clone = clone $entity_translation;
    $this->assertFalse($clone->isNewRevision());

    // After forcing the clone to be a new revision only it should be flagged     // as a new revision, but the original entity should not.     $clone->setNewRevision();
    $this->assertTrue($clone->isNewRevision());
    $this->assertFalse($entity_translation->isNewRevision());
  }

  
    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       // need to make sure $entity->revision_log is reset whenever it is empty.       // Therefore, this code allows us to avoid clobbering an existing log       // entry with an empty one.       $record->revision_log = $this->original->revision_log->value;
    }
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.