cck_fieldgroup_tabs.module

Tracking master branch
  1. drupal
    1. 6 contributions/cck_fieldgroup_tabs/cck_fieldgroup_tabs.module

Add tabs display option for CCK fieldgroups.

Functions & methods

NameDescription
cck_fieldgroup_tabs_content_extra_fields
cck_fieldgroup_tabs_form_alter
cck_fieldgroup_tabs_nodeapi
cck_fieldgroup_tabs_node_type_form
cck_fieldgroup_tabs_pre_render

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Add tabs display option for CCK fieldgroups.
  5. */
  6. /*
  7. * Implementation of hook_form_alter().
  8. */
  9. function cck_fieldgroup_tabs_form_alter(&$form, $form_state, $form_id) {
  10. if (isset($form['type']) && isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) {
  11. if (!isset($form['#pre_render'])) {
  12. $form['#pre_render'] = array();
  13. }
  14. // Register a process callback.
  15. $form['#pre_render'][] = 'cck_fieldgroup_tabs_pre_render';
  16. }
  17. else if ($form_id == 'content_display_overview_form') {
  18. if (isset($form['#groups']) && $form['#groups']) {
  19. $contexts = content_build_modes($form['#contexts']);
  20. foreach ($form['#groups'] as $name) {
  21. foreach (array_keys($contexts) as $key) {
  22. $form[$name][$key]['format']['#options']['tabs'] = t('tabs');
  23. }
  24. }
  25. }
  26. }
  27. else if ($form_id == 'fieldgroup_group_edit_form') {
  28. $form['settings']['form']['style']['#options']['tabs'] = t('tabs');
  29. }
  30. elseif ($form_id == 'node_type_form' && $form['#node_type']->type && !$form['#programmed']) {
  31. $form += cck_fieldgroup_tabs_node_type_form($form['#node_type']->type);
  32. }
  33. }
  34. /*
  35. * Implementation of hook_content_extra_fields().
  36. */
  37. function cck_fieldgroup_tabs_content_extra_fields($type_name) {
  38. if ($title = variable_get('cck_fieldgroup_tabs_residual_title_' . $type_name, '')) {
  39. $extra = array();
  40. $extra['cck_fieldgroup_tabs_residual'] = array(
  41. 'label' => t('@title field', array('@title' => $title)),
  42. 'description' => t('CCK fieldgroup tabs Basics tab.'),
  43. 'weight' => -20,
  44. );
  45. return $extra;
  46. }
  47. }
  48. /*
  49. * Settings for content type forms.
  50. */
  51. function cck_fieldgroup_tabs_node_type_form($node_type) {
  52. $form = array();
  53. $form['cck_fieldgroup_tabs'] = array(
  54. '#type' => 'fieldset',
  55. '#title' => t('CCK Fieldgroup Tabs'),
  56. '#collapsible' => TRUE,
  57. '#collapsed' => TRUE,
  58. '#description' => t('CCK Fieldgroup Tabs with residual fields not in fieldgroup tabs in a Basics Tab.'),
  59. );
  60. $form['cck_fieldgroup_tabs']['cck_fieldgroup_tabs_residual_title'] = array(
  61. '#type' => 'textfield',
  62. '#title' => t('Name of default basic tab'),
  63. '#description' => t('Leave blank to keep top level form elements like title and body outside of a Basic Tabs.'),
  64. '#default_value' => variable_get('cck_fieldgroup_tabs_residual_title_' . $node_type, ''),
  65. );
  66. $form['cck_fieldgroup_tabs']['cck_fieldgroup_tabs_description'] = array(
  67. '#type' => 'textarea',
  68. '#title' => t('Description'),
  69. '#description' => t('Text to display above the tabset on content display.'),
  70. '#default_value' => variable_get('cck_fieldgroup_tabs_description_' . $node_type, ''),
  71. );
  72. return $form;
  73. }
  74. /*
  75. * Form pre-render callback.
  76. *
  77. * Convert fieldset elements to tabs.
  78. */
  79. function cck_fieldgroup_tabs_pre_render($form) {
  80. if (!isset($form['fieldgroup_tabs'])) {
  81. foreach (fieldgroup_groups($form['type']['#value']) as $group_name => $group) {
  82. if (isset($form[$group_name]) && $group['settings']['form']['style'] == 'tabs' && (!isset($form[$group_name]['#access']) || $form[$group_name]['#access'])) {
  83. // Add a tabset if not already added.
  84. if (!isset($form['fieldgroup_tabs'])) {
  85. $form['fieldgroup_tabs'] = array(
  86. '#type' => 'tabset',
  87. '#weight' => $group['weight'],
  88. );
  89. }
  90. // Otherwise, reset the weight if lower than what's already set.
  91. // This ensures the tabset has the weight of the lowest fieldgroup
  92. // it includes.
  93. else if ($group['weight'] < $form['fieldgroup_tabs']['#weight']) {
  94. $form['fieldgroup_tabs']['#weight'] = $group['weight'];
  95. }
  96. $element = $form[$group_name];
  97. $element['#type'] = 'tabpage';
  98. $element['#weight'] = $group['weight'];
  99. unset($element['#collapsible'], $element['#collapsed']);
  100. $form['fieldgroup_tabs'][$group_name] = $element;
  101. unset($form[$group_name]);
  102. }
  103. }
  104. $node_type = $form['type']['#value'];
  105. if (isset($form['fieldgroup_tabs']) && variable_get('cck_fieldgroup_tabs_residual_title_'. $node_type, FALSE)) {
  106. // Add residual content to a tab if desired.
  107. $residual = array(
  108. '#type' => 'tabpage',
  109. // Pass the value through t() if needed.
  110. '#title' => t(variable_get('cck_fieldgroup_tabs_residual_title_'. $node_type, '')),
  111. '#weight' => content_extra_field_weight($node_type, 'cck_fieldgroup_tabs_residual'),
  112. );
  113. foreach (element_children($form) as $key) {
  114. // Skip the buttons so that they are below the tabset. Don't process
  115. // hidden or value fields.
  116. if ($key != 'fieldgroup_tabs' && $key != 'buttons' && (!isset($form[$key]['#type']) || !in_array($form[$key]['#type'], array('hidden', 'value')))) {
  117. $residual[$key] = $form[$key];
  118. unset($form[$key]);
  119. }
  120. }
  121. $form['fieldgroup_tabs']['fieldgroup_tabs_basic'] = $residual;
  122. }
  123. }
  124. return $form;
  125. }
  126. /*
  127. * Implementation of hook_nodeapi().
  128. */
  129. function cck_fieldgroup_tabs_nodeapi(&$node, $op, $teaser, $page) {
  130. switch ($op) {
  131. case 'view':
  132. // NODE_BUILD_NORMAL is 0, and ('whatever' == 0) is TRUE, so we need a ===.
  133. if ($node->build_mode === NODE_BUILD_NORMAL || $node->build_mode == NODE_BUILD_PREVIEW) {
  134. $context = $teaser ? 'teaser' : 'full';
  135. }
  136. else {
  137. $context = $node->build_mode;
  138. }
  139. foreach (fieldgroup_groups($node->type) as $group_name => $group) {
  140. // $node->content[$group_name] will be set only if there was visible content.
  141. if (isset($node->content[$group_name]) && $group['settings']['display'][$context]['format'] == 'tabs') {
  142. // Add a tabset if not already added.
  143. if (!isset($node->content['fieldgroup_tabs'])) {
  144. $node->content['fieldgroup_tabs'] = array(
  145. '#type' => 'tabset',
  146. '#weight' => $group['weight'],
  147. );
  148. if ($description = variable_get('cck_fieldgroup_tabs_description_' . $node->type, '')) {
  149. $node->content['fieldgroup_tabs']['#description'] = filter_xss_admin($description);
  150. }
  151. }
  152. // Otherwise, reset the weight if lower than what's already set.
  153. // This ensures the tabset has the weight of the lowest fieldgroup
  154. // it includes.
  155. else if ($group['weight'] < $node->content['fieldgroup_tabs']['#weight']) {
  156. $node->content['fieldgroup_tabs']['#weight'] = $group['weight'];
  157. }
  158. $element = $node->content[$group_name]['group'];
  159. $element['#type'] = 'tabpage';
  160. $element['#weight'] = $group['weight'];
  161. $node->content['fieldgroup_tabs'][$group_name] = $element;
  162. unset($node->content[$group_name]);
  163. }
  164. }
  165. if (isset($node->content['fieldgroup_tabs'])) {
  166. // Add residual content to a tab if desired.
  167. if (variable_get('cck_fieldgroup_tabs_residual_title_'. $node->type, FALSE)) {
  168. $residual = array(
  169. '#type' => 'tabpage',
  170. '#title' => t(variable_get('cck_fieldgroup_tabs_residual_title_'. $node->type, '')),
  171. '#weight' => content_extra_field_weight($node->type, 'cck_fieldgroup_tabs_residual'),
  172. );
  173. foreach (element_children($node->content) as $key) {
  174. if ($key != 'fieldgroup_tabs') {
  175. $residual[$key] = $node->content[$key];
  176. unset($node->content[$key]);
  177. }
  178. }
  179. // Move all #content_extra_fields into the residual tab in order to allow tabs to render them correctly.
  180. // #pre-render with alter_extra_weights in the tab is needed to correctly sort the elements.
  181. $residual['#content_extra_fields'] = $node->content['#content_extra_fields'];
  182. $residual['#pre_render'] = array('content_alter_extra_weights');
  183. $key = array_search('content_alter_extra_weights', $node->content['#pre_render']);
  184. unset($node->content['#content_extra_fields'], $node->content['#pre_render'][$key]);
  185. $node->content['fieldgroup_tabs']['fieldgroup_tabs_basic'] = $residual;
  186. }
  187. }
  188. break;
  189. }
  190. }