Steps to reproduce:

1. Enable moderation states on Article
2. Add content (Article)
3. Disable moderation states on Article
4. Add content (Article) => creates following error message

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Field\WidgetBase::errorElement() must be of the type array, null given, called in /var/www/build/html/core/lib/Drupal/Core/Field/WidgetBase.php on line 441 in Drupal\Core\Field\WidgetBase->errorElement() (line 500 of core/lib/Drupal/Core/Field/WidgetBase.php).

Drupal\Core\Field\WidgetBase->errorElement(NULL, Object, Array, Object) (Line: 441)
Drupal\Core\Field\WidgetBase->flagErrors(Object, Object, Array, Object) (Line: 255)
Drupal\Core\Entity\Entity\EntityFormDisplay->flagWidgetsErrorsFromViolations(Object, Array, Object) (Line: 157)
Drupal\Core\Entity\ContentEntityForm->flagViolations(Object, Array, Object) (Line: 103)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 83)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 270)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'node_article_form') (Line: 119)
Drupal\Core\Form\FormValidator->validateForm('node_article_form', Array, Object) (Line: 569)
Drupal\Core\Form\FormBuilder->processForm('node_article_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('node_article_form', Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object) (Line: 113)
Drupal\node\Controller\NodeController->add(Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 649)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pericxc created an issue. See original summary.

cilefen’s picture

Issue summary: View changes
alexpott’s picture

This is somewhat mitigated by re-enabling moderation on the article fixes the problem - just in case anyone as enabled content moderation on a live site.

Wrt to the problem there is a question about what should happen when disabling moderation on a bundle for the existing entities under moderation. Should we remove all the states? So that if you enable again they get the default state? Not sure.

timmillwood’s picture

Assigned: Unassigned » timmillwood

I'll get a patch written for this today (once I've worked out once the issue is).

timmillwood’s picture

Here's a fix for this.

timmillwood’s picture

Status: Active » Needs review
pericxc’s picture

Thank you @timmillwood for the fix, just tested it. It works fine.

pericxc’s picture

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

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

We need a automated test plus I think we need to think about what disabling really means if you have entities under moderation.

timmillwood’s picture

I'm working on extending \Drupal\content_moderation\Tests\ModerationStateNodeTest::testCreatingContent to disable moderation then test a non-moderated node can be added.

I think when moderated is disabled all settings and moderation states should be kept. My bigger concern is when enabling moderation all entities without a moderation state will get the default "draft" state even if they're published.

timmillwood’s picture

Status: Needs work » Needs review
FileSize
3.25 KB
2.22 KB

Here's a test that covers this.

timmillwood’s picture

Issue tags: -Needs tests

Removing tag

alexpott’s picture

Still not completely sure what disabling means for nodes created after and then published and then content_moderation is enabled again.

timmillwood’s picture

@alexpott - I think that is a separate discussion because the same applies for any nodes created prior to enabling moderation regardless of the fact that moderation may or may not have been enabled previously.

Take the example of a Drupal 8.1.x site with X published nodes, and Y unpublished nodes. When upgrading to 8.2.x and enabling moderation all of these nodes will not have a ContentModerationState entity associated with them, therefore $entity->moderation_state->target_id will return 'draft' (or whatever the default state is for the bundle).

I think we need a method that is run when enabling moderation on a bundle which makes sure each entity has a ContentModerationState entity of either the default state for unpublished nodes, and the published state for published nodes. I guess it is possible to have multiple published states, but we would just have to pick one. Then do we do this for every revision, so that reverting works? Do we do this for every language?

As this spirals I am 100% sure this is a separate issue.

timmillwood’s picture

alexpott’s picture

FileSize
1.45 KB

Here's a slightly different approach that saves quite a few queries for non moderated content entities.

alexpott’s picture

timmillwood’s picture

I'm happy with that @alexpott and would RTBC it if I were allowed ;)

catch’s picture

That fix looks reasonable. Follow-up to determine the enable/disable behaviour also reasonable, bumped it to major.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me as well.

  • catch committed 59e527c on 8.3.x
    Issue #2811927 by timmillwood, alexpott: Disabling Content moderation on...

  • catch committed 3db25ab on 8.2.x
    Issue #2811927 by timmillwood, alexpott: Disabling Content moderation on...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.3.x and cherry-picked to 8.2.x. Thanks!

Status: Fixed » Closed (fixed)

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