Problem/Motivation

When using a module providing Conditions, such as Rules, it is likely to end up with "broken" Menu Position Rules due to Form API's handling of boolean fields. This can easily lead to non-activated MPRs and even whitepages because of missing contexts as is the case with rules.

This issue has been solved in Core (#2811519: Blocks do not appear after being placed with the Rules module enabled (or other missing schemata for Condition plugins)) for Block plugins, but we need to also apply this fix to Menu Position Rules. Their explanation of the problem is as follows:

In order to know whether a Condition plugin has been configured, the submitted values are compared to its default state. If they match exactly, it is consider to be not configured, and is removed.
The Form API will submit Boolean values as 1 or 0 instead of TRUE or FALSE, which breaks this comparison.
[...]
Therefore, all condition plugins without valid schema would not be correctly removed, and would be consulted during access checks for blocks.
This caused any blocks configured (while a module like Rules was enabled) to stop displaying.

Proposed resolution

Assure that negate is in fact boolean before storing configured Conditions.

Remaining tasks

Implement an update hook to fix existing affected rules. We might be able to do this similar to the way Core handled it:

/**
 * Fix invalid 'negate' values in block visibility conditions.
 */
function block_post_update_fix_negate_in_conditions() {
  $block_storage = \Drupal::entityTypeManager()->getStorage('block');
  /** @var \Drupal\block\BlockInterface[] $blocks */
  $blocks = $block_storage->loadMultiple();
  foreach ($blocks as $block) {
    $block_needs_saving = FALSE;
    // Check each visibility condition for an invalid negate value, and fix it.
    foreach ($block->getVisibilityConditions() as $condition_id => $condition) {
      $configuration = $condition->getConfiguration();
      if (array_key_exists('negate', $configuration) && !is_bool($configuration['negate'])) {
        $configuration['negate'] = (bool) $configuration['negate'];
        $condition->setConfiguration($configuration);
        $block_needs_saving = TRUE;
      }
    }
    if ($block_needs_saving) {
      $block->save();
    }
  }
}
CommentFileSizeAuthor
#2 condition_not_empty-2856304-2.patch683 bytesckaotik
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ckaotik created an issue. See original summary.

ckaotik’s picture

Assigned: ckaotik » Unassigned
Status: Active » Needs review
FileSize
683 bytes

Please review the attached patch.

ckaotik’s picture

Issue summary: View changes
sassafrass’s picture

I have successfully applied this patch and am not experiencing any issues.

GaëlG’s picture

@sassafrass Do you mean you had issues before applying the patch? If so, why not switch this issue's status to "Reviewed and tested by the community"?

ckaotik’s picture

Status: Needs review » Closed (duplicate)