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:

protected function applyContexts(ConditionPluginCollection &$conditions, $logic) {
    $have_1_testable_condition = FALSE;
    foreach ($conditions as $id => $condition) {
      if ($condition instanceof ContextAwarePluginInterface) {
        try {
          $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));

          // Skip when any of the contexts is not set.
          $missingContextData = FALSE;
          foreach ($contexts as $context) {
            if ($context->getContextData()->getValue() === NULL) {
              if ($logic == 'and') {
                return FALSE;
              }
              else {
                $missingContextData = TRUE;
                break;
              }
            }
          }

          if ($missingContextData) {
            continue;
          }

          $this->contextHandler->applyContextMapping($condition, $contexts);
          $have_1_testable_condition = TRUE;
        }
        catch (ContextException $e) {
          // Log a message about the error.
          // \Drupal::logger('block_visibility_groups')->error($e);

          // If the condition is negated, shouldn't refuse so quickly.
          if ($logic == 'and' && !$condition->isNegated()) {
            // Logic is all and found condition with contextException.
            return FALSE;
          }
          $conditions->removeInstanceId($id);
        }
      }
      else {
        $have_1_testable_condition = TRUE;
      }
    }
    if ($logic == 'or' && !$have_1_testable_condition) {
      return FALSE;
    }
    return TRUE;
  }
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

debra-v created an issue. See original summary.

debra-v’s picture

Issue summary: View changes
debra-v’s picture

Issue summary: View changes
damienmckenna’s picture

Status: Needs work » Active

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.

cobenash’s picture

Status: Active » Needs review
StatusFileSize
new1018 bytes

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. :)

proweb.ua’s picture

Status: Needs review » Reviewed & tested by the community

#5 works

ccjjmartin’s picture

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

joelpittet’s picture

Version: 2.0.4 » 2.0.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

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!

b_sharpe made their first commit to this issue’s fork.

b_sharpe’s picture

Status: Needs work » Needs review

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/...

joelpittet’s picture

Issue tags: -Needs tests

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?

b_sharpe’s picture

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.

ccjjmartin’s picture

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

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.

twod’s picture

Status: Needs review » Needs work

Not sure why that shows as user 0... 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?

greg boggs’s picture

I can confirm, this bug made all my blocks disappear, hrm, never mind, still debugging. Could be a bug from the content of the blocks.

greg boggs’s picture

Ok figured it out.

Once updating to the current release, I have a VG of

path = /example*
Path = /example/search (negated)
Content type = page (negated)

With content type = page (negated) is on the VG, the group does not display when it should on any page or content type or route.

If I use only path based rules, the VG displays as expected. Not sure it's related to this exactly.

greg boggs’s picture

I ended up just switching entirely to path based rules, but found another bug.

/example* works
/example/* works
/example/*/* fails

eddylbs’s picture

Thanks !
MR !26 works for me on Block Visibility Groups 2.0.4 and Drupal core 10.4.7

falco010’s picture

Priority: Normal » Major
Status: Needs work » Needs review

We had the same issues after updating this module to 2.0.4

The MR !26 solves it. Also bumping priority to Major as the latest release is breaking existing functionality.

liampower’s picture

I was facing this issue with webforms losing their page titles (Matched by URL pattern).

Config of the block visibility group had 2 conditions

  • Display block on a list of URL patterns
  • Display block on specific node bundles

The configuration for URL patterns was no longer working, after applying the patch from MR !26 it works again. Thanks!

damienmckenna’s picture

Version: 2.0.x-dev » 2.1.x-dev
Status: Needs review » Needs work

This needs to be rerolled for the 2.1.x branch.

damienmckenna changed the visibility of the branch 2.1.x to hidden.

damienmckenna changed the visibility of the branch 2.0.x to hidden.

damienmckenna’s picture

I copied the test coverage to a new MR for 2.1.x, let's see how the tests perform.

damienmckenna’s picture

The tests currently pass with the test changes on their own. This suggests that the problem doesn't exist in the 2.1.x branch. Can someone please test out whether the problem still happens on their sites with the current 2.1.x branch? Thank you.

debra-v’s picture

I just tried to install the 2.1.x-dev branch to test and it's throwing errors in the block_visibility_groups_admin module

TypeError: Drupal\block_visibility_groups_admin\ConditionCreatorManager::__construct(): Argument #4 ($route_provider) must be of type Drupal\Core\Routing\RouteProviderInterface, ArrayObject given, called in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 in /var/www/html/web/modules/contrib/block_visibility_groups/block_visibility_groups_admin/src/ConditionCreatorManager.php on line 36 
falco010’s picture

Attached is a patch from the MR that can be added via Composer without including any further changes from the MR.

hitchshock’s picture

Status: Needs work » Needs review

I'm facing this issue on the User page.
Steps to reproduce:
- make the block that must be available for a certain content type and the user page (/user/*)
- configure the module:
- Only one condition must pass
- add Request Path condition
- add Inhoudstype condition
- Expected behaviour: block is available on the content page and user page
- Actual behaviour: block is available only on the content page

So the issue exists on 2.0. Also, I can confirm that the test in the MR fails without the fix on 2.0

But... Since the test passes for 2.1, then probably it won't be an issue there, and the root cause is fixed.

So, my suggestion is:
- merge the updated tests into 2.1
- to do a backport of the fix from 2.1 -> 2.0, cuz the issue is really a critical one

  • joelpittet committed dd36fdeb on 2.0.x authored by b_sharpe
    fix: #3503194 Visibility incorrectly returns false on OR logic when...
joelpittet’s picture

Status: Needs review » Fixed

Committed thank you all!

Your examples became the test suite almost one-for-one: content type OR path (@hitchshock, @liampower), content type OR vocabulary (@debra-v), and the negated condition case (@greg-boggs). Thanks to @b_sharpe for the MR, @cobenash and @falco010 for patches, @damienmckenna for the 2.1.x reroll, @ccjjmartin for the #2864027: Allow choosing more than one visibility group per block pointer that stopped us double-fixing, and @proweb.ua, @eddylbs and @twod for testing and review.

Still open: @twod's point in #15 about optional contexts being treated as required. That deserves its own issue rather than riding along here ... I'll leave this one for someone to file. And the 2.1.x branch has some fun errors that I need to dig into... and some random test failures this evening were quite a pain... but the 2.0.x merge 🚂 passed!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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