Content Moderation's form element assumes bundles are used for the entity page it's on. Make a custom entity with Drupal Console that doesn't support bundles, and try to create one.

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    /** @var ContentEntityInterface $entity */
    $entity = $items->getEntity();

    /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */
    $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle()); //<--this line crashes everything
    if (!$this->moderationInformation->isModeratedEntity($entity)) {
      // @todo https://www.drupal.org/node/2779933 write a test for this.
      return $element + ['#access' => FALSE];
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RaisinBranCrunch created an issue. See original summary.

timmillwood’s picture

Version: 8.3.0 » 8.4.x-dev
Issue tags: +Workflow Initiative
Related issues: +#2843083: Select entity type / bundle in workflow settings

Good spot!

It's only recently we've started supporting non-bundle entity types, so we really need to increase the testing on this. One of the main outstanding things if you can't enable moderation on a non-bundle entity type, because the current UI is within the bundle settings. So interested to know how you're hitting this. However, this will change once #2843083: Select entity type / bundle in workflow settings gets in, which will allow you to enable moderation on non-bundle entity types.

timmillwood’s picture

Here's a patch for this.

The last submitted patch, 3: 2868429-3-test-only.patch, failed testing.

Sam152’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/content_moderation/src/Tests/ModerationFormTest.php
@@ -1,6 +1,7 @@
 namespace Drupal\content_moderation\Tests;
+use Drupal\workflows\Entity\Workflow;

Super nit, missing newline between namespace and use, hopefully can be fixed on commit.

Otherwise +1 to more comprehensive testing for non-config bundles. Diff makes perfect sense, no need to get an instance of the bundle for an exception only.

Ada Hernandez’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
5.45 KB
434 bytes

adding newline ---> #5

Sam152’s picture

Status: Needs review » Reviewed & tested by the community
RaisinBranCrunch’s picture

Thanks, so glad I was able to be of use. Honestly, I'm not entirely sure how this came up. I didn't look as closely at it at first, but now it seems like this doesn't happen just for any entity. Our site has a custom entity with a field that references a content type that is Workflow'd by content moderation... could that be calling this?

The stack trace that led up to the error was from WidgetBase:

'#0 /var/www/some-site/core/lib/Drupal/Core/Field/WidgetBase.php(322): Drupal\\content_moderation\\Plugin\\Field\\FieldWidget\\ModerationStateWidget->formElement(Object(Drupal\\content_moderation\\Plugin\\Field\\ModerationStateFieldItemList), 0, Array, Array, Object(Drupal\\Core\\Form\\FormState))
#1 /var/www/some-site/core/lib/Drupal/Core/Field/WidgetBase.php(189): Drupal\\Core\\Field\\WidgetBase->formSingleElement(Object(Drupal\\content_moderation\\Plugin\\Field\\ModerationStateFieldItemList), 0, Array, Array, Object(Drupal\\Core\\Form\\FormState))
#2 /var/www/some-site/core/lib/Drupal/Core/Field/WidgetBase.php(104): Drupal\\Core\\Field\\WidgetBase->formMultipleElements(Object(Drupal\\content_moderation\\Plugin\\Field\\ModerationStateFieldItemList), Array, Object(Drupal\\Core\\Form\\FormState))
#3 /var/www/some-site/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(168): Drupal\\Core\\Field\\WidgetBase->form(Object(Drupal\\content_moderation\\Plugin\\Field\\ModerationStateFieldItemList), Array, Object(Drupal\\Core\\Form\\FormState))

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 6: 2868429-6.patch, failed testing.

Sam152’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
5.54 KB

Reroll and back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php
@@ -109,8 +109,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
 
-    /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */
-    $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle());
     if (!$this->moderationInformation->isModeratedEntity($entity)) {
       // @todo https://www.drupal.org/node/2779933 write a test for this.
       return $element + ['#access' => FALSE];
@@ -119,7 +117,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen

@@ -119,7 +117,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
     $workflow = $this->moderationInformation->getWorkflowForEntity($entity);
     $default = $items->get($delta)->value ? $workflow->getState($items->get($delta)->value) : $workflow->getInitialState();
     if (!$default) {
-      throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label()));
+      throw new \UnexpectedValueException('Invalid moderation state configuration, moderation states are enabled but no default is set.');
     }

Do we want to keep the more specific error message when there's a bundle? Or at least add the entity type to the message?

Sam152’s picture

Status: Needs work » Needs review
FileSize
5.49 KB
1.01 KB

There is no situation when this exception will be triggered.

$default = $items->get($delta)->value ? $workflow->getState($items->get($delta)->value) : $workflow->getInitialState();

In the true part of the ternary, the workflow entity already throws an exception if the state doesn't exist, in the false part now that we have the required_states feature on the ContentModeration workflow type, there will always be something set.

So, this is actually totally safe to remove.

Sam152’s picture

I also need this backported to 8.3. Attached.

Sam152’s picture

Title: getBundleEntityType() can return null » ModerationStateWidget depends on EntityTypeInterface::getBundleEntityType despite content moderation supporting entity types without a bundle.

Status: Needs review » Needs work

The last submitted patch, 13: 2868429-83-backport.patch, failed testing.

Sam152’s picture

Status: Needs work » Needs review
timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Good solution to #11.

Queued an 8.3.x test for #13.

The last submitted patch, 12: 2868429-12.patch, failed testing.

  • catch committed 260f5f6 on 8.4.x
    Issue #2868429 by Sam152, timmillwood, Adita, RaisinBranCrunch, catch:...

  • catch committed c8ffa53 on 8.3.x
    Issue #2868429 by Sam152, timmillwood, Adita, RaisinBranCrunch, catch:...
catch’s picture

Version: 8.4.x-dev » 8.3.x-dev
Status: Reviewed & tested by the community » Fixed

I like that much better. Committed/pushed to 8.4.x and cherry-picked to 8.3.x, thanks!

Neither the 8.4.x nor 8.3.x patches applied to me, but did a local re-roll prior to commit.

tacituseu’s picture

timmillwood’s picture

That's strange because entity_test_mulrevpub exists in 8.3.x: http://cgit.drupalcode.org/drupal/tree/core/modules/system/tests/modules...

tacituseu’s picture

It is intermittent, 30% failure rate judging by https://www.drupal.org/node/3060/qa.
Edit: scratch that, all those "tested on commit" failed.

  • alexpott committed 71c43c1 on 8.3.x
    Revert "Issue #2868429 by Sam152, timmillwood, Adita, RaisinBranCrunch,...
alexpott’s picture

Status: Active » Needs work

I've reverted this on 8.3.x

catch’s picture

Status: Needs work » Needs review

This might be related to #2864008: Convert web tests to browser tests for content_moderation module, at least let's run it past DrupalCI now I've cherry-picked that commit.

timmillwood’s picture

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

Looks great. All the changes are the same as the patch for 8.4.x which I confirmed by applying and committing the changes locally and then checking out the 8.4.x version of the test.

  • catch committed 646d047 on 8.3.x
    Issue #2868429 by Sam152, timmillwood, Adita, catch, RaisinBranCrunch:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.3.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.