Problem/Motivation

Block visibility settings allow to show a block under certain conditions but they have to pass all conditions to be valid. Sometimes it's needed for just a single condition to pass, one of the main features of https://www.drupal.org/project/block_visibility_groups

Steps to reproduce

Place a block on all Articles but also basic page with URL "/test"
Currently the block will never appear, with the change though it can appear under both conditions.

Proposed resolution

Add setting to block form for condition logic

Remaining tasks

Review

User interface changes

update

API changes

NA

Data model changes

NA

Release notes snippet

New block setting for condition logic to change visibility from an "and" condition to also an "or" condition.

CommentFileSizeAuthor
#99 Screenshot 2024-04-01 at 8.31.15 PM.png69.6 KBsmustgrave
#93 923934-93-10.2.x.patch20.4 KBadriancruz
#92 block_visibility_has_wrong_logic-923934-92.patch20.06 KBjohnlutz
#89 interdiff-87_89.txt770 bytesGauravvvv
#89 923934-89.patch19.99 KBGauravvvv
#87 923934-87.patch19.99 KBGauravvvv
#86 923934-86-10.2.x.patch20.06 KBflefle
#81 923934-81.patch22.56 KB_utsavsharma
#81 interdiff_80-81.txt664 bytes_utsavsharma
#80 923934-80-10.1.x.patch20.46 KBDaniel Kulbe
#80 923934-80-9.5.x.patch22.7 KBDaniel Kulbe
#80 923934-80-9.4.x.patch22.63 KBDaniel Kulbe
#79 923934-79-10.1.x.patch20.52 KBDaniel Kulbe
#79 923934-79-9.5.x.patch22.75 KBDaniel Kulbe
#79 923934-79-9.4.x.patch22.68 KBDaniel Kulbe
#78 923934-78-10.1.x.patch20.52 KBDaniel Kulbe
#78 923934-78-9.5.x.patch22.75 KBDaniel Kulbe
#78 923934-78-9.4.x.patch22.68 KBDaniel Kulbe
#76 923934-76-10.1.x.patch14.4 KBDaniel Kulbe
#76 923934-76-9.5.x.patch15.16 KBDaniel Kulbe
#76 923934-76-9.4.x.patch15.14 KBDaniel Kulbe
#74 9.5.0_reroll-923934-72.patch14.3 KBjonnyhocks
#72 9.5.0-reroll-923934-72.patch29.21 KBjonnyhocks
#68 923934 after patch.png73.52 KBaarti zikre
#67 interdiff_65-67.txt469 bytesranjith_kumar_k_u
#67 923934-67.patch14.25 KBranjith_kumar_k_u
#65 reroll-for-9.4.1-923934-65.patch14.38 KBSerhii Shandaliuk
#58 interdiff-923934-56-58.txt2.06 KBmohit_aghera
#58 923934-58.patch14.27 KBmohit_aghera
#56 interdiff-923934-54-56.txt764 bytesmohit_aghera
#56 923934-56.patch13.61 KBmohit_aghera
#54 reroll_diff_923934_44-54.txt7.04 KBankithashetty
#54 923934-54.patch13.62 KBankithashetty
#51 interdiff-923934-47_51.txt764 bytesGauravvvv
#51 923934-51.patch10.89 KBGauravvvv
#50 923934-50-d92.patch13.29 KBnicrodgers
#47 923934-47-2.patch10.9 KBDaniel Kulbe
#47 923934-47-1.patch10.32 KBDaniel Kulbe
#44 923934-44.patch13.26 KBjonnyhocks
#43 after_patch_screenshot.png40.54 KBvikashsoni
#43 before_patch_screenshot.png29.29 KBvikashsoni
#42 923934-42.patch13.31 KBSuresh Prabhu Parkala
#41 923934-40.patch9.13 KBAndrewTur
#39 interdiff_923934_38-39.txt671 bytesankithashetty
#39 923934-39.patch13.24 KBankithashetty
#38 interdiff_923934_37-38.txt1.65 KBankithashetty
#38 923934-38.patch13.24 KBankithashetty
#37 923934-37.patch13.23 KBAndrewTur
#36 923934-36.patch13.3 KBravi.shankar
#32 block_visibility_has_wrong_logic-923934-14-reroll-32-drupal-8.8.2.patch13.45 KBB-Prod
#29 block_visibility_has_wrong_logic-923934-14-reroll-29.patch13.91 KBccjjmartin
#27 block_visibility_has_wrong_logic-923934-14-reroll-27.patch12.99 KBmr.york
#23 patch_after_apply.png58.51 KBarunkumark
#15 block_visibility_has_wrong_logic-923934-14.patch14.1 KBdrugan
#12 block_visibility_has_wrong_logic-923934-12.patch11.83 KBdrugan

Issue fork drupal-923934

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mdupont’s picture

What is wrong in the logic? The examples you give are all behaving correctly.

In your first example, you set the block to display only on nodes of content type "Blog entry". Blogs page is not of content type "blog entry" so the block won't show.

In your second example, the block will only display on the page with the path "blog", therefore the Blogs page. To display on several pages, enter "blog/*" under the first line, so the block will be displayed on any page with the path blog/[anything].

Your third example is the same as the second one, where you added a restriction on the content type. As the page with the path "blog" is not of content type "Blog entry", the block will never be displayed.

I assume that your Blogs page is accessible under "blog" and your blog entries under "blog/[something]". In this case, what you'd want to do is to configure your block this way :
- Tick "Only the listed pages"
- Write "blog" and "blog/*" in the textarea
- Make sure "Content types" is empty

mertskeli’s picture

So, there is no option which allows to show a block on both a page and a node type.
No concerns regarding 1 and 2. But option 3 makes it a bug.
A page and a node are two different things, and visibility per page and visibility per node (type) are supposed to be of no relation to each other, until we consider example.com/node "a page" or a node has a URL alias.

I assume that your Blogs page is accessible under "blog" and your blog entries under "blog/[something]".

Every blog entry is a node, not a page, and is a node/[something], not a blog/[something].

mdupont’s picture

Status: Active » Closed (works as designed)

It works as designed. Visibility filters add up. If the current page is not a node, it won't have a node type, so you won't want to show the block on it if you have set up a filter to show it only on nodes of a certain type.

For a more flexible way to set the block visibility, you can look for contrib modules (like Panels or Context) or write a PHP snippet which will give you all the flexibility you need (like in your case: display the block on the Blogs page AND on each node of type blog entry). A quick search should give you some ready-to-use snippets.

mertskeli’s picture

mdupont, you misunderstood this issue, but I'm not going to re-open it.

If the current page is not a node, it won't have a node type, so you won't want to show the block on it

Blog IS a page, and it includes a list of recent/all posts.

a PHP snippet

True for D6. But this issue is per D7 which does not have php option in block settings.

mdupont’s picture

I understand your frustration with the current block visibility settings, but it works in D7 the same way than in D6. I think I understood the issue.

In D7 you are still able to use a PHP snippet to control block visibility (as unfriendly as it is), if you have enabled the PHP filter module (in core).

Blog is a page, indeed, but it's not a node. It's a custom page created for the blog module. Therefore it is not a blog entry node. If you restrict block visibility to blog entry nodes, the block won't show up on any custom (non node) pages nor on any node of a different type than blog entry.

So in the end it works as designed, but that doesn't mean than the way it was designed is good. What your issue is really about is a feature request (not a bug report) for introducing a kind of "OR filter".

Explanation: in addition to being able to filter block visibility by path AND by node type, as it is currently, you want to be able to filter block visibility by path OR by node type. In the first case, you are effectively restricting visibility only to nodes with the corresponding type and path. In the second case, the block will show up as soon as one of the conditions is met (is a node of the corresponding type OR is any page that matches the path).

The issue you mentions would be fixed if the block system would work like in the second case. It is not because the block system works like in the first case, it is an AND filter. It is not wrong logic, but for you it is not the logic you need.

Quoting you:

A page and a node are two different things, and visibility per page and visibility per node (type) are supposed to be of no relation to each other

This is where you're wrong. Visibility settings are in relation and add up. Usually, if you tick a content type under "Show block for specific content types" (and you apply no other visibility restriction), you don't expect the block to show elsewhere than on node/[nid] pages where the node has the given content type, did you?

In the end, a solution would be a new feature like an additional radio button in the block visibility settings:
"Show block if:
[ ] One of these conditions is met
[ ] All of these conditions are met
"

Of course, as this stage, such a feature would not be added to Drupal core, it belongs to a contrib module to be developed, and maybe, if it is good enough, it could be integrated into D8.

You still have other options : you could use Pathauto to generate URL aliases like "blog/[node:title]" for blog entry nodes and restrict block visibility by path only, or you could enable the PHP filter module and write a code snippet.

mertskeli’s picture

(I appreciate your calm and detailed answer.)
D7 makes it only OR logic, comparing two incomparable terms. It is wrong. Maybe it is not a functional bug (although it looks like a functional perversion) but it is a UI bug for sure.
The visibility setting says: Page and Node (type). As if a node can never be on a page, and a page is unable to contain nodes.
So for your list of "Recent blog nodes" you have to choose either a blog page, OR a blog node. Or you have to download 2-3 more modules to make it work.

mdupont’s picture

Right, as you said the current UI doesn't provide a straightforward way to display a block both on the blog listing page and blog nodes. Drupal 7 is not the only version affected, former versions had the same block visibility UI logic and limitations.

The bug therefore lies in the fact that the UI doesn't make it clear that different "categories" of filters add up and aren't independent from each other. So if you take D7 filters, you have "Pages", "Content types", "Roles" and "Users" and each of them must return TRUE for the block to be displayed :
- "Pages": should it be displayed at this URL?
- "Content types": are we on a node page and should it be displayed for a node of this content type?
- "Roles": does the current user belong to one of the roles for which we should display the block?
- "Users": Can users decide to show or hide this block in their preferences, and is the current user's setting to show this block?
The block will only be displayed if you can answer "yes" to all these questions. The UI doesn't make it clear for users.

As this issue already contains quite a few posts, I suggest to open a new issue stating that the block visibility settings UI is confusing, and linking back to this one.

mertskeli’s picture

I suggest to forget about it. If too many people complain, it will be fixed. For now it seems to be acceptable.

mdupont’s picture

edurenye’s picture

Version: 7.x-dev » 8.5.x-dev
Issue summary: View changes
Status: Closed (works as designed) » Active

I'm reopening this issue because in the exposed case was working as designed but there is another situation (at least in d8) that does not follow the rules, if you set visible in /blog and in /node/* and then in content type you select all except blog post and negate the condition, then it's supposed to show the block in blog and in the blog posts but it's just shown in the blog posts.

cilefen’s picture

drugan’s picture

First, Negate the condition checkbox is returned into place.

Second, added AND / OR conditions conjunction operator option. The logic is simple: in the case of AND all conditions (negated included) should pass. Actually, it is the current state without the patch applied. In the case of OR at least one of the conditions set should pass. If no one is passed then the block will be hidden. If no one condition is set then the AND / OR setting has no effect and block will be visible everywhere.

Third, removed Show for the listed pages / Hide for the listed pages options from the Pages condition which is confusing (at least for me). The Negate the condition is quite easy to understand and it's enough as I think. Also, if user will try to negate the empty Pages value or * (asterisk, which is basically has the same effect as empty) then the negate option will be forcibly set to FALSE when saving the block.

The caveat is that if for example you have only two content types Article and Basic page and they are all checked and negated then the block will disappear everywhere in the case of conjunction operator AND or, if conjunction operator is OR and no other conditions are set. Logically, it is insane setting and I don't think that any of admins will try to do it.

You may see working example of the patch on the Commerce Quick Purchase module. Note that when using the module all the blocks on the system will get AND / OR conditions conjunction operator not only the block provided by the module.

drugan’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 12: block_visibility_has_wrong_logic-923934-12.patch, failed testing. View results

drugan’s picture

Status: Needs work » Needs review
FileSize
14.1 KB

The reason for the test fail was in Show for the listed pages / Hide for the listed pages options. Just because it has two radio buttons one of which is always returns TRUE. Actually, I remember was stumbled on some issue where the default Negate the condition option (single checkbox) was changed for radios because of "usability" reasons. As a consequence the tests were adjusted for the change. Which are now returned to the initial state to work with the single FALSE or TRUE value of the checkbox.

Status: Needs review » Needs work

The last submitted patch, 15: block_visibility_has_wrong_logic-923934-14.patch, failed testing. View results

drugan’s picture

Status: Needs work » Needs review

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

acrosman’s picture

Having struggled with the block UI and writing block condition plugins due to the current behavior of assuming AND at all times, I strongly support the notion of adding support for ORs.

drugan’s picture

@acrosman

Actually, this feature was always in the core. I've just implemented it. Look at the line 39:

https://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/Condition/C...

jwilson3’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #15 does exactly what is needed. Block Visibility Groups is a very nice module that should be included in core at some point!

tim.plunkett’s picture

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

This needs automated test coverage before it can go in.

arunkumark’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
58.51 KB

I have manually tested the patch and tested. It seems to be working as expected.

Apply patch

Berdir’s picture

Status: Reviewed & tested by the community » Needs work

This still doesn't have automated test coverage so it can't be RTBC.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

preksha’s picture

Have manually applied this patch and tested, seems to be working fine.

mr.york’s picture

ccjjmartin’s picture

I can confirm the last patch applied to 8.6.2. The author didn't mention tests so we likely still need those.

ccjjmartin’s picture

The last reroll applied properly but missed a chunk a code. Reroll 2 attached.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

B-Prod’s picture

In the last 2 patches, it seems there are some mistakes.
Patch 27 : the OR condition is not taken into account, as there is still a 'and' in the code:
$this->resolveConditions($conditions, 'and')

It needs to use the $and_or variable to evaluate correctly.

Patch 29 : the gathered_contexts variable is not defined, as it has been replace by the $definitions one.

The current patch is a reroll for the current published release (Drupal 8.8.2).

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

ptmkenny’s picture

Referencing another issue related to block visibility conditions.

ravi.shankar’s picture

AndrewTur’s picture

Reroll against dev-9.2.x

ankithashetty’s picture

Fixed Custom failure errors in #37 in the following patch, thanks!

ankithashetty’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

AndrewTur’s picture

Rerolled the patch to apply against 9.1.10

Suresh Prabhu Parkala’s picture

Status: Needs work » Needs review
FileSize
13.31 KB

Re-rolled against 9.3. Please review.

vikashsoni’s picture

Patch apply successfully for reference sharing screenshot

jonnyhocks’s picture

Unfortunately the patch in #39 didn't apply cleanly for me on 9.2.0, so I have re-rolled for 9.2.0.

wanjee’s picture

Patch 923934-40.patch in #41 is missing condition class AndOrCondition declaration.
Applying 923934-44.patch instead against 9.1.10 works.

larowlan’s picture

Title: Block visibility has wrong logic » Add support for OR in block visibility conditions
Version: 9.2.x-dev » 9.3.x-dev
Category: Bug report » Feature request
Issue summary: View changes
Issue tags: +Needs issue summary update, +Bug Smash Initiative

This issue was triaged by the bug smash initiative @lendude, @darvanen, @quietone and myself.

We agreed that this is no longer a bug-report, but now a feature request to add OR support to block visibility conditions.

Reclassifying as such, and updating the issue title.

Also updating the version as features go into 9.3.x

Adding template to the issue summary and adding remaining tasks

Daniel Kulbe’s picture

nicrodgers’s picture

Status: Needs review » Needs work

The patches in #47 are missing the main plugin condition file core/modules/system/src/Plugin/Condition/AndOrCondition.php

nicrodgers’s picture

Assigned: Unassigned » nicrodgers
nicrodgers’s picture

Here's a re-roll of 44 for 9.2.x.

Still needs a re-roll for 9.3.x.

Gauravvvv’s picture

Re-rolled patch #47, against 9.3.x. Please review. attached interdiff for same.

Gauravvvv’s picture

Status: Needs work » Needs review
nicrodgers’s picture

Status: Needs review » Needs work

As mentioned in #48, the patches in #47 are missing the AndOr plugin condition and thus should not be used. The patch in #51 is also missing the plugin because it was based on #47.

For 9.3, I'd suggest rerolling #44.

ankithashetty’s picture

Rerolled the patch in #44 against 9.3.x as suggested in #53, thanks!

quietone’s picture

Status: Needs review » Needs work

Looks like commit-code-check failed.

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
13.61 KB
764 bytes

Fixing commit-code-check.sh failures.
coding standards tests are passing on local now. Should be good to go.

----------------------------------------------------------------------------------------------------
Checking core/modules/block/js/block.es6.js

yarn run v1.22.10
$ cross-env BABEL_ENV=legacy node ./scripts/js/babel-es6-build.js --check --file /Users/mohit/Experiments/dc/drupal8/core/modules/block/js/block.es6.js
[10:07:23] '/Users/mohit/Experiments/dc/drupal8/core/modules/block/js/block.es6.js' is being checked.
✨  Done in 0.92s.
core/modules/block/js/block.es6.js passed

Status: Needs review » Needs work

The last submitted patch, 56: 923934-56.patch, failed testing. View results

mohit_aghera’s picture

Attempting to fix the test cases.

+++ b/core/modules/block/src/BlockForm.php
@@ -262,7 +262,7 @@ protected function buildVisibilityInterface(array $form, FormStateInterface $for
-        $form['node_type']['#title'] = $this->t('Content types');
+        $form['node_type']['#title'] = $this->t('Content types (Deprecated)');
       }

This change looks a little confusing to me. I had to do this because we've altered the titles in the issue https://www.drupal.org/project/drupal/issues/1932810

I think someone else should look into this one as well.

Berdir’s picture

  1. +++ b/core/modules/block/src/BlockForm.php
    @@ -255,36 +260,23 @@ protected function buildVisibilityInterface(array $form, FormStateInterface $for
    -      $form['node_type']['bundles']['#title'] = $this->t('Content types');
    -      $form['node_type']['negate']['#type'] = 'value';
    -      $form['node_type']['negate']['#title_display'] = 'invisible';
    -      $form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
    +      if (isset($form['node_type'])) {
    +        $form['node_type']['#title'] = $this->t('Content types (Deprecated)');
    +      }
    +      if (isset($form['user_role'])) {
    +        $form['user_role']['#title'] = $this->t('Roles');
    +      }
    +      if (isset($form['request_path'])) {
    +        $form['request_path']['#title'] = $this->t('Pages');
    +      }
         }
    +
         if (isset($form['entity_bundle:node'])) {
           $form['entity_bundle:node']['negate']['#type'] = 'value';
    

    the change to old deprecated content types is fine, but it's just that, the old deprecated version. The new entity bundle condition is not being adjusted now, that code is still unchanged, so you nee to update that too.

  2. +++ b/core/modules/block/src/BlockForm.php
    @@ -325,6 +317,16 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
         // Validate visibility condition settings.
    +    if ($path = $form_state->getValue(['visibility', 'request_path'])) {
    +      // Forcibly set FALSE for negate if there is no pages defined,
    +      // otherwise the block will not be displayed even there is no any other
    +      // restrictions set.
    +      // @see \Drupal\Component\Plugin\Exception\ContextException\ConditionAccessResolverTrait
    +      if ((empty($path['pages']) || $path['pages'] === '*') && !empty($path['negate'])) {
    +        $form_state->setValue(['visibility', 'request_path', 'negate'], FALSE);
    +      }
    +    }
    

    shouldn't that be in the plugin itself?

  3. +++ b/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php
    @@ -167,11 +167,16 @@ public function testRecentNodeBlock() {
         $this->assertSession()->pageTextNotContains($label);
    -    $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user', 'route']);
    +    $this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user']);
    +
    +    // Front-page will be un-cacheable due to limitation added in block configuration.
    +    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'UNCACHEABLE');
     
    -    // Ensure that a page that does not have a node context can still be cached,
    -    // the front page is the user page which is already cached from the login
    -    // request above.
    +    // Create an article node and visit it.
    +    $node7 = $this->drupalCreateNode(['uid' => $this->adminUser->id(), 'type' => 'article']);
    +    $this->drupalGet('node/' . $node7->id());
    +    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
    +    $this->drupalGet('node/' . $node7->id());
         $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
    

    I don't really understand why this test changes. why should changes to the UI affect caching?

  4. +++ b/core/modules/system/src/Plugin/Condition/AndOrCondition.php
    @@ -0,0 +1,93 @@
    + * Provides an 'And Or' condition.
    + *
    + * @Condition(
    + *   id = "and_or",
    + *   label = @Translation("Conditions logic"),
    + * )
    + */
    +class AndOrCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
    +
    

    The approach seems strange here, that plugin will also show up in other places that use condition plugins. Why isn't the AND/OR setting just a fixed UI element in the block UI and saved in a separate setting?

quietone’s picture

Status: Needs review » Needs work

NW for #59.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kazah’s picture

Any update?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mmbk’s picture

patch #58 does not apply to core 9.4.x any more.

Serhii Shandaliuk’s picture

Status: Needs work » Needs review
ranjith_kumar_k_u’s picture

aarti zikre’s picture

Issue summary: View changes
FileSize
73.52 KB

Verified the patch for drupal 9.5.x version.
https://www.drupal.org/files/issues/2022-07-14/923934-67.patch

After applying patch we can see that and and or option available for the respective block under configuration section.
Where we can choose And or Or condition. Based on this block will be visible. By default AND is selected.

For OR :

If we have applied filter to the content type to blog and in page restriction page given url of another content type such as basic page/artical/ custom then it will display on both blog content type as well as that mentioned path too.

Reference SS

2022-07-21/923934 after patch.png

Test Result pass
Can be move to RTBC

aarti zikre’s picture

Status: Needs review » Reviewed & tested by the community
quietone’s picture

Status: Reviewed & tested by the community » Needs work

The issue summary is out of date and this issue is tagged for an issue summary update. Lets see that was asked for. It was 11 months ago, in #46, by a committer. An image has been added to the Issue Summary but that is all. Setting back to NW for an issue summary update.

Comment #59 asked questions about the patch and recommended changes. There has been no change to the patch or any discussion of what @Berdir is saying. That comment has, well, been ignored. That means that the reroll and all the following work are not helping to resolve this issue.

Remember to read all the comments before working on an issue as well as the Issue tags.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jonnyhocks’s picture

FileSize
29.21 KB

Understand there are still issues to be addressed as noted in comment 70 but I needed to reroll for a project. I have uploaded here if anyone else were to find it helpful in the interim.

jonnyhocks’s picture

jonnyhocks’s picture

Apologies for uploading a corrupted patch in 72 (contained a merge conflict) - I attach a working re-roll for 9.5.x.

Daniel Kulbe’s picture

Adding an other issue this is somewhat related to (#3264843).

Daniel Kulbe’s picture

Adding an other update here respecting #3264843, which should somehow also address #59 question 1.

Daniel Kulbe’s picture

Took me a moment, but now I can tell - the changed conditions logic does not work properly:

--- a/core/modules/block/src/BlockAccessControlHandler.php
+++ b/core/modules/block/src/BlockAccessControlHandler.php
@@ -85,20 +85,35 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
       $conditions = [];
       $missing_context = FALSE;
       $missing_value = FALSE;
-      foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
+      $visibility = $entity->getVisibilityConditions();
+      $available_contexts = count($visibility);
+      $and_or = 'and';
+      if ($visibility->has('and_or')) {
+        $and_or = $visibility->get('and_or')->getConfiguration()['all'];
+        // This condition was need just to fetch and/or config setting.
+        $visibility->removeInstanceId('and_or');
+      }
+      foreach ($visibility as $condition_id => $condition) {
         if ($condition instanceof ContextAwarePluginInterface) {
           try {
             $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
             $this->contextHandler->applyContextMapping($condition, $contexts);
+            $conditions[$condition_id] = $condition;
           }
           catch (MissingValueContextException $e) {
-            $missing_value = TRUE;
+            $available_contexts--;
+            // The required or last condition checked has no context.
+            if (($and_or == 'and') || !$available_contexts) {
+              $missing_context = TRUE;
+            }
           }
           catch (ContextException $e) {
             $missing_context = TRUE;
           }
         }
-        $conditions[$condition_id] = $condition;
+        else {
+          $conditions[$condition_id] = $condition;
+        }
       }
 
       if ($missing_context) {

The following case applies:

I have in the block 'entity_bundle:node' one of the options checked, also the negated checkbox. So this block should show everywhere, where the page is not a node of this bundle.
Visiting a page different page than a node (e.g. the node list), an MissingValueContextException will be thrown by the plugin, with this being the last of the visibility option or even when the condition logic is 'AND', $missing_context will be TRUE.
This does not meet with the expectation, the block should be shown everywhere else but on a node of that bundle.

On a sidenote, the $missing_value is never set to anything else but FALSE. The case should be dropped, the variable removed when not required anymore.

Daniel Kulbe’s picture

Please also note, I fixed in this Version :

  • Missing context vs. context value, a cache issue, as stated in the comment in code.
  • The conditions count was created prior the and-or plugin was removed.
  • The form
  • I applied the same logic in the radio negate switch form to all considerable elements in the form.
  • I choose to stick to jQuery in block.js

One further opinion on this ... the check regarding the empty request_path plugin - shouldn't it be happen in the plugin form validation method instead. It is called a few lines below where the check was added.

Daniel Kulbe’s picture

FileSize
22.68 KB
22.75 KB
20.52 KB

Sorry, still found a tiny JavaScript selector issue. I will not start the failed tests in 78 again, as nothing has changed.
Can please sb. take care of the functional test, who has some strong suites there.

Daniel Kulbe’s picture

One final thought on this - the original code is right in one further thing: The Condition should be added to the conditions array in every case, even if there is an exception, so when collecting cachability, every condition is applied to forbidden for missing value, not just only the valid ones. Adding another version here, which fixes this.

_utsavsharma’s picture

trickfun’s picture

I think that having one condition logic for all visibility types is wrong.
"user roles" condition should be always in AND mode.

user roles is different from "content type" or "path".

i suggest to separate "user role" from other conditions.
I think the better solution is to have

Visibility (AND or OR for all conditions)

AND

User Visibility (AND or OR for all conditions relative to user)

Thank you

kopeboy’s picture

@ #82: This is a restriction imho: why?
I could say we should leave it more general, as you can't exclude in advance an OR condition on user role.

Here is an example usecase for a block showing diff/debug/editorial information:

  1. Show this block on taxonomy term pages only

    AND
  2. Show this block on admin or edit pages only

    OR
  3. Show this block if user has taxonomy editor role
trickfun’s picture

Is it possible to have this use case?

1. show A block on taxonomy term
OR
2. show A block on /my-custom-url

3. show in the previous page only if user is admin

thank you

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

flefle’s picture

Attached bellow is the adopted patch for 10.2.x.

Gauravvvv’s picture

Status: Needs work » Needs review
FileSize
19.99 KB

Added patch for 11.x

shweta__sharma’s picture

Status: Needs review » Needs work

CCF error

Gauravvvv’s picture

Status: Needs work » Needs review
FileSize
19.99 KB
770 bytes

Tried fixing CCF, attached interdiff for same.

smustgrave’s picture

Status: Needs review » Needs work

Caused a large number of failures.

onfire84’s picture

Tryed Patch #86 on a 10.2.x Drupal Page. Patch does apply, but when i try to configure a block on the blocklayout page, it fails with "Error: Undefined constant "Drupal\block\form_state" in Drupal\block\BlockForm->buildVisibilityInterface() (line 242 of core/modules/block/src/BlockForm.php)."

johnlutz’s picture

Fixed the patch that applies against 10.2.x.

adriancruz’s picture

FileSize
20.4 KB

Patch for 10.2.x

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update, -Needs tests

Hiding patches for clarity

Cleaned up issue summary so removing that tag.

Ran test-only feature and got https://git.drupalcode.org/issue/drupal-923934/-/jobs/1210671 but that shows the schema update

This shows the block placement test. https://git.drupalcode.org/issue/drupal-923934/-/jobs/1211130

So removing that tag as well

Turned patch #93 into an MR with a few changes
Removed some changes to the tests that seemed out of scope.
Turned the new condition into an attribute vs annotation usage.
Added new test coverage.

Suggestions appreciated

smustgrave’s picture

Status: Needs review » Needs work

Posted in slack and @berdir made a recommendation to take a deeper look at #59

smustgrave’s picture

Updated issue summary with new solution.

I've pushed changes to the new MR but there is a test failure I can't figure out why, with the default config.

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review

All tests green, think ready for review of new approach.

acbramley’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Left some comments on the MR, IMO the summary could use a rewording - it took me a few goes to understand what "hit" meant in this context.

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

Updated and cleaned out more to just include a single example.

Addressed feedback

smustgrave’s picture

Issue summary: View changes
smustgrave’s picture

Status: Needs review » Needs work

Forgot the upgrade path test