diff --git a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php index 9a35f90..e5fa4ba 100644 --- a/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php +++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php @@ -105,15 +105,20 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { - /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) { $entity = $items->getEntity(); - - /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */ if (!$this->moderationInformation->isModeratedEntity($entity)) { - // @todo https://www.drupal.org/node/2779933 write a test for this. - return $element + ['#access' => FALSE]; + return []; } + return parent::form($items, $form, $form_state, $get_delta); + } + + /** + * {@inheritdoc} + */ + public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ + $entity = $items->getEntity(); $workflow = $this->moderationInformation->getWorkflowForEntity($entity); $default = $items->get($delta)->value ? $workflow->getTypePlugin()->getState($items->get($delta)->value) : $workflow->getTypePlugin()->getInitialState($workflow, $entity); diff --git a/core/modules/content_moderation/tests/src/Kernel/ModerationStateWidgetTest.php b/core/modules/content_moderation/tests/src/Kernel/ModerationStateWidgetTest.php new file mode 100644 index 0000000..76d797d --- /dev/null +++ b/core/modules/content_moderation/tests/src/Kernel/ModerationStateWidgetTest.php @@ -0,0 +1,78 @@ +installEntitySchema('content_moderation_state'); + $this->installEntitySchema('user'); + $this->installConfig(['content_moderation', 'system']); + + NodeType::create([ + 'type' => 'moderated', + ])->save(); + NodeType::create([ + 'type' => 'unmoderated', + ])->save(); + + $workflow = Workflow::load('editorial'); + $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated'); + $workflow->save(); + } + + /** + * Test the widget does not impact a non-moderated entity. + */ + public function testWidgetNonModeratedEntity() { + // Create an unmoderated entity and build a form display which will include + // the ModerationStateWidget plugin, in a hidden state. + $entity = Node::create([ + 'type' => 'unmoderated', + ]); + $entity_form_display = EntityFormDisplay::create([ + 'targetEntityType' => 'node', + 'bundle' => 'unmoderated', + 'mode' => 'default', + 'status' => TRUE, + ]); + $form = []; + $form_state = new FormState(); + $entity_form_display->buildForm($entity, $form, $form_state); + + // The moderation_state field should have no values for an entity that isn't + // being moderated. + $entity_form_display->extractFormValues($entity, $form, $form_state); + $this->assertEquals(0, $entity->moderation_state->count()); + } + +}