Example Panels style plugin with substyles

Public

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. Source: https://gist.github.com/smoothify/7289790

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  * Definition of the 'bootstrap_style' panels style plugin.
  6.  */
  7.  
  8. $plugin = array(
  9. 'title' => t('Example Parent Style'),
  10. 'description' => t('Example style providing substyles'),
  11. // We offer substyles so provide callbacks to do so.
  12. 'get child' => 'example_style_get_substyle',
  13. 'get children' => 'example_style_get_substyles',
  14. 'settings form' => 'example_style_settings_form',
  15. 'pane settings form' => 'example_style_settings_form',
  16. 'defaults' => array(
  17. 'first_option' => '',
  18. 'second_option' => '',
  19. ),
  20. );
  21.  
  22. /**
  23.  * Provides List of substyles.
  24.  */
  25. function example_style_load_substyles($substyle_name = NULL) {
  26. $substyles = array(
  27. 'first' => array(
  28. 'name' => 'first',
  29. 'title' => 'First Substyle',
  30. 'panes' => TRUE,
  31. 'regions' => TRUE,
  32. ),
  33. 'second' => array(
  34. 'name' => 'second',
  35. 'title' => 'Second Substyle',
  36. 'panes' => TRUE,
  37. 'regions' => FALSE,
  38. ),
  39. );
  40. if ($substyle_name) {
  41. return $substyles[$substyle_name];
  42. }
  43. return $substyles;
  44. }
  45.  
  46. /**
  47.  * Merge the main style plugin with a substyle to create a sub plugin.
  48.  */
  49. function example_style_merge_plugin($plugin, $style_name, $substyle) {
  50. $plugin['name'] = $style_name . ':' . $substyle['name'];
  51. $plugin['title'] = 'Example: ' . check_plain($substyle['title']);
  52. $plugin['substyle'] = $substyle;
  53. if (!empty($substyle['panes'])) {
  54. // Make this substyle show up as a pane style.
  55. $plugin['render pane'] = 'example_style_render_pane';
  56. }
  57. if (!empty($substyle['regions'])) {
  58. // Make this substyle show up as a region style.
  59. $plugin['render region'] = 'example_style_render_region';
  60. }
  61. $plugin['weight'] = 0;
  62. return $plugin;
  63. }
  64.  
  65. /**
  66.  * Callback to provide a single substyle.
  67.  */
  68. function example_style_get_substyle($plugin, $style_name, $substyle_name) {
  69. $substyle = example_style_load_substyles($substyle_name);
  70. return example_style_merge_plugin($plugin, $style_name, $substyle);
  71. }
  72.  
  73. /**
  74.  * Callback to provide all substyles.
  75.  */
  76. function example_style_get_substyles($plugin, $style_name) {
  77. $data = example_style_load_substyles();
  78. foreach ($data as $id => $substyle) {
  79. $substyles[$style_name . ':' . $id] = example_style_merge_plugin($plugin, $style_name, $substyle);
  80. }
  81. return $substyles;
  82. }
  83.  
  84.  
  85. /**
  86.  * Theme fuction for the region styles.
  87.  */
  88. function theme_example_style_render_region(&$variables) {
  89. $output = '';
  90. $settings = $variables['settings'];
  91. $panes = $variables['panes'];
  92. $style = $variables['style'];
  93. $substyle = $style['substyle'];
  94.  
  95. switch ($substyle['name']) {
  96. case 'first':
  97. // Here you could call your own theme function.
  98. $output .= '<div style="border: 2px dotted #aaa; padding: 20px; margin: 20px">';
  99. $output .= '<h4>Region: First Style</h4>';
  100. $output .= '<div>First Option: ' . $settings['first_option'] . '</div>';
  101. $output .= implode(PHP_EOL, $panes);
  102. $output .= '</div>';
  103. break;
  104. }
  105.  
  106. return $output;
  107. }
  108.  
  109. /**
  110.  * Theme function for the pane style.
  111.  */
  112. function theme_example_style_render_pane(&$variables) {
  113. $output = '';
  114. $settings = $variables['settings'];
  115. $content = $variables['content'];
  116. $style = $variables['style'];
  117. $substyle = $style['substyle'];
  118.  
  119. switch ($substyle['name']) {
  120. case 'first':
  121. // Here you could call your own theme function or just send in some
  122. // style settings to the default panels pane.
  123. $output .= '<div style="border: 1px solid #aaa; padding: 20px; margin: 20px">';
  124. $output .= '<h4>First Style</h4>';
  125. $output .= '<div>First Option: ' . $settings['first_option'] . '</div>';
  126. $output .= theme('panels_pane', $variables);
  127. $output .= '</div>';
  128. break;
  129.  
  130. case 'second':
  131. // Here you could call any theme function or just send in some
  132. // style settings to the default panels pane.
  133. $output .= '<div style="border: 1px solid #ccc; padding: 20px; margin: 20px">';
  134. $output .= '<h4>Second Style</h4>';
  135. $output .= '<div>Second Option: ' . $settings['second_option'] . '</div>';
  136. $output .= theme('panels_pane', $variables);
  137. $output .= '</div>';
  138. break;
  139. }
  140.  
  141. return $output;
  142. }
  143.  
  144. /**
  145.  * Options for the Panels style plugin to help style panes.
  146.  */
  147. function example_style_settings_form($settings, $display, $pid, $type, $form_state) {
  148. $style = $form_state['style'];
  149. $substyle = $style['substyle'];
  150. $form = array();
  151.  
  152. if ($substyle['name'] == 'first') {
  153. $form['first_option'] = array(
  154. '#title' => t('Options for First Substyle'),
  155. '#type' => 'select',
  156. '#options' => array(
  157. 'style1' => 'Style 1',
  158. 'style2' => 'Style 2',
  159. ),
  160. '#default_value' => $settings['first_option'],
  161. '#empty_option' => t('Default'),
  162. '#empty_value' => NULL,
  163. );
  164. }
  165. elseif ($substyle['name'] == 'second') {
  166. $form['second_option'] = array(
  167. '#title' => t('Options for Second Substyle'),
  168. '#type' => 'select',
  169. '#options' => array(
  170. 'style1' => 'Style 1',
  171. 'style2' => 'Style 2',
  172. ),
  173. '#default_value' => $settings['second_option'],
  174. '#empty_option' => t('Default'),
  175. '#empty_value' => NULL,
  176. );
  177. }
  178. return $form;
  179. }

Comments