Problem/Motivation
When using
Group →
module together with Content Moderation and when adding moderated content (e.g. a node) to a group before Content Moderation's hook_entity_insert()
has run, the following exception is thrown:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-1-1-editorial-en' for key 'content_moderation_state__lookup'
This is because Group module (re-)saves the entity when it is being added to the group (see
🐛
Node update hook is triggered upon group content create
Needs work
for some background/discussion on that). That "inner" save then causes the content moderation state entity to be created, so that it already exists when content_moderation_entity_insert()
is triggered for the original save. Content Moderation then tries to create a new content moderation state entity which causes the problem.
Content Moderation already tries to be smart and load any existing content moderation state for the given entity, but that fails because the loaded revision ID is not set at this point, so it looks for a database record with the content_entity_revision_id
set to NULL
but there is a record with content_entity_revision_id
set to (e.g.) 1
. Since it cannot find what it is looking for it creates a new entity to INSERT
(instead of UPDATE
) which then triggers the exception.
See \Drupal\content_moderation\Entity\ContentModerationState::loadFromModeratedEntity()
.
Steps to reproduce
- Add contrib
Group →
module to codebase:
composer require drupal/group
- Apply patch from
#2872697-41: Node update hook is triggered upon group content create →
to Group module:
curl https://www.drupal.org/files/issues/2020-11-06/2872697-41.patch | git apply
- Add a custom module that:
- Has a lower weight than Content Moderation (which has 0)
- Does
$group->addContent()
in hook_node_insert()
- Install Content Moderation and Group:
drush en content_moderation group
- Enable Content Moderation for a given node type
- Add a node of that type
Expected result: Node is created
Actual result: SQL Exception (see above)
Proposed resolution
When attempting to load the existing content moderation state entity when there is no loaded revision ID, but there is a revision ID, use the revision ID instead.
The ("not loaded") revision ID is what will be set on the content moderation state entity anyway, so it is what causes the duplicate key exception, so it seems appropriate to check for that.
Remaining tasks
Review the patch.
User interface changes
None.
API changes
None.
Data model changes
None.
Release notes snippet
-