You are here

24 March 2014
This shows code examples of what I had to do to create custom pane programmatically for Panels in Drupal 7. In this example I create new pane where you can pick your node and display it.

HOOK.info

I added panels dependency to my .info file.
Source code viewer
  1. dependencies[] = panels
Programming Language: YAML

HOOK.module plugin directory

Add ctools plugin directory hook. Also create a folder called "plugins" to your module file. In plugins directory create a folder called "content_types".
Source code viewer
  1. /**
  2.  * Implements hook_ctools_plugin_directory().
  3.  */
  4. function HOOK_ctools_plugin_directory($module, $plugin) {
  5. if (($module == 'ctools' || $module == 'panels') && !empty($plugin)) {
  6. return 'plugins/' . $plugin;
  7. }
  8. }
Programming Language: PHP

PANE.inc plugin file

Create a file that will be your plugin in plugins/content_types directory.
Source code viewer
  1. $plugin = array(
  2. 'title' => t('This is my custom pane.'),
  3. 'single' => FALSE,
  4. 'description' => t('My custom pane lets pick a node and display it.'),
  5. 'category' => array('My category'),
  6. 'all contexts' => TRUE,
  7. );
  8.  
  9. /**
  10.  * Title callback for admin page.
  11.  */
  12. function HOOK_PANE_admin_title($subtype, $conf, $context = NULL) {
  13. return t('My custom pane');
  14. }
  15.  
  16. /**
  17.  * Callback to provide administrative info (the preview in panels when building a panel).
  18.  */
  19. function HOOK_PANE_admin_info($subtype, $conf, $context = NULL) {
  20. $block = new stdClass();
  21. $block->title = t('Custom pane');
  22. $config = array();
  23. if ($conf['override_title'] == TRUE) {
  24. $title_value = '<b>' . $conf['override_title_text'] . '</b>';
  25. }
  26. else {
  27. $title_value = t('Not Set');
  28. }
  29. $config[] = t('Title') . ': ' . $title_value;
  30. $block->content = theme_item_list(array( 'items' => $config, 'title' => NULL, 'type' => 'ul', 'attributes' => array() ));
  31. return $block;
  32. }
  33.  
  34. /**
  35.  * Edit callback for the content type.
  36.  */
  37. function HOOK_PANE_content_type_edit_form($form, &$form_state) {
  38. $conf = $form_state['conf'];
  39.  
  40. if ($form_state['op'] == 'add') {
  41. $form['nid'] = array(
  42. '#prefix' => '<div class="no-float">',
  43. '#title' => t('Enter the title or NID of a node'),
  44. '#description' => t('To use a NID from the URL, you may use %0, %1, ..., %N to get URL arguments. Or use @0, @1, @2, ..., @N to use arguments passed into the panel.'),
  45. '#type' => 'textfield',
  46. '#maxlength' => 512,
  47. '#autocomplete_path' => 'ctools/autocomplete/node',
  48. '#weight' => -10,
  49. '#suffix' => '</div>',
  50. );
  51. }
  52. else {
  53. $form['nid'] = array(
  54. '#type' => 'value',
  55. '#value' => $conf['nid'],
  56. );
  57. }
  58.  
  59. return $form;
  60. }
  61.  
  62. /**
  63.  * Submit callback for settings form.
  64.  */
  65. function HOOK_PANE_content_type_edit_form_submit($form, &$form_state) {
  66. foreach (element_children($form) as $key) {
  67. if (isset($form_state['values'][$key])) {
  68. $form_state['conf'][$key] = $form_state['values'][$key];
  69. }
  70. }
  71. }
  72.  
  73. /**
  74.  * Run-time rendering of the body of the block (content type).
  75.  */
  76. function HOOK_PANE_content_type_render($subtype, $conf, $panel_args) {
  77. $block = new stdClass();
  78.  
  79. if (isset($conf['nid'])) {
  80.  
  81. $block = new stdClass();
  82. if ( $conf['override_title'] == TRUE ) {
  83. $block->title = $conf['override_title_text'];
  84. }
  85. else {
  86. $block->title = NULL;
  87. }
  88.  
  89. $nid_match = array();
  90. preg_match('/\[id: (\d*?)\]$/', $conf['nid'], $nid_match);
  91. $node = node_load($nid_match[1]);
  92. $block->content = node_view($node, 'teaser');
  93.  
  94. return $block;
  95. }
  96. return NULL;
  97. }
Programming Language: PHP