Issue by Sam152, mpotter, gnuget, kriboogh, joelpittet: Unable to set a moderation_state value for unsaved entities that are assigned an ID

8.6.x
Nathaniel Catchpole 2018-06-26 16:07:19 +01:00
parent 679fe1f111
commit 191bf52849
2 changed files with 34 additions and 0 deletions
core/modules/content_moderation

View File

@ -175,6 +175,10 @@ class ModerationInformation implements ModerationInformationInterface {
public function isDefaultRevisionPublished(ContentEntityInterface $entity) {
$workflow = $this->getWorkflowForEntity($entity);
$default_revision = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($entity->id());
// If no default revision could be loaded, the entity has not yet been
// saved. In this case the moderation_state of the unsaved entity can be
// used, since once saved it will become the default.
$default_revision = $default_revision ?: $entity;
// Ensure we are checking all translations of the default revision.
if ($default_revision instanceof TranslatableInterface && $default_revision->isTranslatable()) {

View File

@ -251,4 +251,34 @@ class ModerationStateFieldItemListTest extends KernelTestBase {
];
}
/**
* Test saving a moderated node with an existing ID.
*
* @dataProvider moderatedEntityWithExistingIdTestCases
*/
public function testModeratedEntityWithExistingId($state) {
$node = Node::create([
'title' => 'Test title',
'type' => 'example',
'nid' => 999,
'moderation_state' => $state,
]);
$node->save();
$this->assertEquals($state, $node->moderation_state->value);
}
/**
* Test cases for ::testModeratedEntityWithExistingId.
*/
public function moderatedEntityWithExistingIdTestCases() {
return [
'Draft non-default state' => [
'draft',
],
'Published default state' => [
'published',
],
];
}
}