Problem/Motivation

Hi guys, I noticed that content moderation module throws an exception when I have enabled RNG module at same time.

I was unsure where to report this issue, then I reported into RNG project here:
https://github.com/dpi/rng/issues/142

Basically it's throwing this expception:

Error: Call to undefined method Drupal\rng\Form\EventSettingsForm::getEntity() in Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::processActions() (line 183 of core/modules/content_moderation/src/Plugin/Field/FieldWidget/ModerationStateWidget.php).

Original report

Because "ModerationStateWidget::processActions" is executing a chaining methods, like this:
$entity = $form_state->getFormObject()->getEntity();

Since "EventSettingsForm" is a configuration form that won't be publish or unpublish, I believe that we should setup a validation into "processActions" method:

    if(!method_exists($form_state->getFormObject(), 'getEntity')) {
      return;
    }
    $entity = $form_state->getFormObject()->getEntity();

Steps to reproduce

1. Enable content moderation and rng module
2. Go to Event types(i.e. /admin/structure/rng/event_types)
3. Add new event type.
4. Select "use existing bundle" and select "Article" under the dropdown.
5. Create a new article
6. Visit the "event" tab of the article.

Expected:
- See available events

Actual:
- Fatal error.

Proposed resolution

Remaining tasks

Review

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

keboca created an issue. See original summary.

timmillwood’s picture

Project: Drupal core » RNG - Events and Registrations
Version: 8.3.0 » 8.x-1.x-dev
Component: content_moderation.module » Code

Why does this form has a moderation_state widget on it anyway?

It doesn't look to be a content entity form as it doesn't extent ContentEntityForm, so Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget::processActions should never get called.

This sounds like an issue with RNG or the configuration of RNG.

dpi’s picture

Title: Content Moderation and RNG throw an exception » Content Moderation widget gets entity incorrectly
Project: RNG - Events and Registrations » Drupal core
Version: 8.x-1.x-dev » 8.3.x-dev
Component: Code » content_moderation.module

RNG uses entity form display modes, which is why the error is showing.

ModerationStateWidget is making assumptions that the root form always extends `\Drupal\Core\Entity\EntityForm`. It is trying to grab the entity of the root form.

ModerationStateWidget should instead grab the entity from \Drupal\Core\Field\FieldItemListInterface::getEntity(), which will get the proper entity regardless of which dimension the entity form is attached to.

Unfortunately process callbacks dont have access to the original field list object, so its going to be tricky to do this.

This is not a problem of RNG. I expect other consumers of entity form displays, like inline_entity_form?, will come across this issue.

dpi’s picture

Issue summary: View changes
timmillwood’s picture

Version: 8.3.x-dev » 8.4.x-dev
Status: Active » Needs review
Issue tags: +Workflow Initiative
FileSize
1.61 KB

Here's a patch for this.

This will be a non-issue once #2753717: Add select field to choose moderation state on entity forms is in.

timmillwood’s picture

@keboca - Does this fix your issue?

vijaycs85’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
FileSize
242.82 KB
91.75 KB
960.15 KB
267.08 KB

Tested the patch in #5 by following steps to reproduce and patch fixes the issue.

timmillwood’s picture

Issue summary: View changes

I wondered if this needs tests, but equally don't think we need to bother as #2753717: Add select field to choose moderation state on entity forms will remove this widget.

Gábor Hojtsy’s picture

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

Well, #2753717: Add select field to choose moderation state on entity forms has been "almost done" for a while, so I think it would be important to not add untested code assuming that lands soon, while we have been assuming that for a while. Either this needs tests or can be marked duplicate of that IMHO.

amateescu’s picture

Status: Needs work » Closed (duplicate)

I vote for not wasting time if #2753717: Add select field to choose moderation state on entity forms is where we want to go in the end :)