Commit 3c1ce586 authored by catch's avatar catch Committed by alexpott

Issue #2784341 by alexpott: Remove form functions from ModerationInterface

(cherry picked from commit 0d00a94b139602d9280360dc2977dbc989c6ef1e)
(cherry picked from commit 04abf4c0)
parent 42b81c64
......@@ -133,7 +133,7 @@ function content_moderation_local_tasks_alter(&$local_tasks) {
* Implements hook_form_alter().
*/
function content_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_content_moderation_create_entity_type_info()->bundleFormAlter($form, $form_state, $form_id);
_content_moderation_create_entity_type_info()->formAlter($form, $form_state, $form_id);
}
/**
......
......@@ -4,6 +4,8 @@
use Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
......@@ -328,21 +330,23 @@ public function entityBundleFieldInfoAlter(&$fields, EntityTypeInterface $entity
*
* @see hook_form_alter()
*/
public function bundleFormAlter(array &$form, FormStateInterface $form_state, $form_id) {
if ($this->moderationInfo->isRevisionableBundleForm($form_state->getFormObject())) {
/* @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $bundle */
$bundle = $form_state->getFormObject()->getEntity();
$this->entityTypeManager->getHandler($bundle->getEntityType()->getBundleOf(), 'moderation')->enforceRevisionsBundleFormAlter($form, $form_state, $form_id);
public function formAlter(array &$form, FormStateInterface $form_state, $form_id) {
$form_object = $form_state->getFormObject();
if ($form_object instanceof BundleEntityFormBase) {
$type = $form_object->getEntity()->getEntityType();
if ($this->moderationInfo->canModerateEntitiesOfEntityType($type)) {
$this->entityTypeManager->getHandler($type->getBundleOf(), 'moderation')->enforceRevisionsBundleFormAlter($form, $form_state, $form_id);
}
}
elseif ($this->moderationInfo->isModeratedEntityForm($form_state->getFormObject())) {
/* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
$this->entityTypeManager->getHandler($entity->getEntityTypeId(), 'moderation')->enforceRevisionsEntityFormAlter($form, $form_state, $form_id);
// Submit handler to redirect to the latest version, if available.
$form['actions']['submit']['#submit'][] = [EntityTypeInfo::class, 'bundleFormRedirect'];
elseif ($form_object instanceof ContentEntityFormInterface) {
$entity = $form_object->getEntity();
if ($this->moderationInfo->isModeratedEntity($entity)) {
$this->entityTypeManager
->getHandler($entity->getEntityTypeId(), 'moderation')
->enforceRevisionsEntityFormAlter($form, $form_state, $form_id);
// Submit handler to redirect to the latest version, if available.
$form['actions']['submit']['#submit'][] = [EntityTypeInfo::class, 'bundleFormRedirect'];
}
}
}
......
......@@ -2,13 +2,10 @@
namespace Drupal\content_moderation;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormInterface;
/**
* General service for moderation-related questions about Entity API.
......@@ -71,27 +68,6 @@ public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type,
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isModeratedEntityForm(FormInterface $form_object) {
return $form_object instanceof ContentEntityFormInterface
&& $this->isModeratedEntity($form_object->getEntity());
}
/**
* {@inheritdoc}
*/
public function isRevisionableBundleForm(FormInterface $form_object) {
if ($form_object instanceof BundleEntityFormBase) {
$bundle_of = $form_object->getEntity()->getEntityType()->getBundleOf();
$type = $this->entityTypeManager->getDefinition($bundle_of);
return $type->isRevisionable();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
......
......@@ -5,7 +5,6 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormInterface;
/**
* Interface for moderation_information service.
......@@ -61,33 +60,6 @@ public function canModerateEntitiesOfEntityType(EntityTypeInterface $entity_type
*/
public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type, $bundle);
/**
* Determines if this form is for a moderated entity.
*
* @param \Drupal\Core\Form\FormInterface $form_object
* The form definition object for this form.
*
* @return bool
* TRUE if the form is for an entity that is subject to moderation, FALSE
* otherwise.
*/
public function isModeratedEntityForm(FormInterface $form_object);
/**
* Determines if the form is the bundle edit of a revisionable entity.
*
* The logic here is not entirely clear, but seems to work. The form- and
* entity-dereference chaining seems excessive but is what works.
*
* @param \Drupal\Core\Form\FormInterface $form_object
* The form definition object for this form.
*
* @return bool
* True if the form is the bundle edit form for an entity type that supports
* revisions, false otherwise.
*/
public function isRevisionableBundleForm(FormInterface $form_object);
/**
* Loads the latest revision of a specific entity.
*
......
......@@ -3,10 +3,8 @@
namespace Drupal\Tests\content_moderation\Unit;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityFormInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
......@@ -113,38 +111,6 @@ public function testShouldModerateEntities($status) {
$this->assertEquals($status, $moderation_information->shouldModerateEntitiesOfBundle($entity_type, 'test_bundle'));
}
/**
* @dataProvider providerBoolean
* @covers ::isModeratedEntityForm
*/
public function testIsModeratedEntityForm($status) {
$entity_type = new ContentEntityType([
'id' => 'test_entity_type',
'bundle_entity_type' => 'entity_test_bundle',
]);
$entity = $this->prophesize(ContentEntityInterface::class);
$entity->getEntityType()->willReturn($entity_type);
$entity->bundle()->willReturn('test_bundle');
$form = $this->prophesize(ContentEntityFormInterface::class);
$form->getEntity()->willReturn($entity);
$moderation_information = new ModerationInformation($this->setupModerationEntityManager($status), $this->getUser());
$this->assertEquals($status, $moderation_information->isModeratedEntityForm($form->reveal()));
}
/**
* @covers ::isModeratedEntityForm
*/
public function testIsModeratedEntityFormWithNonContentEntityForm() {
$form = $this->prophesize(EntityFormInterface::class);
$moderation_information = new ModerationInformation($this->setupModerationEntityManager(TRUE), $this->getUser());
$this->assertFalse($moderation_information->isModeratedEntityForm($form->reveal()));
}
/**
* Data provider for several tests.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment