diff --git a/core/modules/content_moderation/content_moderation.module b/core/modules/content_moderation/content_moderation.module index aca5079..84f32b0 100644 --- a/core/modules/content_moderation/content_moderation.module +++ b/core/modules/content_moderation/content_moderation.module @@ -251,3 +251,12 @@ function content_moderation_workflow_insert(WorkflowInterface $entity) { function content_moderation_workflow_update(WorkflowInterface $entity) { content_moderation_workflow_insert($entity); } + +/** + * Implements hook_preprocess(). + */ +function content_moderation_preprocess(&$variables, $hook) { + \Drupal::service('class_resolver') + ->getInstanceFromDefinition(EntityTypeInfo::class) + ->preprocess($variables, $hook); +} diff --git a/core/modules/content_moderation/src/EntityTypeInfo.php b/core/modules/content_moderation/src/EntityTypeInfo.php index 45f796c..d673e13 100644 --- a/core/modules/content_moderation/src/EntityTypeInfo.php +++ b/core/modules/content_moderation/src/EntityTypeInfo.php @@ -364,23 +364,17 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id $this->entityTypeManager->getHandler($type->getBundleOf(), 'moderation')->enforceRevisionsBundleFormAlter($form, $form_state, $form_id); } } - elseif ($form_object instanceof ContentEntityFormInterface) { + elseif ($form_object instanceof ContentEntityFormInterface && $form_object->getOperation() == 'edit') { $entity = $form_object->getEntity(); if ($this->moderationInfo->isModeratedEntity($entity)) { $this->entityTypeManager ->getHandler($entity->getEntityTypeId(), 'moderation') ->enforceRevisionsEntityFormAlter($form, $form_state, $form_id); - if (empty($this->validator->getValidTransitions($entity, $this->currentUser))) { - $latest_revision = $this->getTranslationAffectedRevision($this->moderationInfo->getLatestRevision($entity->getEntityTypeId(), $entity->id())); - drupal_set_message(\Drupal::translation()->translate('This @entity_type_label cannot be saved, publish or delete the latest revision first.', ['@entity_type_label' => $entity->getEntityType()->getLabel(), '@latest_revision_edit_url' => $latest_revision->toUrl('edit-form', ['language' => $latest_revision->language()])->toString(), '@latest_revision_delete_url' => $latest_revision->toUrl('delete-form', ['language' => $latest_revision->language()])->toString()]), 'error'); - } - $invalid_transitions = array_diff_key($this->validator->getPermittedTransitions($entity, $this->currentUser), $this->validator->getValidTransitions($entity, $this->currentUser)); if ($invalid_transitions) { $labels = array_map(function ($transition) { return $transition->label(); }, $invalid_transitions); - $last = array_pop($labels); - $invalid_transitions_string = implode(', ', $labels) . ', or ' . $last; + $invalid_transition_labels = implode(', ', $labels); $latest_revision = $this->getTranslationAffectedRevision($this->moderationInfo->getLatestRevision($entity->getEntityTypeId(), $entity->id())); $form['invalid_transitions'] = [ 'rule' => [ @@ -390,7 +384,7 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id 'label' => [ '#type' => 'item', '#prefix' => '', - '#markup' => \Drupal::translation()->translate('It is not possible to @invalid this @entity_type_label.', ['@invalid' => $invalid_transitions_string, '@entity_type_label' => $entity->getEntityType()->getLabel()]), + '#markup' => \Drupal::translation()->translate('It is not possible to %invalid_transition_labels this @entity_type_label.', ['%invalid_transition_labels' => $invalid_transition_labels, '@entity_type_label' => $entity->getEntityType()->getLabel()]), '#suffix' => '', ], 'message' => [ @@ -398,6 +392,7 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id '#markup' => \Drupal::translation()->translate('Publish or delete the latest revision to allow all workflow transitions.', ['@latest_revision_edit_url' => $latest_revision->toUrl('edit-form', ['language' => $latest_revision->language()])->toString(), '@latest_revision_delete_url' => $latest_revision->toUrl('delete-form', ['language' => $latest_revision->language()])->toString()]), ], '#weight' => 999, + '#no_valid_transitions' => empty($this->validator->getValidTransitions($entity, $this->currentUser)), ]; } @@ -408,6 +403,18 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id } /** + * @param array $variables + * @param string $hook + */ + public function preprocess(array &$variables, $hook) { + if (strpos($hook, '_edit_form') && $variables['form']['invalid_transitions']['#no_valid_transitions']) { + $invalid_transitions = $variables['form']['invalid_transitions']; + unset($variables['form']); + $variables['form'] = $invalid_transitions; + } + } + + /** * @param \Drupal\Core\Entity\EntityInterface $entity * @return \Drupal\Core\Entity\EntityInterface */ diff --git a/core/modules/content_moderation/src/Tests/ModerationFormTest.php b/core/modules/content_moderation/src/Tests/ModerationFormTest.php index 47287a4..74a5a5b 100644 --- a/core/modules/content_moderation/src/Tests/ModerationFormTest.php +++ b/core/modules/content_moderation/src/Tests/ModerationFormTest.php @@ -172,7 +172,7 @@ public function testContentTranslationNodeForm() { $this->assertTrue($this->xpath('//input[@value="Save and Create New Draft (this translation)"]')); $this->assertFalse($this->xpath('//input[@value="Save and Publish (this translation)"]')); $this->assertFalse($this->xpath('//input[@value="Save and Archive (this translation)"]')); - $this->assertText('You cannot Publish this Content because there are other translations with unsaved draft revisions.'); + $this->assertText('It is not possible to Publish this Content.'); $this->drupalPostForm(NULL, [ 'body[0][value]' => 'Forth version of the content.', ], t('Save and Create New Draft (this translation)')); @@ -215,8 +215,7 @@ public function testContentTranslationNodeForm() { $this->assertFalse($this->xpath('//input[@value="Save and Create New Draft (this translation)"]')); $this->assertFalse($this->xpath('//input[@value="Save and Publish (this translation)"]')); $this->assertFalse($this->xpath('//input[@value="Save and Archive (this translation)"]')); - $this->assertText('You cannot Create New Draft, Publish, Archive this Content because there are other translations with unsaved draft revisions.'); - $this->assertText('Due to draft revisions on other translations this entity cannot be saved'); + $this->assertText('It is not possible to Create New Draft, Publish, Archive this Content.'); // We should be able to publish the english forward revision. $this->drupalGet($edit_path);