Visibility incorrectly returns false on OR logic when context missing from a different condition

Created on 29 January 2025, 2 months ago

Problem/Motivation

Blocks incorrectly not displaying after upgrade from 2.0.3 to 2.0.4

Steps to reproduce

Set up a block with a block visibility group having 2 separate mutually exclusive conditions where only 1 must pass (ie. one condition based on content-type and one condition based on vocabulary type).

Block will not display when one condition passes because the other condition has a null context.

Proposed resolution

Check that $logic = 'and' before returning false from applyContexts() when context data is null for one condition.
Current code

Suggested code:

          // Skip when any of the contexts is not set.
          if ($logic == 'and') {
            foreach ($contexts as $context) {
              if ($context->getContextData()->getValue() === NULL) {
                return FALSE;
              }
            }
          }
πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States debra-v

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @debra-v
  • πŸ‡ΊπŸ‡ΈUnited States debra-v
  • πŸ‡ΊπŸ‡ΈUnited States debra-v
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Thank you for reporting the bug, I'm sorry we left that bug in the module and didn't catch it.

    FYI the correct status for a new issue is "Active"; "needs work" is for when a solution has been proposed via a patch or merge request but it needs further work.

  • πŸ‡ΉπŸ‡ΌTaiwan cobenash Taipei

    Hey Folks,

    I have the same issue. My environment is as follows:

    * Drupal 10.4.2
    * Block visibility group 2.0.4

    Thanks for the code provided by @debra-v

    It works on my side, and I uploaded a patch regarding the proposed code.

    So I changed the status from active to needs review. :)

  • πŸ‡ΊπŸ‡¦Ukraine proweb.ua

    #5 works

  • πŸ‡ΊπŸ‡ΈUnited States ccjjmartin Austin, TX

    #5 fixes broken logic conditions for me, +1 RTBC

  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    The code differs significantly from the 2.1.x branch. Adding tests would help ensure this bug is fixed in both branches.

    @cobenash, @proweb.ua, @ccjjmartin – would any of you be able to write a test to prevent regressions? A merge request would be preferred, but I appreciate the patch!

  • First commit to issue fork.
  • Pipeline finished with Success
    about 1 month ago
    Total: 424s
    #440097
  • Pipeline finished with Success
    about 1 month ago
    Total: 240s
    #440107
  • πŸ‡¨πŸ‡¦Canada b_sharpe

    Moved to MR, edited existing test to use two different contexts so that the AND/OR conditions are properly evaluated / tested.

    Test fails without patch: https://git.drupalcode.org/issue/block_visibility_groups-3503194/-/jobs/...

    Test passes with patch: https://git.drupalcode.org/issue/block_visibility_groups-3503194/-/jobs/...

  • πŸ‡¨πŸ‡¦Canada joelpittet Vancouver

    Wow, that was quick! Were you already working on the test, @b_sharpe?

    I always hesitate to modify an existing test instead of adding a new one since there’s a risk of losing valuable coverage. It’s a bit tricky to tell if anything important is lost here, but seeing the red/green tests is definitely encouraging! Can you alleviate that worry?

  • πŸ‡¨πŸ‡¦Canada b_sharpe

    Ha, was actually just coincidence, I needed this to fix a prod instance today.

    I would normally agree about a new test over editing; however, in this case the edit made more sense as the old test is for testing both AND/OR (testMultipleConditions()) and would have caught this issue in the first place had there been more than one context so I figured the edit made more sense.

  • πŸ‡ΊπŸ‡ΈUnited States ccjjmartin Austin, TX

    This issue is attempting to fix the broken logic in the 2.0.4 version of the module, which I can verify is a real bug. However, the proposed change here will be in conflict with the direction taken in the 2.1.x version of the module, see this issue: https://www.drupal.org/project/block_visibility_groups/issues/2864027 ✨ Allow choosing more than 1 visibility group per block Needs work

    There is a backport patch to the 2.0.x version of the module available there that modifies the same lines of code the proposed patch here is modifying. I believe the other patch fixes this issue as well and knowing it was already merged in the 2.1.x version, that in theory is the direction the maintainers was to head. Ideally someone who understands the code in this patch would review the affected code in the already merged branch to verify if the issue here was fixed or not.

  • πŸ‡ΈπŸ‡ͺSweden twod Sweden

    Never mind, it fixed itself.

    I tested this with a condition that has two optional contexts, and if one or more of them didn't have a value it prevented the block from being displayed, which kinda defeats the purpose of the contexts being set as not required...

    Maybe instead of FALSE it gets set to the inverse of the context definition's required flag?

Production build 0.71.5 2024