Problem/Motivation

To reproduce:

a) Create a help topic.

b) Edit the help topic, changing the ID of the topic.

You will get an error similar to this:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Config\ConfigDuplicateUUIDException: Attempt to save a configuration entity 'config_error' with UUID 'a23e10dc-f081-4fb5-82c5-5ebe88956d51' when this UUID is already used for 'defining_error_responses_including_403_404_pages' in Drupal\Core\Config\Entity\ConfigEntityBase->preSave() (line 331 of core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php).

Drupal\config_help\Entity\HelpTopic->preSave(Object) (Line: 434)
Drupal\Core\Entity\EntityStorageBase->doPreSave(Object) (Line: 389)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 259)
...

Proposed resolution

I think this used to work... but it isn't working now. So we should either:
a) Not let people edit the machine name after the entity is first saved
b) Detect that it is changing and make it work somehow

Remaining tasks

Figure out if this is possible, and make it work.

Make a patch with a test.

User interface changes

Depends on the solution decided on, see Proposed Resolution for now.

API changes

Probably not?

Data model changes

No

Comments

jhodgdon created an issue. See original summary.

jhodgdon’s picture

Issue summary: View changes
Issue tags: +Needs tests

Update summary and add tag.

jhodgdon’s picture

The error is happening in ConfigEntityBase::preSave() from here:

 // 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'");
  }

So, it looks like if we set $this->originalId during the editing process, it will work fine.

Hm... it looks like originalId gets set during the constructor in ConfigEntityBase (and not overridden in our entity), so I don't know why this is not working. Except I did notice that setOriginalId(NULL) is called in public function createDuplicate(), so maybe that is the problem. Hmm..

OK, let's see what the editing process does. That is in HelpTopicForm... don't see anything there, let's check the base EntityForm class. It looks like what it does when setting up the entity is a clone of the entity object, followed by some value copying. That should be OK.

Well, I'll do some more debugging...

  • jhodgdon committed aa6314e on 8.x-1.x
    Issue #2744301 by jhodgdon: Add test to make sure you can change ID when...
jhodgdon’s picture

Priority: Major » Normal
Status: Active » Closed (cannot reproduce)

Actually, it looks like this bug has gone away again. I've added a test to make sure it stays gone away.

gnuget’s picture

Glad that this error is not happening anymore.

Will check the other issue.