vertical_tabs_example.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @file
  4. * Module file for vertical_tabs_example module.
  5. */
  6. /**
  7. * @defgroup vertical_tabs_example Example: Vertical Tabs
  8. * @ingroup examples
  9. * @{
  10. * Demonstrates the vertical tabs functionality provided by Drupal 7.
  11. *
  12. * This example does not cover how to save or load a custom setting, and only
  13. * deals with the elements visibility and summary in the tab.
  14. *
  15. * @see vertical_tabs_example.js
  16. */
  17. /**
  18. * Implements hook_menu().
  19. *
  20. * Gives us a simple explanation page.
  21. */
  22. function vertical_tabs_example_menu() {
  23. $items['examples/vertical_tabs'] = array(
  24. 'title' => 'Vertical tabs example',
  25. 'description' => 'Shows how vertical tabs can best be supported by a custom module',
  26. 'page callback' => '_vertical_tabs_example_explanation',
  27. 'access callback' => TRUE,
  28. );
  29. return $items;
  30. }
  31. /**
  32. * Implements hook_form_alter().
  33. *
  34. * Adds custom fieldset to the node form, and attach ajax behaviour for vertical
  35. * panels to update the settings description.
  36. *
  37. * @see vertical_tabs_example.js
  38. */
  39. function vertical_tabs_example_form_alter(&$form, $form_state, $form_id) {
  40. // Only include on node add/edit forms.
  41. if (!empty($form['#node_edit_form'])) {
  42. // Create a fieldset that will be included in the vertical tab.
  43. $form['vertical_tabs_example'] = array(
  44. '#type' => 'fieldset',
  45. '#title' => t('Example vertical tab'),
  46. '#collapsible' => TRUE,
  47. '#collapsed' => FALSE,
  48. '#tree' => TRUE,
  49. // Send this tab to the top of the list.
  50. '#weight' => -99,
  51. // The #group value must match the name of the vertical tabs element.
  52. // In most cases, this is 'additional_settings'.
  53. '#group' => 'additional_settings',
  54. // Vertical tabs provide its most usable appearance when they are used to
  55. // include a summary of the information contained in the fieldset. To do
  56. // this, we attach additional JavaScript to handle changing the summary
  57. // when form settings are changed.
  58. '#attached' => array(
  59. 'js' => array(
  60. 'vertical-tabs' => drupal_get_path('module', 'vertical_tabs_example') . '/vertical_tabs_example.js',
  61. ),
  62. ),
  63. );
  64. // The form elements below provide a demonstration of how a fieldset
  65. // summary can be displayed in a collapsed tab.
  66. //
  67. // This checkbox is used to show or hide the custom settings form using
  68. // javascript (altering states of a container defined later).
  69. $form['vertical_tabs_example']['enabled'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Change this setting'),
  72. '#default_value' => FALSE,
  73. );
  74. // This container will be used to store the whole form for our custom
  75. // settings. This way, showing/hiding the form using javascript is easier,
  76. // as only one element should be set visible.
  77. $form['vertical_tabs_example']['vertical_tabs_examplecontainer'] = array(
  78. '#type' => 'container',
  79. '#parents' => array('vertical_tabs_example'),
  80. '#states' => array(
  81. 'invisible' => array(
  82. // If the checkbox is not enabled, show the container.
  83. 'input[name="vertical_tabs_example[enabled]"]' => array('checked' => FALSE),
  84. ),
  85. ),
  86. );
  87. // The string of this textfield will be shown as summary in the vertical
  88. // tab.
  89. $form['vertical_tabs_example']['vertical_tabs_examplecontainer']['custom_setting'] = array(
  90. '#type' => 'textfield',
  91. '#title' => t('Use this setting instead'),
  92. '#default_value' => 'I am a setting with a summary',
  93. '#description' => t('As you type into this field, the summary will be updated in the tab.'),
  94. );
  95. }
  96. }
  97. /**
  98. * Simple explanation page.
  99. */
  100. function _vertical_tabs_example_explanation() {
  101. return t("<p>The Vertical Tabs Example shows how a custom module can add a vertical tab to a node edit form, and support its summary field with JavaScript.</p><p>To see the effects of this module, <a href='!node_add'>add a piece of content</a> and look at the set of tabs at the bottom. We've added one called 'Example vertical tab.'</p>", array('!node_add' => url('node/add')));
  102. }
  103. /**
  104. * @} End of "defgroup vertical_tabs_example".
  105. */