Creating your own contexts

Every so often you find yourself wanting data passed around your panels which you can't easily do out of the box. We had one example with Calendars (another fantastic module), where we wanted to pass dates around in a specific format. To do that we were able to create our own context for 'date'. You will need to write a module to do this and tell panels you have a context for it.

You need to:-

To implement hook_ctools_plugin_directory

function MYMODULE_ctools_plugin_directory($module, $plugin) {
if (!empty($plugin)) {
return "ctools_plugin/$plugin";
}

}

This says that we have stuff inside the directory ctools_plugin/contexts inside our own module. $plugin is the type of plugin being searched for, which is 'contexts' in this case. You don't have to do it exactly this way, but this is how Panels examples does it.

We then create our directory structure and put a file 'datecontext.inc' in there, which represents the 'datecontext' we want to build.

datecontext.inc needs this following:-

Our plugin array looks like this

$plugin = array(
'title' => t("Date context"),
'description' => t('A single date which is derived from PHP.'),
'context' => 'olamalu_rota_context_create_datecontext', // creates context
'context name' => 'datecontext',
'settings form' => 'datecontext_settings_form',
'keyword' => 'datecontext',
// Provides a list of items which are exposed as keywords.
'convert list' => 'datecontext_convert_list',
// Convert keywords into data.
'convert' => 'datecontext_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter data understood by create_date'),
),

);

The title & description are self-explanatory. The rest is

Code to create the context:-

function olamalu_rota_context_create_datecontext($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('datecontext');
$context->plugin = 'datecontext';
if ($empty) {
return $context;
}
$context->data = new stdClass();
if ($conf) {
$context->data->date = check_plain($data['date_setting']);
} else {
$context->data->date = 'now';
}
return $context;

}

Here we create an object 'ctools_context' and then depending on the settings form (which takes an argument about the date) we create a date string inside $context->data. This string is used by the PHP create_date function.

Code to create the settings form:-

function datecontext_settings_form($conf, $external = FALSE) {
if (empty($conf)) {
$conf = array(
'date_setting' => 'now',
);
}
$form = array();
$form['date_setting'] = array(
'#type' => 'textfield',
'#title' => t('Setting for Date'),
'#size' => 50,
'#description' => t('A setting understood by create_date'),
'#default_value' => $conf['date_setting'],
'#prefix' => '<div class="clear-block no-float">',
'#suffix' => '</div>',
);
return $form;

}

Just like any Drupal form we allow the user to enter the string understood by PHP create_date – or if none we just take 'now'.

Code to create the sub-keyword list:-

function datecontext_convert_list() {
return array(
'day' => t('Day as Y-m-d'),
'month' => t('Month'),
'year' => t('Year'),
);

}

Here we list out the options for converting date to day, month or year values. This is simply a list given to the person creating the panel to choose from when they are working with our context.

Code to create the output of the conversion list:-

function datecontext_convert($context, $type) {
switch ($type) {
case 'day':
return date_format(date_create($context->data->date), 'Y-m-d');
case 'month':
return date_format(date_create($context->data->date), 'Y-m');
case 'year':
return date_format(date_create($context->data->date), 'Y');
}

}

And here based on the selections above, the value is converted. Note the switch is on the same values as in the options above.

And voila! This context is now available in our panels. (Remember to flush the cache).

And when you click edit or add, you'll see the settings form we created

These examples were figured out from the Ctools Plugin Examples module inside the core Ctools package.