I will try to explain how to create a custom “visibility rule” which in ctools is called an access plugin for use for ex. panels). The main reason for this, is that i have seen several times that the quick way to create a custom visibility rule in panels, is to choose the “PHP Code” rule, and enter som custom php code, and that’s in my opinion the worst way ever to handle access rules.
So in this guide i will show how easy it is to create your own custom access rule. Ex. you have an panel pane which only should be visible if a expression is TRUE, that could be if the node visited is of the type of ‘Page’, the authenticated user visiting the page have role ‘normal site user’ or a lot of other rules.
A case for me was to create an access rule, so a panel pane only is visible if a custom field on a node have a specified value.
In this example i need a node type with a field named ‘field_age_value’, and will be able to add a field to a panel pane, and create a visibility rule for this pane, so it’s only visible if the age on the node is 40.
First of all you need to tell your custom module that we’re working with ctools plugins, that’s done by implementing hook_ctools_plugin_directory() in your .module file, and telling ctools where to look for your plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13
This tell’s ctools to look in plugins/ folder after custom plugins. In this case it’s looking in plugins/access for an .inc file. Create the access plugin subdirectory.
$ mkdir -p plugins/access
In this directory (plugins/access) create the file field_age_value.inc, which contains the plugin, which will handle the access to the panel pane.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
When this is done and the module is enabled in your installation, you should clear the cache to be sure that ctools will find the plugin.
$ drush cc all
You will now be able to find your custom visibility rule in the panels UI, by pressing the settings icon for a given field in ex. a node view panel.
And select the “Node: field_age_value” visibility rule, and press “next”.
And finally set the age to 40 in the text field.
When the panel pane is updated and saved, when you visit the node you just configured, the pane you have set up the visibility rule for, will only be shown if the field_age_value field on the shown node is 40.
Thats it for this friday night.