getOriginalId example

$form['actions']['submit']['#value'] = $this->t('Create new set');

    return $this->protectBundleIdElement($form);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;
    $is_new = !$entity->getOriginalId();
    $entity->save();

    if ($is_new) {
      $this->messenger()->addStatus($this->t('The %set_name shortcut set has been created. You can edit it from this page.', ['%set_name' => $entity->label()]));
    }
    else {
      $this->messenger()->addStatus($this->t('Updated set name to %set-name.', ['%set-name' => $entity->label()]));
    }
    $form_state->setRedirectUrl($this->entity->toUrl('customize-form'));
  }

}
$this->configFactory->get('the_provider.the_config_prefix.foo')
      ->willReturn($immutable_config_object->reveal());
    $this->configFactory->loadMultiple(['the_provider.the_config_prefix.foo'])
      ->willReturn([]);
    $this->configFactory->rename('the_provider.the_config_prefix.foo', 'the_provider.the_config_prefix.bar')
      ->shouldBeCalled();
    $this->configFactory->getEditable('the_provider.the_config_prefix.bar')
      ->willReturn($config_object->reveal());

    // Performing a rename does not change the original ID until saving.     $this->assertSame('foo', $entity->getOriginalId());
    $entity->set('id', 'bar');
    $this->assertSame('foo', $entity->getOriginalId());

    $this->entityQuery->condition('uuid', 'bar')->willReturn($this->entityQuery);
    $this->entityQuery->execute()->willReturn([$entity->id()]);

    $return = $this->entityStorage->save($entity);
    $this->assertSame(SAVED_UPDATED, $return);
    $this->assertSame('bar', $entity->getOriginalId());
  }

  

  public function testSaveConfigEntity() {
    $this->setUpKeyValueEntityStorage();

    $entity = $this->getMockEntity('Drupal\Core\Config\Entity\ConfigEntityBase', [['id' => 'foo']][
      'toArray',
      'preSave',
    ]);
    $entity->enforceIsNew();
    // When creating a new entity, the ID is tracked as the original ID.     $this->assertSame('foo', $entity->getOriginalId());

    $expected = ['id' => 'foo'];
    $entity->expects($this->atLeastOnce())
      ->method('toArray')
      ->willReturn($expected);

    $this->keyValueStore->expects($this->exactly(2))
      ->method('has')
      ->with('foo')
      ->willReturn(FALSE);
    $this->keyValueStore->expects($this->once())
      
public function testCRUD() {
    $default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
    // Verify default properties on a newly created empty entity.     $storage = \Drupal::entityTypeManager()->getStorage('config_test');
    $empty = $storage->create();
    $this->assertNull($empty->label);
    $this->assertNull($empty->style);
    $this->assertSame($default_langcode$empty->language()->getId());

    // Verify ConfigEntity properties/methods on the newly created empty entity.     $this->assertTrue($empty->isNew());
    $this->assertNull($empty->getOriginalId());
    $this->assertSame('config_test', $empty->bundle());
    $this->assertNull($empty->id());
    $this->assertTrue(Uuid::isValid($empty->uuid()));
    $this->assertNull($empty->label());

    $this->assertNull($empty->get('id'));
    $this->assertTrue(Uuid::isValid($empty->get('uuid')));
    $this->assertNull($empty->get('label'));
    $this->assertNull($empty->get('style'));
    $this->assertSame($default_langcode$empty->language()->getId());

    
/** * {@inheritdoc} */
  public function status() {
    return $this->storage->status();
  }

  /** * {@inheritdoc} */
  public function getOriginalId() {
    return $this->storage->getOriginalId();
  }

  /** * {@inheritdoc} */
  public function setOriginalId($id) {
    return $this->storage->setOriginalId($id);
  }

  /** * {@inheritdoc} */
// Ensure the updated values are stored in the entity.     $this->assertSame($expected_plugin_config$entity->get('the_plugin_collection_config'));
  }

  /** * @covers ::setOriginalId * @covers ::getOriginalId */
  public function testGetOriginalId() {
    $new_id = $this->randomMachineName();
    $this->entity->set('id', $new_id);
    $this->assertSame($this->id, $this->entity->getOriginalId());
    $this->assertSame($this->entity, $this->entity->setOriginalId($new_id));
    $this->assertSame($new_id$this->entity->getOriginalId());

    // Check that setOriginalId() does not change the entity "isNew" status.     $this->assertFalse($this->entity->isNew());
    $this->entity->setOriginalId($this->randomMachineName());
    $this->assertFalse($this->entity->isNew());
    $this->entity->enforceIsNew();
    $this->assertTrue($this->entity->isNew());
    $this->entity->setOriginalId($this->randomMachineName());
    $this->assertTrue($this->entity->isNew());
  }

  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // Only handle renames, not creations.     if (!$this->isNew() && $this->getOriginalId() !== $this->id()) {
      $bundle_type = $this->getEntityType();
      $bundle_of = $bundle_type->getBundleOf();
      if (!empty($bundle_of)) {
        throw new ConfigNameException("The machine name of the '{$bundle_type->getLabel()}' bundle cannot be changed.");
      }
    }
  }

  /** * Returns view or form displays for this bundle. * * @param string $entity_type_id * The entity type ID of the display type to load. * * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[] * A list of matching displays. */
      foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) {
        $this->set($plugin_config_key$plugin_collection->getConfiguration());
      }
    }

    // Ensure this entity's UUID does not exist with a different ID, regardless     // of whether it's new or updated.     $matching_entities = $storage->getQuery()
      ->condition('uuid', $this->uuid())
      ->execute();
    $matched_entity = reset($matching_entities);
    if (!empty($matched_entity) && ($matched_entity != $this->id()) && $matched_entity != $this->getOriginalId()) {
      throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this UUID is already used for '$matched_entity'");
    }

    // If this entity is not new, load the original entity for comparison.     if (!$this->isNew()) {
      $original = $storage->loadUnchanged($this->getOriginalId());
      // Ensure that the UUID cannot be changed for an existing entity.       if ($original && ($original->uuid() != $this->uuid())) {
        throw new ConfigDuplicateUUIDException("Attempt to save a configuration entity '{$this->id()}' with UUID '{$this->uuid()}' when this entity already exists with UUID '{$original->uuid()}'");
      }
    }
    
if ($this->isNew()) {
      return TRUE;
    }

    // $this->original only exists during save. See     // \Drupal\Core\Entity\EntityStorageBase::save(). If it exists we re-use it     // here for performance reasons.     /** @var \Drupal\Core\Entity\ContentEntityBase $original */
    $original = $this->original ? $this->original : NULL;

    if (!$original) {
      $id = $this->getOriginalId() !== NULL ? $this->getOriginalId() : $this->id();
      $original = $this->entityTypeManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id);
    }

    // If the current translation has just been added, we have a change.     $translated = count($this->translations) > 1;
    if ($translated && !$original->hasTranslation($this->activeLangcode)) {
      return TRUE;
    }

    // Compare field item current values with the original ones to determine     // whether we have changes. If a field is not translatable and the entity is

  protected function doPreSave(EntityInterface $entity) {
    $id = $entity->id();

    // Track the original ID.     if ($entity->getOriginalId() !== NULL) {
      $id = $entity->getOriginalId();
    }

    // Track if this entity exists already.     $id_exists = $this->has($id$entity);

    // A new entity should not already exist.     if ($id_exists && $entity->isNew()) {
      throw new EntityStorageException("'{$this->entityTypeId}' entity with ID '$id' already exists.");
    }

    
$storage->clearReplacementId($style->id());
    }
  }

  /** * Update field settings if the image style name is changed. * * @param \Drupal\image\ImageStyleInterface $style * The image style. */
  protected static function replaceImageStyle(ImageStyleInterface $style) {
    if ($style->id() != $style->getOriginalId()) {
      // Loop through all entity displays looking for formatters / widgets using       // the image style.       foreach (EntityViewDisplay::loadMultiple() as $display) {
        foreach ($display->getComponents() as $name => $options) {
          if (isset($options['type']) && $options['type'] == 'image' && $options['settings']['image_style'] == $style->getOriginalId()) {
            $options['settings']['image_style'] = $style->id();
            $display->setComponent($name$options)
              ->save();
          }
        }
      }
      

  public function getDescription() {
    return $this->description;
  }

  /** * {@inheritdoc} */
  public function postSave(EntityStorageInterface $storage$update = TRUE) {
    parent::postSave($storage$update);

    if ($update && $this->getOriginalId() != $this->id()) {
      $update_count = $storage->updateType($this->getOriginalId()$this->id());
      if ($update_count) {
        \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($update_count,
          'Changed the content type of 1 post from %old-type to %type.',
          'Changed the content type of @count posts from %old-type to %type.',
          [
            '%old-type' => $this->getOriginalId(),
            '%type' => $this->id(),
          ]));
      }
    }
    
Home | Imprint | This part of the site doesn't use cookies.