Needs work
Project:
Page Manager
Version:
8.x-4.x-dev
Component:
Code
Priority:
Major
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
8 Mar 2017 at 16:53 UTC
Updated:
5 Mar 2025 at 15:29 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
millionleaves commentedThis is something that would make Page Manager in D8 considerably more useful to me. It's one of the reasons that I love D7 Panels.
Some use cases beyond the one supplied above:
Comment #3
andyf commentedHere's a first pass at that, feedback welcome.
Comment #4
andyf commentedComment #6
andyf commentedHopefully fixed errors with existing tests (but I haven't added new ones). Btw I don't know if it's ok to change the signature for
\Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant::__construct()while it's in beta?Comment #8
andyf commentedSo the tests are fixed on D8.4.x but there's a single error for 8.5.x. Sadly I don't have time right now to look into that.
Comment #9
dawehnerIts super nice to have a piece of well written and even more important, battle tested code here.
Maybe we could add a @todo to move this somewhere else in core, so its actually reusable :)
Let's ensure this also points to the page_manager equivalent
A general tip: If you add new dependencies you can make them optional, as this ensures 100% that none of other people's code is broken:
This is particular useful for any kind of baseclasses.
Some general coding style question:
$visibility = empty($configuration['visibility']) ? [] : $configuration['visibility'];vs.Do you have any particular preference?
Note: This code could use
$this->getVisibility()... block module has its own UX "optimizations", so node_type, user_role etc .... I'm wondering whether this really makes sense in the context of page_manager, where you have ctools installed and as such a LOT of conditions. I am strongly wondering whether the UI should look more like the selection criterias.
Comment #10
andyf commentedThanks for the thoughtful feedback @dawehner!
1.
Yeah, good idea! Done.
2.
This really is the page manager equivalent - I'm referencing where I stole the code from, and ideally what should be refactored to use this if it's moved into core.
3.
Thanks! Done. PhpStorm's Change Signature FTW!
4.
Haha! Yeah, I wasn't overly happy with that little bit. I think your way looks better (:
5.
Doh! Thanks, done.
6.
Yeah, that's a really good point - never thought about it. I've added a
@todo, but for now I think what we have makes some kind of sense (: Also it looks like there's work being done on changing how core works, so it might be worth seeing how that pans out: #2284687: Redesign UI for better management of block visibility.Thanks again!
Comment #12
andyf commentedOk, well, PhpStorm's Change Signature does have some limitations (: I also needed to reorder the parameters passed to the mock builder. Interdiff is against #6.
Comment #14
jlballes commentedIt would make this module much better, same that in Drupal 7.
I will test it as soon as i can.
Thanks
Comment #15
tim.plunkettComment #16
tim.plunkettFailures due to #2904887: Symfony 3 is incompatible with Page Manager
Comment #17
andyf commentedAdd
DependencySerializationTraittoConditionSubformso it can be used in an AJAX form.Comment #18
millionleaves commentedThanks. I've applied #17 to a clean copy of Page Manager 8.x-4.0-beta2 but I'm not sure what I'm supposed to be seeing.
I have one page in Page Manager which uses a path of /node/%.
Within that, I have one variant which is selected if the node type is Service.
Within that, I have two blocks, Body and Webform.
I cannot see any UI that would allow me to set visibility rules on these blocks.
I imagine I'm missing something ...
Comment #19
andyf commented@millionleaves Not sure, sorry, it works for me. When you add/edit a block using page manager you should see the same kind of block visibility tabs you see on the core block form. You're not using a Panels page are you? If so you'll also need the patch in #2769099: Block visibility conditions.
The following simplytest.me link works for me: https://simplytest.me/project/page_manager?patch[]=https://www.drupal.or...
(You'll need to enable page_manager and page_manager_ui first.)
Comment #20
millionleaves commentedAh yes - that was the secret. I'm using Panels, and can now set the visibility of blocks based on node field values.
However, 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 71Removing 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.
Comment #21
andyf commentedThanks @millionleaves.
I'm not entirely sure what kind of condition you've set up. Would you be able to provide an export of the page?
Thank!
Comment #22
millionleaves commentedThanks. See attached. Is this what you're looking for?
Comment #23
andyf commented@millionleaves Perfect, thanks! I'm not familiar with the
node_fieldcondition and I can't seem to find it in my current codebase... Do you know what provides it? What's its name in the UI?Thanks
Comment #24
millionleaves commented@AndyF
In this example, the node_field condition is ensuring that the webform block should only be displayed on a node with NID 5. It's from the Panels block visibility settings patch that you referenced in #19, where you can use node fields to determine the visibility of a block within a Panel. To recreate, you add a block to a (panels) variant. Within that you can choose Node Field to control visibility of the block.
Comment #25
andyf commented@millionleaves Thanks. I hate to say it, but I have no idea where that condition comes from! It's not from the patches themselves (they just expose existing conditions via the UI). It doesn't seem to be part of panels or page_manager or node, I can't get any results searching on its machine name or label, and I can't find any solutions to enabling a block based on NID that don't use the request path condition.
Would you be able to type the following command from the document root of your codebase?
Hopefully that will show what defines the condition.
Thanks!
Comment #26
millionleaves commentedHere it is:
modules/entity_field_condition/src/Plugin/Condition/NodeField.php
https://www.drupal.org/project/entity_field_condition
Comment #27
andyf commentedThanks @millionleaves, that's helped me fix a problem with using conditions with context mappings (which includes the node field) for page manager. I haven't tested it with Panels but fingers crossed it will work there too (please confirm!).
Comment #28
millionleaves commentedThanks!
I've applied the patch to a fresh download of the dev version of Page Manager. Patch applies cleanly, and my site no longer blows up when I visit a page using a panels variant with block visibility conditions.
Unfortunately ....
Panels Pages are loading without the block that has visibility conditions, regardless of whether the conditions are met. I don't get this since another block in the same variant is working fine. May be the block type (the missing block is a Webform block, the other is the Body field).
and ...
I then tried applying the Panels Block Visibility patch to the latest -dev of Panels to see if that helped and found that I could not longer edit either of the blocks at all. I actually ran into this problem the other day and resolved it by reverting to the current stable version of Panels, so I doubt it's related to this latest patch.
So, I need to do some more testing of different conditions to pinpoint whether it's my setup or some combination of the Panels and Page Manager patches causing the problems. I'll try and do that in the next day or so.
Comment #29
berdirIf I see it correctly then this uses the core context repository. But page manager has its own context system, only then contexts for contexts from the url/static contexts will work.
Comment #30
andyf commentedThanks @berdir! Forgive me, I'm not sure what you mean exactly by "url/static contexts". Do you have any suggestions for how I might trigger a failure? Just FYI the test I did before posting was creating a variant at
/node/{nid}and making a block display only on node 1 using the node_field condition from #26.I'm new to contexts by the way, so any and all help appreciated, thanks again!
Comment #31
berdirYes, sorry for the short drive-by comment, didn't have much time.
Yes, node probably works because core provides the context for that. But try the same with a page that has a taxonomy_term argument, either replacing /taxonomy/term/{taxonomy_term} or a custom route. Then configure that path argument to be a taxonomy_term.
You won't be able add a visibility condition now for a specific vocabulary. But if you have a block which requires a taxonomy_term context (for example a view with a term id argument and term validation), then you will be able to select and use the term provided by the path.
I don't have a full overview of how that works these days, but the contexts are stored in tempstore when editing, see \Drupal\page_manager_ui\Controller\PageManagerController::selectBlock() which selects the available blocks and provides the contexts for that and at runtime, you can get the contexts from the page variant, \Drupal\page_manager\Entity\PageVariantViewBuilder::view() sets them on the display variant, where you can access them with $this->getContexts().
Comment #32
andyf commentedPerfect, thanks @berdir!
Comment #33
andyf commentedComment #34
huzookaI'd say https://www.drupal.org/node/2769099 is related to this.
Comment #35
swigle commentedJust wanted to give thanks to ppl above me; This worked for me as well. As a side note, be sure that you have the most updated version of both panels and page_manager modules (not the latest stable version) otherwise the patches won't work.
This in combination with the Entity_field_condition module, gives us the desired functionality, albeit less user friendly.
Thanks for all the work!
Comment #36
andyf commentedI've got some free time: I'm going to work on this a bit more, starting with some tests.
Comment #37
andyf commentedI've added some unit and functional tests, let's see what testbot has to say. Feedback welcome! I haven't looked into @berdir's comment in #31 at all.
Thanks!
Comment #38
andyf commentedThere seems to be a little flakiness in the JS functional tests. I'm thinking it might be because I assumed that when the block title field appeared in the add block modal that the condition vertical tabs had also been set up. I've fixed it so that I wait on the element I'm actually clicking (doh!).
Comment #40
andyf commentedAnd fixed a few stragglers, fingers crossed...
Comment #44
andyf commentedNot sure why, but there's still some flakiness in the JS tests. It's not consistent, but tends to be around waiting for the condition's vertical tab and summary. It frequently occurs in
Drupal\Tests\page_manager_ui\FunctionalJavascript\VisibilitySubformTest::testContentTypeCondition()where it asserts the content type link.Comment #45
andyf commentedJust a minor addition that converts the visibility subform helper into a service for easier injection.
Comment #46
andyf commentedtrigger testbot and summary update
Comment #47
drupov commentedAccess plugins in the panels world in Drupal 7 could be added to a pane/region/page etc. on the fly, so there were no plugins active until you added one. From that point on you could add two access plugins to a pane and give them different values. That's what made the access system in panels superior to the core block visibility in D7.
Additionally these access plugins are applied and available for a pane right away (pane config screen), so there is not way to just say "I don't want this one" - this should be done in code... Also I guess if having lots of plugins on a site, it will be very difficult to track which ones are available and really used.
I am playing around with the patch here and in https://www.drupal.org/project/panels/issues/2769099 and while it seems to work (I am still discovering some issues with a custom condition, but I'll update on that later) the solution seems to be as the one we had in D7.
Over at 2769099 there was a proposal to introduce a panels access plugin, see https://www.drupal.org/project/panels/issues/2769099#comment-12049274 - I am wondering if we should consider building on that?
See this screenshot for hopefully better understanding:
https://ibb.co/fgFq5J
Comment #48
drupov commentedOk, I see there is https://www.drupal.org/project/block_visibility_groups
Hopefully this can be managed better with it
Comment #49
andyf commentedThanks @drupov,
From a backend POV that's what we're doing/simulating, but the UI arguably doesn't match that very well. (
Drupal\Core\Condition\ConditionPluginCollection::getConfiguration()detects whether the condition config is the same as the default config and only saves conditions which are actually configured.)I'm not sure exactly what you're getting at. It's possible to have two conditions on a block, but I guess that's not what you mean?
Thanks, yeah that's a really good point. @dawehner mentioned the UX issues with it in #2858877-9: Allow for block visibility rules point 6 which led to a
@todobeing added and there's a core block issue #2284687: Redesign UI for better management of block visibility. At the moment we're just copying the core UI which I guess makes sense at least to try and get the initial functionality in? When #2284687: Redesign UI for better management of block visibility lands we can refactor it.If you do find any issues, it would be great if you could see if the issue manifests only with panels, or both with page manager and panels, and report back on the appropriate thread (thanks!).
The difference in approach there is much more than the UI. The patch in #2769099-10: Block visibility conditions as I understand it creates a new kind of plugin, which means you can't use existing (including contrib) conditions. I wanted to be able to use the same condition for a variant as for a block.
Just for the UI, or is there something else?
I'm far from an expert here, but as I understand it you're not seeing all conditions. From what I can gather (ie take with a pinch of salt) the reason you see a node condition is because the Node module has a context provider service (
Drupal\node\ContextProvider\NodeRouteContext) which kind of makes the context globally available (even if not always populated with an actual node entity). But there isn't a similar context provider for taxonomy terms (no idea why not), so as @berdir mentioned in #31 to make the vocabulary condition work on an overridden taxonomy term page the patch needs to show conditions for the specific contexts associated with the page as well as the global ones. As I say, I'm still feeling my way around this area a bit!Thanks again!
Comment #50
drupov commentedThanks!
I mean two same access plugins, sorry
Totally agree. It will be best to move forward.
trying :)
Absolutely. We also have the mentioned #2284687: Design a GUI for managing block visibility.
UI, but as said, that will be tackled anyway.
Thanks again for these contributions, really appreciate it!
Comment #51
andyf commentedI'm going to take a look at integrating page manager contexts.
Comment #52
andyf commentedHere's a first attempt at integrating page manager contexts.
As @drupov mentioned we can't add the same condition more than once, so for example if there are two term contexts then we can't have a condition on both of them. (I hadn't originally realised that the UI change would impact functionality.) I'm not sure if this is something to address immediately or in a follow-up issue? Also I could really do with someone who knows something about contexts casting an eye over the patch if at all possible (:
Thanks!
Comment #54
andyf commentedComment #55
drupov commentedWorks for me! If there is a context you can apply it to the condition:
https://ibb.co/fc1LUo
One thing is a little bit messy maybe: for example if this demo context is not there, the condition for it would still be displayed:
https://ibb.co/df1Upo
The best seems to be conditions to be added when needed (kind of what "Block Visibility Group" does: https://ibb.co/jHB7aT), so start with an empty set and add those available through context when needed.
Maybe it's really worth thinking about the whole "we can't add the same condition more than once"-thing to be done here. This was one of the main advantages of the access system for Panels in Drupal 7.
Anyway, feels like real progress here, thanks @AndyF!
Comment #56
andyf commentedThanks for the testing and feedback @drupov!
This boils down to the context providers mentioned at the end of #49. What I've done is include contexts both from the "global" providers (which you get by calling
getAvailableContexts()on thecontext.repositoryservice) and the contexts from the page variant. It's true that if you try to add a condition under a variant's selection criteria it only shows conditions for the page contexts, not the ones fromcontext.repository. Maybe we should do the same? I guess any context that would be provided by the repository can be added explicitly via page manager, so you should still be able to access all the same conditions...It definitely seems a higher priority now it's more than just a UX improvement, it actually limits functionality. Also I guess it would affect the config schema and might involve a hook_update_N() if we were to make the change later. OTOH it was kinda nice to be relying on code stolen from core, we might be duplicating to some extent work done in #2284687: Redesign UI for better management of block visibility and there's always the risk this issue will linger! That said, I'm not sure how much more work it would be: we can maybe (mostly) copy the UI for page variant selection criteria.
Comment #57
drupov commentedYou're right and also that part (Add selection criteria functionality for panes) would fit better as a Panels issue?
I think with the work you've done here we've made good progress into solving this issue, so maybe it's really better to try to make it RTBC and handle the rest separately.
Comment #58
jweirather commentedHi all, thanks so much for your work on this feature!
We've applied the patch in #55, and it appears to have applied cleanly, but we're not seeing any of the UI elements shown in the screenshots, which we would use to apply selection rules/conditions.
Are there any other steps needed to test the patch, other than applying it?
Comment #59
socialnicheguru commentedIt no longer applies to the latest dev (april 2019)
Comment #60
manuel.adanPatch ported with no additional review except some CS. Interdiff fails. After update, a cache rebuild seems to be required.
Comment #62
weseze commentedI tried the patch from #60. It applied cleanly against "8.x-4.0-beta4, released 29 May 2019". I cleared the cache after applying the patch.
Nothing changed... There were no visibility rules available.
Comment #63
lamp5Which kind of page manager variant, you are using? Blocks?
Comment #64
weseze commentedFrom the config file: "variant: panels_variant"
Using the standard page from total_control dashboard module. I expected the blocks in the content to have visibility rules.
Comment #65
swigle commentedSorry, can't confirm what #62 is saying actually, but at first i could (edited this post).
I first updated all my modules, then applied patch @ #60, but forgot for this to work, i also have to patch the panels module.
Seems to work after i did that.
So for anyone who comes across this, try that :)
Comment #66
manuel.adanRe-roll of #60, no additional changes. Interdiff fails due to changes in base code.
Comment #68
nick_vhWhen trying this patch I got the following so I think this patch needs a bit more work. Also there is a test failure as seen above.
Comment #69
nick_vhSomehow this was a duplicateComment #70
nwom commented#66 applied without problems and works perfectly after applying the patch here as well: #2769099: Block visibility conditions. Thank you!
Comment #71
nwom commentedThe patch on #66 needed a re-roll:
Comment #72
crutch commentedcc - https://www.drupal.org/project/panels/issues/2769099#comment-14792705
Comment #73
sseto commentedGot working. Thanks for the patch NWOM!
Comment #74
crutch commentedI have applied below but conditions are still not available as before, reference #72.
Panels: Custom attributes in panels blocks and variants
https://www.drupal.org/project/panels/issues/2849867#comment-14791140
Page Manager: Allow for block visibility rules
https://www.drupal.org/project/page_manager/issues/2858877#comment-14787260
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
Comment #75
kalaiyarasithangavel commentedHi NWOM,
Patch( page_manager-block_visibility_conditions-2858877-71.patch ) not working for drupal 9.5.9 and page manager 8.x-4.0-rc2.
Comment #76
kalaiyarasithangavel commentedHi,
I have created and added the patch. that is working for drupal 9.5.9 and page manager 8.x-4.0-rc2.
Kindly review it.
Comment #77
kalaiyarasithangavel commentedComment #78
nwom commented#76 works on 9.5.11 as well! Thanks for the updated patch
Comment #81
japerryComment #82
socialnicheguru commentedCan https://www.drupal.org/project/context_stack be used with in page_manager to help determine the context?
Comment #83
ludo.rIs there any reason why the
ConditionAccessResolverhas been removed from available services?It can be useful for other modules, in my case here: #2769099: Block visibility conditions
Here's a patch that brings the resolver back as a service, it must be used on top of the issue's merge request.
I'd be happy to include it in the MR.