Drupal 7 Panels Module Visibility Rules
Share with Others
In this episode we continue learning about the Drupal 7 Panels module and go into how visibility rules can be used to control the display of individual Panels items within your Drupal panels content.
In this episode you will learn:
- What Drupal 7 Panels module visibility rules are
- How Panels visibility rules differ from Panels selection rules
- How to use Drupal Panels visibility rules to hide and show content based on URL path
- How to use Panels visibility rules to hide and show content based on user role or user permission
Hello everyone and welcome to another Daily Dose of Drupal, today we’re on Episode Number 134 and we’re going to continue on with the Drupal 7 Panels Module videos and we’re going to go over visibility rules and this one should actually be pretty short because we learned most of what we need to know in the Selection Rule video in Episode 133 and this is just a little bit of an extension onto that and changes up how you can use instead of Selection Rules, Visibility Rules to control things that are a little bit of a finer level within the Panel page itself.
Before we get started; as always I am Shane Thomas, you can follow me on Twitter at smthomas3, you can find me on Google + and you can also go to codekarate.com, check out the newsletter and sign up if you haven’t already, also click here to learn a little bit about how you can help support code karate and make sure to check out the other videos and blog posts.
Let’s go ahead and get started; make sure you have probably followed along with the other 4 Panels videos, you’ll of course need the Panels Module which you can find on drupal.org. Let’s go ahead and get started; so we have our Panel page that we were on last time and if you remember we had two different variants that we were displaying based on the different selection rules.
We ended up displaying one variant if the user was not authenticated an authenticated user and we displayed the other variant for anyone else. So what we’re going to do is I’m going to go ahead and remove this string URL path selection rule just to make sure that we can use any path to get to this Panel page and we’re going to go over how we can use Visibility Rules and Visibility Rules is very similar to Selection Rules except for instead of controlling the access to the entire variant you can actually control things on an individual item level within your Panel layout.
So what this means is let’s say for instance and keep in mind that this has many different users and so I’m just showing an example of how it can be used but you’ll probably see many uses for this on different websites that you’ve worked on. So when you click on this little gear icon on the top right corner you see there’s a Visibility Rule section. What this Visibility Rule section does is very similar to the selection rules but instead of controlling the entire variant we’re just controlling this one item.
So we can look for a specific context, we can look for the URL path, a user role or a user permission. So I’m going to go through basically the same sequence I went through in the last one and you can see how this works. You can of course experiment with this other selection rule options and we’ll go over context in a different episode, you can also use specific PHP code which can also be helpful just always be careful when you’re entering in PHP code into the Drupal Admin User Interface. In this case we’re going to select String URL Path and I’m going to say if it’s panel-page/test we’re going to allow access so I’m going to click Save and I will update and save this.
Now if I come back here you’ll notice that there’s nothing showing up in this top right section anymore. That’s because it’s only going to be visible on this specific page. If I go to panel-page/test in the URL you’ll notice that now it shows up. If I go back to just panel-page you’ll notice that that section is missing. We can also do the same thing instead of with a current path or a specific path we can add a rule for a User Role or a User Permission.
If we set … this is only available for authenticated users, we’ll save that, I’m also going to go ahead and remove this first one. You can of course have multiple so you can have multiple select or Visibility Rules just as you can have multiple selection rules so I’m going to save that. I’m also going to come back in here before I get too far, I’m going to remove this selection rule that only allows authenticated users to see this page. So this means anonymous users will be able to see everything except for this section right here.
So I’ll go ahead and update and save this and I’ll refresh the page and as you notice here you can of course see this section. However; if I logout and come to this Panel page I can see everything but this section right here and what this is useful for is of course it you want to have one Panel page maybe it’s a dashboard of some sort or some type of information on your website but you want users who are logged in or who have a specific role to be able to see a different section or additional information that you do not want anonymous users to see.
So as you can see Visibility Rules can be very helpful with fine tuning the Permissions or the Visibility of specific items within your Panel page. Just always be worry when you’re using Panels that every visibility rules, every Selection Rule you add is extra processing. Normally it’s not a big deal but the more items you add to your Panel page is the more Visibility Rules. Overtime it can make your page a little bit of a heavy loader or a heavy loading page and can take a little while so just keep that in mind.
You can of course counteract a lot of this stuff with caching for anonymous users and things like that but always be worry of everything that you’re adding and make sure it’s actually necessary.
That’s it for this time on the Daily Dose of Drupal. Next time we’re going to go over Panels Context and learn a little bit about how those can be used using Panels and on your Drupal website. So that’s it for this time and we’ll see you next time, thanks for watching.