Save a flagged node to change it's moderation state and you get the following:

The website encountered an unexpected error. Please try again later.

LogicException: The user has already flagged the entity with the flag. in Drupal\flag\FlagService->flag() (line 234 of modules/contrib/flag/src/FlagService.php).
flag_form_save(Object, Array) (Line: 179)
flag_form_submit(Array, Object)
call_user_func_array('flag_form_submit', Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 583)
Drupal\Core\Form\FormBuilder->processForm('node_feature_edit_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, 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: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
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: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
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: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

It appears I can't save the node without unchecking the flag, but we need to be able to keep the flag in place (it's used for featuring content that may need to be updated).

Comments

seanr created an issue. See original summary.

joachim’s picture

Priority: Normal » Critical
Issue tags: -exception

I think we can call this one critical. Thanks for reporting!

(BTW: please don't use the issue tags field for keywords!)

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

Hmm it's working for me, with Workbench Moderation.

Can you give more detail on how to reproduce this please?

socketwench’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
hernani’s picture

I am able to reproduce this bug.

- Create a global flag applicable to a certain content type.
- Define flag to appear in node edit form.
- First flagging works, but following ones done by other users make it fail.

The problem is caused by the fact $flag_service->getAllEntityFlaggings does not return flaggings done by other users to global flags. This means that the second time the node is saved with a global flag and the uid of the user is different from the original one, this fails and returns the error indicated in first post.

tolu_’s picture

Status: Closed (cannot reproduce) » Active
joachim’s picture

Thanks for figuring this one out, @hernani! (And also @tolu_ for updating the status, as otherwise we might not have noticed this for ages!)

I'm not sure what purpose getAllEntityFlaggings() serves, since indeed as you say, it doesn't do anything sensible with global flags.

The only caller of it is flag_form_save(), which has a bunch of things wrong with it, not all of which should be fixed here:

    $flagging_ids = array_map(function($flagging) {
      return $flagging->getFlagId();
    }, $flaggings);

Calling getFlagId() gets you a flag ID. Clue's in the name ;) Why is the array called $flagging_ids?

  foreach ($values as $flag_id => $value) {
    // Get the flag from the Flag Service.
    $flag = $flag_service->getFlagById($flag_id);

Entities should be loaded en masse outside of loops.

    // Determine if the flagging exists.
    $flag_exists = in_array($flag_id, $flagging_ids);

Code and comment don't agree. Determine if a flag or a flagging exists?

What should be fixed here is that flag_form_save() should call loadIsFlagged() rather than getAllEntityFlaggings(). getAllEntityFlaggings() handles global flags properly, and additionally, it doesn't to load full Flagging entities, which we don't need, so there's a performance gain.

getAllEntityFlaggings returns a list of Flag IDs, so for sanity, this should be changed as part of this patch:

    // Determine if the flagging exists.
    $flag_exists = in_array($flag_id, $flagging_ids);

$flagging_ids is incorrect. It's an array of Flag IDs. But since we already have $flag_id around, I suggest something like $flag_ids_flagged, as it's the array of flag IDs that the entity is flagged with.

I'll file a separate issue for loading of the flags.

jhedstrom’s picture

Status: Active » Closed (duplicate)
Related issues: +#2863892: getAllEntityFlaggings() doesn't work with global flags

Confirmed that this is resolved by the fix in #2863892: getAllEntityFlaggings() doesn't work with global flags. Marking as a duplicate.