Skip to content

Instantly share code, notes, and snippets.

@LarsEliasNielsen
Last active December 26, 2015 07:59
Show Gist options
  • Save LarsEliasNielsen/7119321 to your computer and use it in GitHub Desktop.
Save LarsEliasNielsen/7119321 to your computer and use it in GitHub Desktop.
Ctools Content Type for Panels (pane). Adding stylesheets and scripts in pane. Variables can be set in pane, and extended to scripts. Markup is stored in a standard template file, with parsed variables.
<?php
/**
* @file
* Markup template.
* This is placed in: my_module/plugins/panes/resources/
*/
?>
<div>
<div class="my_output"></div>
</div>
/**
* @file
* Custom jQuery script
* This is placed in: my_module/plugins/panes/resources/js/
*/
(function($, settings){
console.log(settings);
$('.my_output').append('<p>Resource base: ' + settings.my_pane.resource_base + '</p>');
$('.my_output').append('<p>Module path: ' + settings.my_pane.module_path + '</p>');
})(jQuery, Drupal.settings);
name = My Module
description = Ctools Content Type Example
core = 7.x
package = Panes
dependencies[] = ctools
<?php
/**
* @file
* Custom module to create custom Panel panes.
*/
/**
* Implements hook_ctools_plugin_directory().
* Looks for ctools content type (pane).
*/
function t2_juleaktivitet_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/panes';
}
}
<?php
/**
* @file
* Panels pane.
* This is placed in: my_module/plugins/panes/
*/
/**
* Pane settings.
*/
$plugin = array(
'single' => TRUE,
'title' => t('My Pane'),
'description' => t('This is my pane.'),
'category' => t('Custom panes'),
'admin info' => 'my_pane_admin_info',
'edit form' => 'my_pane_edit_form',
'render callback' => 'my_pane_render_pane',
'no title override' => TRUE,
'defaults' => array(
'my_variable_1' => NULL,
'my_variable_2' => NULL,
),
);
/**
* Callback: Pane info.
*/
function my_pane_admin_info($subtype, $conf, $contexts) {
if (!empty($conf)) {
$block = new stdClass;
$block->title = 'My pane.';
$block->content = t('<p>This is some info on the pane.</p>
<ul>
<li><u>My variable 1:</u> <em>@my_variable_1</em></li>
<li><u>My variable 2:</u> <em>@my_variable_2</em></li>
</ul>', array(
'@my_variable_1' => $conf['my_variable_1'] ? $conf['my_variable_1'] : 'Variable 1 is not defined.',
'@my_variable_2' => $conf['my_variable_2'] ? $conf['my_variable_2'] : 'Variable 2 is not defined.',
));
return $block;
}
}
/**
* Callback: Render pane.
*/
function my_pane_render_pane($subtype, $conf, $args, $context){
$resource_url = str_replace(DRUPAL_ROOT . DIRECTORY_SEPARATOR, '', dirname(__FILE__)) . '/resources';
_my_pane_include_style($resource_url);
_my_pane_include_javascript($resource_url, $conf);
$markup = file_get_contents(DRUPAL_ROOT . DIRECTORY_SEPARATOR . $resource_url . DIRECTORY_SEPARATOR . 'markup.tpl.php');
$content = ctools_context_keyword_substitute($markup, array('%resource_base' => $resource_url), $context);
$block = new stdClass();
$block->content = $markup;
return $block;
}
/**
* Callback: Edit form.
*/
function my_pane_edit_form($form, &$form_state) {
$conf = $form_state['conf'];
$form['my_variable_1'] = array(
'#title' => t('Variable 1'),
'#description' => t('Here you can define variable 1.'),
'#type' => 'textfield',
'#default_value' => $conf['my_variable_1'],
);
$form['my_variable_2'] = array(
'#title' => t('Variable 2'),
'#description' => t('And here you can define variable 2.'),
'#type' => 'password',
'#default_value' => $conf['my_variable_2'],
);
return $form;
}
/**
* Callback: Submit form.
* Stores data in conf.
*/
function my_pane_edit_form_submit($form, &$form_state) {
foreach (array_keys($form_state['plugin']['defaults']) as $key) {
if (isset($form_state['values'][$key])) {
$form_state['conf'][$key] = $form_state['values'][$key];
}
}
}
/**
* Include stylesheets.
*/
function _my_pane_include_style($resource_url) {
drupal_add_css($resource_url . '/css/main.style.css', array(
'group' => CSS_DEFAULT,
));
drupal_add_css($resource_url . '/css/ie.style.css', array(
'group' => CSS_DEFAULT,
'preprocess' => FALSE,
'browsers' => array('IE' => 'lte IE 8', '!IE' => FALSE),
));
}
/**
* Includes Javascript / jQuery.
*/
function _my_pane_include_javascript($resource_url, $conf){
drupal_add_js(array(
'my_pane' => array(
'module_path' => drupal_get_path('module', 'my_module'),
'resource_base' => $resource_url,
),
), array('type' => 'setting'));
drupal_add_js($resource_url . '/js/my-script.jquery.min.js', array(
'scope' => 'footer',
'group' => JS_LIBRARY,
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment