We can set visibility conditions (variant selection criteria) at page and variant level, but it would be nice to apply visibility rules on individual blocks (panes). You used to be able to do this in D7.

There's a patch doing exactly this for Page Manager in #2858877: Allow for block visibility rules. The patch currently being worked on in this issue leverages that. However it currently means introducing a new dependency on Page Manager. Possible solutions:

  • Factor the visibility/condition form functionality into core.
  • Factor the visibility/condition form functionality into ctools (which is already a dependency of both Page Manager and Panels).
  • Make Panels only show/use the visibility conditions if Page Manager is installed (ugly imho).
  • Any other ideas...?!

@vbouchet took a different approach in solving the problem by providing a new kind of access plugin for Panels 3.x and 4.x in #11.

Remaining tasks

CommentFileSizeAuthor
#66 panels-block_visibility_conditions-4-7-2769099-65.patch14.36 KBrenatog
#65 panels-block_visibility_conditions-4.7-2769099-65.patch14.33 KBrenatog
#60 panels-block_visibility_conditions-2769099-60.patch14.59 KBlobodakyrylo
#58 panels-block_visibility_conditions-2769099-58.patch14.61 KBlobodakyrylo
#53 panels-after-update.jpg27.48 KBcrutch
#53 panels-before-update.jpg78.35 KBcrutch
#44 panels-block_visibility_conditions-2769099-43.patch14.56 KBocastle
#42 panels-block_visibility_conditions-2769099-42.patch14.26 KBocastle
#40 reroll_diff_36-40.txt9.81 KBocastle
#40 panels-block_visibility_conditions-2769099-40.patch14.57 KBocastle
#36 panels-block_visibility_conditions-2769099-36-D8.patch14.38 KBartematem
#33 interdiff-2769099-32-33.txt2.68 KBAndyF
#33 panels-block_visibility_conditions-2769099-33-D8-do-not-test.patch14.38 KBAndyF
#32 interdiff-2769099-18-32.txt10.66 KBAndyF
#32 panels-block_visibility_conditions-2769099-32-D8-do-not-test.patch13.72 KBAndyF
#18 panels-block_visibility_conditions-2769099-18-D8-do-not-test.patch13.77 KBdrupov
#15 panels-block_visibility_conditions-2769099-15-D8-do-not-test.patch11.79 KBdrupov
#14 panels-block_visibility_conditions-2769099-14-D8-do-not-test.patch10.58 KBdrupov
#12 panels-block_visibility_conditions-2769099-12-D8-do-not-test.patch11.62 KBAndyF
#10 screenshot-local.light_.com 2017-04-21 09-27-41.png20.5 KBvbouchet
#10 Screen Shot 2017-04-21 at 09.27.09.png22.61 KBvbouchet

Issue fork panels-2769099

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

kriboogh created an issue. See original summary.

artreaktor’s picture

+1 Would be great to have that feature!

Andre-B’s picture

how would this currently be solved in Drupal 8?

sketman’s picture

+1 for this feature. This was an excellent additions to Panels comparing with Display Suite

andypost’s picture

Category: Feature request » Task

This is a regression from d7 - this is must have

dstorozhuk’s picture

Issue summary: View changes

Update descritition

dstorozhuk’s picture

m.abdulqader’s picture

+1

dstorozhuk’s picture

vbouchet’s picture

Hi,

Just to let you know that I am currently working on it. You can follow the progress on https://github.com/vbouchet31/panels/pull/1 and will provide a proper patch a soon as the TODO are finalized. This currently works properly (at least what I tested) with Page manager and 8.x-3.x branch of Panels.

TODO:

  • Manage how to access the form on Panels IPE
  • Investigate the required work to deal with 8.x.4.x branch
  • Test, test, test

screenshot step 1
screenshot step 2

vbouchet’s picture

I updated the code mainly to integrate with Panels IPE. It is not perfect (mainly because it currently use the same picto as the configure link - the gear) but at least the Visibility rule settings form is accessible via IPE.

Also, I ported the code to the 4.x branch.

Patch for the 8.x-3.x branch
Patch for the 8.x-4.x branch

The main thing missing now is proper test (as usual ;-) ).

Reviews and feedback are welcome.

AndyF’s picture

If I understand correctly #11 introduces new panels access plugins; I came here looking for something that integrates with existing condition plugins. (See also #2858877: Allow for block visibility rules in Page Manager.) Here's a first pass at a Panels patch: it needs the patch from #2858877: Allow for block visibility rules.

Feedback welcome, thanks.

Still todo

  • IPE integration
  • tests

I'm not marking as needs review because it probably makes sense to wait for the issue in Page Manager to be fixed.

AndyF’s picture

Version: 8.x-3.x-dev » 8.x-4.x-dev
drupov’s picture

Re-rolled the patch against the latest dev (Commit dd6e30d on 8.x-4.x)

drupov’s picture

Sorry, some code was missing

andypost’s picture

There's initial patches for review

millionleaves’s picture

I've applied this patch along with the Page Manager patch.

I've already posted this to the Page Manager issue - sorry for the double posting, but I'm unsure whether it's Page Manager or Panels (or something else) generating the issue.

https://www.drupal.org/node/2858877#comment-12269985

When viewing a node using a Panels variant containing a block where visibility of the block is based on a specified ID, I am now getting this error:

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

The server log contains this:

Uncaught PHP Exception InvalidArgumentException: "You must provide the context IDs in the @{service_id}:{unqualified_context_id} format." at /path/to/mysite/core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php line 71

Removing the block visibility rule removes this error and displays the page as it was.

I've tried with the current and -dev versions of both Page Manager and Panels.

drupov’s picture

OK, I reviewed the patch from #15 and there are some methods missing in PanelsBlockConfigureFormBase

swigle’s picture

Tried to apply this patch to most recent version of panels publicly available (8.x-4.2). Patching returns the following:

sudo patch -p1 < panels-block_visibility_conditions-2769099-18-D8-do-not-test.patch
patching file src/Form/PanelsAddBlockForm.php
patching file src/Form/PanelsBlockConfigureFormBase.php
patching file src/Plugin/DisplayBuilder/StandardDisplayBuilder.php
Hunk #1 FAILED at 2.
Hunk #2 succeeded at 36 (offset -1 lines).
Hunk #3 FAILED at 51.
Hunk #4 FAILED at 68.
Hunk #5 FAILED at 90.
Hunk #6 succeeded at 97 (offset -12 lines).
Hunk #7 succeeded at 113 (offset -12 lines).
Hunk #8 succeeded at 159 (offset -14 lines).
Hunk #9 succeeded at 191 (offset -14 lines).
4 out of 9 hunks FAILED -- saving rejects to file src/Plugin/DisplayBuilder/StandardDisplayBuilder.php.rej

A visual comparison of StandardDisplayBuilder.php and the patch, i see a couple of differences, mainly that my version of this file doesn't mention any \Drupal\Core\Extension\ModuleHandlerInterface being used, but the patch file clearly shows this, although not as a change but as something that should be in the source/base file, which it isnt...

I'm trying to patch in the optional visibility for panel blocks, just like millionleaves is. Page manager patch worked, this one doesnt.
Manually adding the ModuleHandlerInterface kinda works, but when trying to edit a block on a page manager page will give me an ajax error:

"An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/panels/page_manager.page/node--node-panels_variant-0/edit/27eac8e2-dd2f-45e3-968f-ec5c57614739?destination=/admin/structure/page_manager/manage/node/page_variant__node-panels_variant-0__content
StatusText: OK
ResponseText: Error: Class &#039;Drupal\page_manager_ui\Form\VisibilitySubform&#039; not found in Drupal\panels\Form\PanelsBlockConfigureFormBase-&gt;__construct() (line 92 of /export/home/research.beta7.swigledev.nl/htdocs/web/modules/contrib/panels/src/Form/PanelsBlockConfigureFormBase.php)."

Am i using the right version of 8.x- panels? or is something else going wrong?

drupov’s picture

You should use the latest dev version of 8.x-4.x, currently at commit

9ae218c Issue #2913353 by phenaproxima: IPE attaches behaviors a bit too quickly

swigle’s picture

Thanks, that did it!
Checked out latest dev version, and reinstalled page_manager. Ran both patches and cleared cache. Everything seems to be in order now!

TD44’s picture

Does the patch #18 has been commited ?

millionleaves’s picture

The patch in #18 works for me, but there is a conflict with this patch:

https://www.drupal.org/project/panels/issues/2849867#comment-12431146 - Custom attributes in panels blocks

I've added a comment to that issue which explains how I worked around the conflict:

https://www.drupal.org/project/panels/issues/2849867#comment-12471980

My end goal with these two patches is:

  • Set block visibility within Panels pages
  • Set CSS classes and IDs on blocks when placing them in a Panels page

With this resolution I've been able to achieve both.

manuel.adan’s picture

Status: Needs review » Needs work

Patch from #18 uses "Drupal\page_manager_ui\Form\VisibilitySubform". This class:

  • is not present in panels, it belongs to the page manager UI sub-module, which would introduce dependency on it
  • it was removed at some point from page_manager, so the patch does not work with the latest page_manager release
AndyF’s picture

Just to clarify, patch from #18 originally came from #12 which states:

Here's a first pass at a Panels patch: it needs the patch from #2858877: Allow for block visibility rules.

Feedback welcome, thanks.

Thanks!

manuel.adan’s picture

Thanks for the clarification. So this patch needs #2858877: Allow for block visibility rules from page_manager to work, which, as I said, introduces a new dependency on page_manager. According to the .info file, such dependency currently exists only for tests.

AndyF’s picture

Sorry, in case it was ambiguous I was clarifying that it hasn't been removed. (It was never there in the first place.)

Thanks!

manuel.adan’s picture

I run into the same as #17:

InvalidArgumentException: You must provide the context IDs in the @{service_id}:{unqualified_context_id} format. in Drupal\Core\Plugin\Context\LazyContextRepository->getRuntimeContexts() (line 71 of core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php).
Drupal\page_manager\ConditionAccessResolver->checkAccess(Object) (Line: 246)
Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder->checkVisibility(Object) (Line: 149)
Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder->buildRegions(Array, Array) (Line: 210)
Drupal\panels\Plugin\DisplayBuilder\StandardDisplayBuilder->build(Object) (Line: 331)
...

Patch #2769099-18: Block visibility conditions + #2858877-17: Allow for block visibility rules. Current language as the block visibility condition.

AndyF’s picture

Thanks @manuel.adan - #17 was a cross-post and I updated the patch in #2858877: Allow for block visibility rules as a result of @millionleaves' comment on that thread. Could you try with the latest page manager patch and see if that improves the situation? (If not, if you have time to see if the problem still occurs when using a non-panels page manager page that would be awesome!)

Thanks

manuel.adan’s picture

@AndyF, I forgot to mention that I tried with [#285877-40], patch applies cleanly in page_manager 8.x-4.0-beta3 (not the latest -dev), but I got a new error when trying to edit a block instance in the panel's content:

ArgumentCountError: Too few arguments to function Drupal\page_manager_ui\Form\VisibilitySubform::__construct(), 3 passed in /var/www/html/modules/panels/src/Form/PanelsBlockConfigureFormBase.php on line 92 and exactly 4 expected in Drupal\page_manager_ui\Form\VisibilitySubform->__construct() (line 55 of /var/www/html/modules/page_manager/page_manager_ui/src/Form/VisibilitySubform.php)
#0 /var/www/html/modules/panels/src/Form/PanelsBlockConfigureFormBase.php(92): Drupal\page_manager_ui\Form\VisibilitySubform->__construct(Object(Drupal\rules\Core\ConditionManager), Object(Drupal\language\ConfigurableLanguageManager), Object(Drupal\Core\Plugin\Context\LazyContextRepository))
#1 /var/www/html/modules/panels/src/Form/PanelsBlockConfigureFormBase.php(103): Drupal\panels\Form\PanelsBlockConfigureFormBase->__construct(Object(Drupal\user\SharedTempStoreFactory), Object(Drupal\rules\Core\ConditionManager), Object(Drupal\language\ConfigurableLanguageManager), Object(Drupal\Core\Plugin\Context\LazyContextRepository))
#2 /var/www/html/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php(28): Drupal\panels\Form\PanelsBlockConfigureFormBase::create(Object(Drupal\Core\DependencyInjection\Container))
...

so I went back to [#285877-17] and did not go further on this, solving it with another approach in my current project.

AndyF’s picture

Issue summary: View changes
Issue tags: -Needs issue summary update

Thanks for the feedback @manuel.adan.

I tried with [#285877-40], patch applies cleanly in page_manager 8.x-4.0-beta3 (not the latest -dev)

Hmmn, that's strange, it's applying for me (it's very recent):

$ git clone -b 8.x-4.x https://git.drupal.org/project/page_manager.git && cd page_manager
Cloning into 'page_manager'...
remote: Counting objects: 3996, done.
remote: Compressing objects: 100% (2927/2927), done.
remote: Total 3996 (delta 2747), reused 1227 (delta 879)
Receiving objects: 100% (3996/3996), 576.89 KiB | 225.00 KiB/s, done.
Resolving deltas: 100% (2747/2747), done.
Checking connectivity... done.
$ curl -Ss https://www.drupal.org/files/issues/2018-06-04/page_manager-block_visibility_conditions-2858877-40-D8.patch | patch -p1
patching file config/schema/page_manager.schema.yml
patching file page_manager.services.yml
patching file page_manager_ui/src/Form/ConditionSubform.php
patching file page_manager_ui/src/Form/VariantPluginAddBlockForm.php
patching file page_manager_ui/src/Form/VariantPluginConfigureBlockFormBase.php
patching file page_manager_ui/src/Form/VisibilitySubform.php
patching file page_manager_ui/tests/src/FunctionalJavascript/VisibilitySubformTest.php
patching file page_manager_ui/tests/src/Unit/ConditionsSubformTest.php
patching file page_manager_ui/tests/src/Unit/VisibilitySubformTest.php
patching file src/ConditionAccessResolver.php
patching file src/ConditionAccessResolverInterface.php
patching file src/Plugin/DisplayVariant/PageBlockDisplayVariant.php
patching file tests/src/Unit/ConditionAccessResolverTest.php
patching file tests/src/Unit/PageBlockDisplayVariantTest.php
$ git show -s
commit d99c1507907cda8940accd33fd016ef496e49876
Author: redhead <redhead@1361080.no-reply.drupal.org>
Date:   Fri Apr 13 20:41:41 2018 -0500

    Issue #2624972 by dstorozhuk, kalistos, piggito: No configuration possible in UI for 301, 303, etc. HTTP responses
I got a new error when trying to edit a block instance in the panel's content

Ah thanks! I've been adding tests to the Page Manager patch and it uncovered a dependency that wasn't being injected: the patch on this page will need updating to use that, should be simple to do. I realise it might be a bit late now, but you might have luck with #2858877-27: Allow for block visibility rules which includes the fix to @millionleaves' problem in #17 but is before I started working on the tests.

I'll try to update the patch on this issue soon.

Thanks!

AndyF’s picture

I've introduced a service to decouple the two patches further: this should work with the latest patch in #2858877: Allow for block visibility rules (tested with #2858877-45: Allow for block visibility rules).

Cheers

AndyF’s picture

Here's an updated patch that allows conditions to be set on contexts from the panel, works with #2858877-52: Allow for block visibility rules.

millionleaves’s picture

Thanks @AndyF - I can confirm that the patch in #33 works with #2858877-52: Allow for block visibility rules, and does (almost) what I need to in terms of setting block visibility in Panels. The only thing missing is the option to set multiple Visibility Rules (as in Panels 7) but I'll leave that for another issue ;)

Note that the issue I reported in #23 still exists, but I've found that if you apply this patch first, and then the Custom Attributes patch then it is relatively easy to resolve the conflict manually, as documented here:

https://www.drupal.org/project/panels/issues/2849867#comment-12703972

Hopefully one of these two patches will get committed soon so the other can be re-rolled against it.

Also, it's worth noting that if you install this module as well, you'll also get the option to set visibility based on the value of a node field:

https://www.drupal.org/project/entity_field_condition

You'll also get the option within Page Manager to use a node field as a Variant selection rule.

drupgirl’s picture

Patch #33 works for me. Thank you very much.

I did notice that I could not isolate only authenticated users because I could not negate user role condition for block visibility. I ended up using panels and blocks in regions with this patch to return negating function to D8.

Not sure why this functionality was removed, but it would be good to have here, as well, imo.

manuel.adan’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests
+++ b/panels.info.yml
@@ -9,6 +9,8 @@ dependencies:
+  - page_manager:page_manager
+  - page_manager:page_manager_ui

This introduces a circular dependency on page_manager, which also depends on panels. I didn't go further after this point.

Needs test coverage.

AndyF’s picture

Thanks @manuel.adan

This introduces a circular dependency on page_manager, which also depends on panels.

I don't think page_manager depends on panels. However I'm not convinced adding the dependency is a good idea nonetheless. See the IS for a description of the dependency issue (also point 2 in #2858877: Allow for block visibility rules IS) with some possible solutions.

manuel.adan’s picture

@AndyF, that's true, I was reviewing some ctools issues just before got here and I still had its dependency in mind. Nevertheless, and as you pointed, it would be preferable to avoid dependency on page_manager in this case.

liquidcms’s picture

tried this patch.. of course difficult to tell what it applies to but i grabbed latest -dev and it almost applies cleanly; i manually fixed the part which didn't.

trying to edit a page in panels and i now wsod with:

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "page_manager.condition_access_resolver". in Drupal\Component\DependencyInjection\Container->get() (line 153 of core/lib/Drupal/Component/DependencyInjection/Container.php).

ocastle’s picture

Re-rolling patch and removed the page_manager dependency

Status: Needs review » Needs work

The last submitted patch, 42: panels-block_visibility_conditions-2769099-42.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

ocastle’s picture

Re-rolling patch. Added back in page_manager dependency - misinterpreted the above.

ocastle’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 44: panels-block_visibility_conditions-2769099-43.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

NWOM’s picture

#44 worked great in combination with #2858877: Allow for block visibility rules. Thank you!

Sseto’s picture

Hey guys,

I tried to apply 2 different patches for Panels and I'm getting an error.

Patch 1: [Block visbility conditions](https://www.drupal.org/project/panels/issues/2769099)

Patch 2: [Allow for block visibility rules](https://www.drupal.org/project/page_manager/issues/2858877)

`https://www.drupal.org/files/issues/2020-07-13/panels-block_visibility_c... (Block visibility conditions)`
`https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_... (Custom attributes in panels blocks and variants)`
`Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_...`

I tried swapping them around and still the same error.

The weird part is that Both patches are somehow still applied and working.

*EDIT: The UI to add `style settings` in https://www.drupal.org/files/issues/2020-04-30/panels_custom_attributes_... is showing, but it actually doesn't add the CSS classes.

Sseto’s picture

Hey everyone! I was able to figure it out. I wrote my steps on the other issue https://www.drupal.org/project/panels/issues/2849867#comment-14001266

NWOM’s picture

@Sseto: Here is a patch that already combines both patches in case you want to add it to composer: #3183258: Block visibility conditions and Custom attributes combined patch for composer.

As a summary, the patch in this issue works, however it is incompatible with the Custom attributes patch, until one gets committed and the other patch gets re-rolled. As a workaround, the combined patch that I created can be used.

crutch’s picture

Applied both patches with the latest 4.x-dev for page_manager and panels.

"drupal/page_manager": {"2858877: Allow for block visibility rules": "https://www.drupal.org/files/issues/2020-06-24/page_manager-block_visibility_conditions-2858877-66.patch"},
"drupal/panels": {"2769099: Block visibility conditions": "https://www.drupal.org/files/issues/2020-07-13/panels-block_visibility_conditions-2769099-43.patch"}

Located these items below in conjunction with the patches, it works for conditionally hiding blocks based on term selection in Page variants.

https://www.drupal.org/project/block_visibility_groups
https://www.drupal.org/project/term_condition

Great thank you!

Sseto’s picture

I recently migrated my D7 site to D9 and the site has this error now:

InvalidArgumentException: You must provide the context IDs in the @{service_id}:{unqualified_context_id} format. in Drupal\Core\Plugin\Context\LazyContextRepository->getRuntimeContexts() (line 71 of core\lib\Drupal\Core\Plugin\Context\LazyContextRepository.php).

As soon as I add a new block into a region, I get it...

Any idea what could be the issue?

crutch’s picture

After the latest updates for page manager and panels, and removing these two previous patches from composer because they should be integrated now, then block visibility is gone and unavailable when editing a block in panels. No errors.

https://www.drupal.org/files/issues/2020-06-24/page_manager-block_visibi... (2858877: Allow for block visibility rules)

https://www.drupal.org/files/issues/2020-07-13/panels-block_visibility_c... (2769099: Block visibility conditions)

Sseto’s picture

I'm also having the same issue as crutch after the new updates. Anyone know what's going on?

Update: Got it working. Thanks for the patch NWOM!

crutch’s picture

Hi Sseto, what recipe of module versions and patches did you use to get it working again? For some reason, I'm not able to get the visibility conditions back. Thank you, Crutch

Sseto’s picture

Hey Crutch, You should try and use this patch: https://www.drupal.org/project/panels/issues/3183258

crutch’s picture

Yes, that is the solution, thank you!

lobodakyrylo’s picture

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

lobodakyrylo’s picture

joseph.olstad’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Status: Needs review » Needs work

The last submitted patch, 60: panels-block_visibility_conditions-2769099-60.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joseph.olstad’s picture

The latest patch #60 introduces 9 new test failures.
see
#3383474: Fix HEAD test failures for 4.x

we only have 1 head failure currently, and looking to fix that.

renatog’s picture

renatog’s picture

Aware that #60 needs works according to comment above

On this meantime, for any reason if someone needs to use on 8.x-4.7 tag, this is the patch

P.S. Making as hidden since tests needs to be done development branch