Skip to content

Instantly share code, notes, and snippets.

@benosman
Last active December 27, 2015 07:29
Show Gist options
  • Save benosman/7289790 to your computer and use it in GitHub Desktop.
Save benosman/7289790 to your computer and use it in GitHub Desktop.
Example Panels style plugin with substyles. You add this to your module or theme in the normal way for ctools / panels plugins. FIlename should end in .inc.
<?php
/**
* @file
* Definition of the 'bootstrap_style' panels style plugin.
*/
$plugin = array(
'title' => t('Example Parent Style'),
'description' => t('Example style providing substyles'),
// We offer substyles so provide callbacks to do so.
'get child' => 'example_style_get_substyle',
'get children' => 'example_style_get_substyles',
'settings form' => 'example_style_settings_form',
'pane settings form' => 'example_style_settings_form',
'defaults' => array(
'first_option' => '',
'second_option' => '',
),
);
/**
* Provides List of substyles.
*/
function example_style_load_substyles($substyle_name = NULL) {
$substyles = array(
'first' => array(
'name' => 'first',
'title' => 'First Substyle',
'panes' => TRUE,
'regions' => TRUE,
),
'second' => array(
'name' => 'second',
'title' => 'Second Substyle',
'panes' => TRUE,
'regions' => FALSE,
),
);
if ($substyle_name) {
return $substyles[$substyle_name];
}
return $substyles;
}
/**
* Merge the main style plugin with a substyle to create a sub plugin.
*/
function example_style_merge_plugin($plugin, $style_name, $substyle) {
$plugin['name'] = $style_name . ':' . $substyle['name'];
$plugin['title'] = 'Example: ' . check_plain($substyle['title']);
$plugin['substyle'] = $substyle;
if (!empty($substyle['panes'])) {
// Make this substyle show up as a pane style.
$plugin['render pane'] = 'example_style_render_pane';
}
if (!empty($substyle['regions'])) {
// Make this substyle show up as a region style.
$plugin['render region'] = 'example_style_render_region';
}
$plugin['weight'] = 0;
return $plugin;
}
/**
* Callback to provide a single substyle.
*/
function example_style_get_substyle($plugin, $style_name, $substyle_name) {
$substyle = example_style_load_substyles($substyle_name);
return example_style_merge_plugin($plugin, $style_name, $substyle);
}
/**
* Callback to provide all substyles.
*/
function example_style_get_substyles($plugin, $style_name) {
$data = example_style_load_substyles();
foreach ($data as $id => $substyle) {
$substyles[$style_name . ':' . $id] = example_style_merge_plugin($plugin, $style_name, $substyle);
}
return $substyles;
}
/**
* Theme fuction for the region styles.
*/
function theme_example_style_render_region(&$variables) {
$output = '';
$settings = $variables['settings'];
$panes = $variables['panes'];
$style = $variables['style'];
$substyle = $style['substyle'];
switch ($substyle['name']) {
case 'first':
// Here you could call your own theme function.
$output .= '<div style="border: 2px dotted #aaa; padding: 20px; margin: 20px">';
$output .= '<h4>Region: First Style</h4>';
$output .= '<div>First Option: ' . $settings['first_option'] . '</div>';
$output .= implode(PHP_EOL, $panes);
$output .= '</div>';
break;
}
return $output;
}
/**
* Theme function for the pane style.
*/
function theme_example_style_render_pane(&$variables) {
$output = '';
$settings = $variables['settings'];
$content = $variables['content'];
$style = $variables['style'];
$substyle = $style['substyle'];
switch ($substyle['name']) {
case 'first':
// Here you could call your own theme function or just send in some
// style settings to the default panels pane.
$output .= '<div style="border: 1px solid #aaa; padding: 20px; margin: 20px">';
$output .= '<h4>First Style</h4>';
$output .= '<div>First Option: ' . $settings['first_option'] . '</div>';
$output .= theme('panels_pane', $variables);
$output .= '</div>';
break;
case 'second':
// Here you could call any theme function or just send in some
// style settings to the default panels pane.
$output .= '<div style="border: 1px solid #ccc; padding: 20px; margin: 20px">';
$output .= '<h4>Second Style</h4>';
$output .= '<div>Second Option: ' . $settings['second_option'] . '</div>';
$output .= theme('panels_pane', $variables);
$output .= '</div>';
break;
}
return $output;
}
/**
* Options for the Panels style plugin to help style panes.
*/
function example_style_settings_form($settings, $display, $pid, $type, $form_state) {
$style = $form_state['style'];
$substyle = $style['substyle'];
$form = array();
if ($substyle['name'] == 'first') {
$form['first_option'] = array(
'#title' => t('Options for First Substyle'),
'#type' => 'select',
'#options' => array(
'style1' => 'Style 1',
'style2' => 'Style 2',
),
'#default_value' => $settings['first_option'],
'#empty_option' => t('Default'),
'#empty_value' => NULL,
);
}
elseif ($substyle['name'] == 'second') {
$form['second_option'] = array(
'#title' => t('Options for Second Substyle'),
'#type' => 'select',
'#options' => array(
'style1' => 'Style 1',
'style2' => 'Style 2',
),
'#default_value' => $settings['second_option'],
'#empty_option' => t('Default'),
'#empty_value' => NULL,
);
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment