/[drupal]/contributions/modules/vertical_tabs/vertical_tabs.module
ViewVC logotype

Contents of /contributions/modules/vertical_tabs/vertical_tabs.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Aug 16 05:36:43 2008 UTC (15 months, 1 week ago) by dmitrig01
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
File MIME type: text/x-php
Initial commit of the Vertical Tabs module. Provides vertical tabs on the node add form instead of fieldsets (see http://tinyurl.com/vertical-tabs).
1 <?php
2
3 /**
4 * @file Main file for the vertical tabs module - provides vertical tabs on the
5 * node form.
6 */
7
8 /**
9 * Implementation of hook_form_alter.
10 */
11 function vertical_tabs_form_alter(&$form, $form_state, $form_id) {
12 // Check to see which form we are in. Since the node form's form id is not
13 // just node_form, we can't implement a function for all node forms, so we are
14 // stuck implementing the global form_alter and checking if there is the
15 // string node_form in the form id.
16 if (strpos($form_id, 'node_form')) {
17 // We're in a node form - we can't group all fieldsets into a vertical_tabs
18 // element, because validation errors would arise from that, so instead we
19 // have to add a large number to all the weights of the fieldsets, because
20 // we want them to all be grouped together. Additionally, we need to bind
21 // the summary callback and add other JavaScript.
22 // The $fieldsets array is a list of all supported fieldsets in core,
23 // and their JavaScript summary creators.
24 $fieldsets = array(
25 'book' => 'book',
26 'revision_information' => 'revision',
27 'author' => 'authoring',
28 'options' => 'publishingOptions',
29 'menu' => 'menu',
30 'comment_settings' => 'comment',
31 'path' => 'path',
32 'attachments' => 'attachments',
33 );
34 // The javascript to add to the page.
35 $javascript = array();
36 // Store the greatest weight so we can know how low to move the buttons.
37 $greatest_weight = 0;
38 // Iterate through the form, finding fieldsets.
39 foreach (element_children($form) as $key) {
40 // We need to make sure that the element we have is a fieldset.
41 if ($form[$key]['#type'] == 'fieldset') {
42 // Assign the weight.
43 $form[$key]['#weight'] = ($form[$key]['#weight'] ? $form[$key]['#weight'] + 1000000 : 1000000);
44 // Determine the greatest weight.
45 $greatest_weight = max($greatest_weight, $form[$key]['#weight']);
46 // Add the JavaScript.
47 $javascript[$key] = array('name' => $form[$key]['#title']);
48 // If there's a summary callback, then add it.
49 if (isset($form[$key]['#summary_callback']) || isset($fieldsets[$key])) {
50 $javascript[$key]['callback'] = (isset($form[$key]['#summary_callback']) ? $form[$key]['#summary_callback'] : $fieldsets[$key]);
51 $javascript[$key]['args'] = (isset($form[$key]['#summary_arguments']) ? $form[$key]['#summary_arguments'] : array());
52 }
53 // Add a class to identify the fieldset.
54 if (isset($form[$key]['#attributes']['class']) && !empty($form[$key]['#attributes']['class'])) {
55 $form[$key]['#attributes']['class'] .= ' vertical-tabs-fieldset vertical-tabs-'. $key;
56 }
57 else {
58 $form[$key]['#attributes']['class'] = 'vertical-tabs-fieldset vertical-tabs-'. $key;
59 }
60 }
61 }
62 // Move the buttons, and add a <div> for easier placement of the tab element.
63 $form['buttons']['#weight'] = $greatest_weight + 1;
64 $form['buttons']['#prefix'] = '<div class="buttons">';
65 $form['buttons']['#suffix'] = '</div>';
66 // Add the JavaScript.
67 drupal_add_js(array('verticalTabs' => $javascript), 'setting');
68 // Indicate that the JavaScript should be added later. We do this so that
69 // our JavaScript gets added after collapse.js.
70 $form['#post_render'][] = 'vertical_tabs_add_js_css';
71 }
72 }
73
74 /**
75 * Add the JavaScript and CSS.
76 */
77 function vertical_tabs_add_js_css($form) {
78 drupal_add_js(drupal_get_path('module', 'vertical_tabs') .'/vertical_tabs.js');
79 drupal_add_css(drupal_get_path('module', 'vertical_tabs') .'/vertical_tabs.css');
80 return $form;
81 }
82

  ViewVC Help
Powered by ViewVC 1.1.2