theming_example.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * @file
  4. * Explains how a module declares theme functions, preprocess functions, and
  5. * templates.
  6. *
  7. * The underlying approach is that a module should allow themes to do all
  8. * rendering, but provide default implementations where appropriate.
  9. *
  10. * Modules are also expected to leave data as render arrays as long as possible,
  11. * leaving rendering to theme functions and templates.
  12. */
  13. /**
  14. * @defgroup theming_example Example: Theming
  15. * @ingroup examples
  16. * @{
  17. * Example of Drupal theming.
  18. *
  19. * The theming_example module attempts to show how module developers can add
  20. * theme functions to their projects so that themes can modify output.
  21. *
  22. * Module developers should to strive to avoid hard-wiring any HTML into the
  23. * output of their code, this should all be done in theme functions.
  24. *
  25. * Starting with the first example, function 'theming_example_page()':
  26. * the output is put into an array $content which is then fed to a theming
  27. * function 'theme_theming_example_content_array()' which loops over the
  28. * content, wrapping it in html in the process.
  29. *
  30. * In order to get function 'theme_theming_example_content_array()' recognized
  31. * it needs to be registered with the module theme register function of the type
  32. * 'hook_theme'.
  33. *
  34. * function 'theming_example_theme()' does this for this module.
  35. * for details of what can be done in this hook see the link to api.drupal.org
  36. *
  37. * The functions 'theming_example_list_page()' and theming_example_order_form()
  38. * work in the same way.
  39. *
  40. * In 'theme_theming_example_list_page()' the content is themed as an
  41. * ordered list and given a class attribute 'theming_example_mylist' which
  42. * is defined in theming_example.css
  43. *
  44. * In function 'theme_theming_example_order_form()' the title is loaded into a
  45. * temporary variable '$title', deleted from the $form array and output
  46. * wrapped in html. The rest of the form is wrapped in a div using '#prefix'
  47. * and '#suffix'
  48. *
  49. * The theming functions can be copied to a theme's template.php, renaming
  50. * appropriately, so if your theme is called 'mytheme' you would copy function
  51. * 'theme_theming_example_content_array()' to function
  52. * 'mytheme_theming_example_page()' in template.php and it will be used instead
  53. * of the original.
  54. *
  55. * The fourth example shows the use of a template file
  56. * 'theming_example_text_form.tpl.php'
  57. * This file can be copied to a theme's folder and it will be used instead.
  58. *
  59. * This example also shows what can be done using Drupal's
  60. * template_preprocess_HOOK method. In this case it modifies the output so
  61. * that a themer can output the whole form or gain control over some of its
  62. * parts in the template file.
  63. */
  64. /**
  65. * Implements hook_menu().
  66. *
  67. * The @link menu_example.module Menu Example @endlink provides extensive
  68. * examples for hook_menu().
  69. */
  70. function theming_example_menu() {
  71. $items['examples/theming_example'] = array(
  72. 'title' => 'Theming Example',
  73. 'description' => 'Some theming examples.',
  74. 'page callback' => 'theming_example_page',
  75. 'access callback' => TRUE,
  76. 'access arguments' => array('access content'),
  77. );
  78. $items['examples/theming_example/theming_example_list_page'] = array(
  79. 'title' => 'Theming a list',
  80. 'page callback' => 'theming_example_list_page',
  81. 'access arguments' => array('access content'),
  82. 'weight' => 1,
  83. );
  84. $items['examples/theming_example/theming_example_select_form'] = array(
  85. 'title' => 'Theming a form (select form)',
  86. 'page callback' => 'drupal_get_form',
  87. 'page arguments' => array('theming_example_select_form'),
  88. 'access arguments' => array('access content'),
  89. 'weight' => 2,
  90. );
  91. $items['examples/theming_example/theming_example_text_form'] = array(
  92. 'title' => 'Theming a form (text form)',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('theming_example_text_form'),
  95. 'access arguments' => array('access content'),
  96. 'weight' => 3,
  97. );
  98. return $items;
  99. }
  100. /**
  101. * Implements hook_theme().
  102. *
  103. * Defines the theming capabilities provided by this module.
  104. */
  105. function theming_example_theme($existing, $type, $theme, $path) {
  106. return array(
  107. 'theming_example_content_array' => array(
  108. // We use 'render element' when the item to be passed is a self-describing
  109. // render array (it will have #theme_wrappers)
  110. 'render element' => 'element',
  111. ),
  112. 'theming_example_list' => array(
  113. // We use 'variables' when the item to be passed is an array whose
  114. // structure must be described here.
  115. 'variables' => array(
  116. 'title' => NULL,
  117. 'items' => NULL,
  118. ),
  119. ),
  120. 'theming_example_select_form' => array(
  121. 'render element' => 'form',
  122. ),
  123. 'theming_example_text_form' => array(
  124. 'render element' => 'form',
  125. // In this one the rendering will be done by a template file
  126. // (theming-example-text-form.tpl.php) instead of being rendered by a
  127. // function. Note the use of dashes to separate words in place of
  128. // underscores. The template file's extension is also left out so that
  129. // it may be determined automatically depending on the template engine
  130. // the site is using.
  131. 'template' => 'theming-example-text-form',
  132. ),
  133. );
  134. }
  135. /**
  136. * Initial landing page explaining the use of the module.
  137. *
  138. * We create a render array and specify the theme to be used through the use
  139. * of #theme_wrappers. With all output, we aim to leave the content as a
  140. * render array just as long as possible, so that other modules (or the theme)
  141. * can alter it.
  142. *
  143. * @see render_example.module
  144. * @see form_example_elements.inc
  145. */
  146. function theming_example_page() {
  147. $content[]['#markup'] = t('Some examples of pages and forms that are run through theme functions.');
  148. $content[]['#markup'] = l(t('Simple page with a list'), 'examples/theming_example/theming_example_list_page');
  149. $content[]['#markup'] = l(t('Simple form 1'), 'examples/theming_example/theming_example_select_form');
  150. $content[]['#markup'] = l(t('Simple form 2'), 'examples/theming_example/theming_example_text_form');
  151. $content['#theme_wrappers'] = array('theming_example_content_array');
  152. return $content;
  153. }
  154. /**
  155. * The list page callback.
  156. *
  157. * An example page where the output is supplied as an array which is themed
  158. * into a list and styled with css.
  159. *
  160. * In this case we'll use the core-provided theme_item_list as a #theme_wrapper.
  161. * Any theme need only override theme_item_list to change the behavior.
  162. */
  163. function theming_example_list_page() {
  164. $items = array(
  165. t('First item'),
  166. t('Second item'),
  167. t('Third item'),
  168. t('Fourth item'),
  169. );
  170. // First we'll create a render array that simply uses theme_item_list.
  171. $title = t("A list returned to be rendered using theme('item_list')");
  172. $build['render_version'] = array(
  173. // We use #theme here instead of #theme_wrappers because theme_item_list()
  174. // is the classic type of theme function that does not just assume a
  175. // render array, but instead has its own properties (#type, #title, #items).
  176. '#theme' => 'item_list',
  177. // '#type' => 'ul', // The default type is 'ul'
  178. // We can easily make sure that a css or js file is present using #attached.
  179. '#attached' => array('css' => array(drupal_get_path('module', 'theming_example') . '/theming_example.css')),
  180. '#title' => $title,
  181. '#items' => $items,
  182. '#attributes' => array('class' => array('render-version-list')),
  183. );
  184. // Now we'll create a render array which uses our own list formatter,
  185. // theme('theming_example_list').
  186. $title = t("The same list rendered by theme('theming_example_list')");
  187. $build['our_theme_function'] = array(
  188. '#theme' => 'theming_example_list',
  189. '#attached' => array('css' => array(drupal_get_path('module', 'theming_example') . '/theming_example.css')),
  190. '#title' => $title,
  191. '#items' => $items,
  192. );
  193. return $build;
  194. }
  195. /**
  196. * A simple form that displays a select box and submit button.
  197. *
  198. * This form will be be themed by the 'theming_example_select_form' theme
  199. * handler.
  200. */
  201. function theming_example_select_form($form, &$form_state) {
  202. $options = array(
  203. 'newest_first' => t('Newest first'),
  204. 'newest_last' => t('Newest last'),
  205. 'edited_first' => t('Edited first'),
  206. 'edited_last' => t('Edited last'),
  207. 'by_name' => t('By name'),
  208. );
  209. $form['choice'] = array(
  210. '#type' => 'select',
  211. '#options' => $options,
  212. '#title' => t('Choose which ordering you want'),
  213. );
  214. $form['submit'] = array(
  215. '#type' => 'submit',
  216. '#value' => t('Go'),
  217. );
  218. return $form;
  219. }
  220. /**
  221. * Submit handler for the select form.
  222. *
  223. * @param array $form
  224. * Form API form array.
  225. * @param array $form_state
  226. * Form API form state array.
  227. */
  228. function theming_example_select_form_submit($form, &$form_state) {
  229. drupal_set_message(t('You chose %input', array('%input' => $form_state['values']['choice'])));
  230. }
  231. /**
  232. * A simple form that displays a textfield and submit button.
  233. *
  234. * This form will be rendered by theme('form') (theme_form() by default)
  235. * because we do not provide a theme function for it here.
  236. */
  237. function theming_example_text_form($form, &$form_state) {
  238. $form['text'] = array(
  239. '#type' => 'textfield',
  240. '#title' => t('Please input something!'),
  241. '#required' => TRUE,
  242. );
  243. $form['submit'] = array(
  244. '#type' => 'submit',
  245. '#value' => t('Go'),
  246. );
  247. return $form;
  248. }
  249. /**
  250. * Submit handler for the text form.
  251. *
  252. * @param array $form
  253. * Form API form array.
  254. * @param array $form_state
  255. * Form API form state array.
  256. */
  257. function theming_example_text_form_submit($form, &$form_state) {
  258. drupal_set_message(t('You entered %input', array('%input' => $form_state['values']['text'])));
  259. }
  260. /**
  261. * Theme a simple content array.
  262. *
  263. * This theme function uses the newer recommended format where a single
  264. * render array is provided to the theme function.
  265. */
  266. function theme_theming_example_content_array($variables) {
  267. $element = $variables['element'];
  268. $output = '';
  269. foreach (element_children($element) as $count) {
  270. if (!$count) {
  271. // The first paragraph is bolded.
  272. $output .= '<p><strong>' . $element[$count]['#children'] . '</strong></p>';
  273. }
  274. else {
  275. // Following paragraphs are just output as routine paragraphs.
  276. $output .= '<p>' . $element[$count]['#children'] . '</p>';
  277. }
  278. }
  279. return $output;
  280. }
  281. /**
  282. * Theming a simple list.
  283. *
  284. * This is just a simple wrapper around theme('item_list') but it's worth
  285. * showing how a custom theme function can be implemented.
  286. *
  287. * @see theme_item_list()
  288. */
  289. function theme_theming_example_list($variables) {
  290. $title = $variables['title'];
  291. $items = $variables['items'];
  292. // Add the title to the list theme and
  293. // state the list type. This defaults to 'ul'.
  294. // Add a css class so that you can modify the list styling.
  295. // We'll just call theme('item_list') to render.
  296. $variables = array(
  297. 'items' => $items,
  298. 'title' => $title,
  299. 'type' => 'ol',
  300. 'attributes' => array('class' => 'theming-example-list'),
  301. );
  302. $output = theme('item_list', $variables);
  303. return $output;
  304. }
  305. /**
  306. * Theming a simple form.
  307. *
  308. * Since our form is named theming_example_select_form(), the default
  309. * #theme function applied to is will be 'theming_example_select_form'
  310. * if it exists. The form could also have specified a different
  311. * #theme.
  312. *
  313. * Here we collect the title, theme it manually and
  314. * empty the form title. We also wrap the form in a div.
  315. */
  316. function theme_theming_example_select_form($variables) {
  317. $form = $variables['form'];
  318. $title = $form['choice']['#title'];
  319. $form['choice']['#title'] = '';
  320. $output = '<strong>' . $title . '</strong>';
  321. $form['choice']['#prefix'] = '<div class="container-inline">';
  322. $form['submit']['#suffix'] = '</div>';
  323. $output .= drupal_render_children($form);
  324. return $output;
  325. }
  326. /**
  327. * Implements template_preprocess().
  328. *
  329. * We prepare variables for use inside the theming-example-text-form.tpl.php
  330. * template file.
  331. *
  332. * In this example, we create a couple new variables, 'text_form' and
  333. * 'text_form_content', that clean up the form output. Drupal will turn the
  334. * array keys in the $variables array into variables for use in the template.
  335. *
  336. * So $variables['text_form'] becomes available as $text_form in the template.
  337. *
  338. * @see theming-example-text-form.tpl.php
  339. */
  340. function template_preprocess_theming_example_text_form(&$variables) {
  341. $variables['text_form_content'] = array();
  342. $text_form_hidden = array();
  343. // Each form element is rendered and saved as a key in $text_form_content, to
  344. // give the themer the power to print each element independently in the
  345. // template file. Hidden form elements have no value in the theme, so they
  346. // are grouped into a single element.
  347. foreach (element_children($variables['form']) as $key) {
  348. $type = $variables['form'][$key]['#type'];
  349. if ($type == 'hidden' || $type == 'token') {
  350. $text_form_hidden[] = drupal_render($variables['form'][$key]);
  351. }
  352. else {
  353. $variables['text_form_content'][$key] = drupal_render($variables['form'][$key]);
  354. }
  355. }
  356. $variables['text_form_content']['hidden'] = implode($text_form_hidden);
  357. // The entire form is then saved in the $text_form variable, to make it easy
  358. // for the themer to print the whole form.
  359. $variables['text_form'] = implode($variables['text_form_content']);
  360. }
  361. /**
  362. * @} End of "defgroup theming_example".
  363. */