Entity view (Content)

Setup Custom Selection Rules for Panels Using Ctools Plugins

By tim
Feb. 23, 2015

Views and Panels are some of the most used modules in Drupal and were both originally written by Earl Miles (aka merlinofchaos), on the back of these Chaos tool suite (ctools) was born. Ctools is described as set of APIs and tools to improve the developer experience. It also features page manager, which is used within panels to mange the pages. Ctools has the ability to make use of plugins, you can write these plugins as part of your Drupal modules. In this blog post we'll look at creating a Ctools plugin to generate custom selection rules. Assuming you already have the basics of a custom module, folder, info file and module file, we'll start by adding hook_ctools_plugin_directory to the module file. In the above code we have the start of the appno.module file for our appno module. The appno_ctools_plugin_directory function allows us to define the directory where all ctools plugins for this module reside. This function could simply return a string of the directory name, which would include all plugins all of the time. This would not be a good idea as we only need the plugins for the current need. In our case we are looking at an 'access' plugin, which are the ones that handle selection rules, and these are owned by ctools. Therefore a simple if statement is used to state this delineation, and the directory path is returned appended with the plugin type. We can now go ahead and create the 'plugins/access' directory within our module folder. In here any number of include files can be created for each access plugin. For this example we'll create an access plugin to determine if it's the morning. Add a file morning.inc with the following code: t("Appno: morning"), 'description' => t('Control access by time of day'), 'callback' => 'appno_ctools_access_check', 'summary' => 'appno_ctools_access_summary', ); This section defines the plugin, like most things in Drupal this is done as an array. The title and description are fairly self-explanatory elements, but are wrapped in the t() function so they are translatable. The callback defines the function that will actually do the access check, the summary is then just another form of description, however it knows all of the configuration and context from the plugin so can be conditional if needed. function appno_ctools_access_check($conf, $context) { if (date('H', REQUEST_TIME) < 12) { return TRUE; } else { return FALSE; } } The above code is the callback, this needs to return true if the selection rule is met, and false not. We are looking to determine if it's the morning or not, for this we will simply use the constant REQUEST_TIME, which is set by Drupal during the bootstrap. The date function allows us to just extract the hour from this so a check can be done to see if it's less than 12. function appno_ctools_access_summary($conf, $context) { return t("Returns true if it's the morning"); } The summary function simply returns a translated string giving a summary of the plugin. The $conf and $context variable could be used here to return a summary based on other configuration, but not needed in this case. In summary, this is a very basic example of what can be done with ctools plugins. However, there is a lot more power even just within the realms of the access plugins, such as setting up configuration options and using contexts based on entities such as nodes and taxonomies.

Post Tags: