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

Contents of /contributions/modules/fieldgroup_table/fieldgroup_table.module

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


Revision 1.1 - (show annotations) (download) (as text)
Thu Dec 28 21:33:46 2006 UTC (2 years, 10 months ago) by bdragon
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Small module that modifies the behavior of CCK fieldgroup.
Any multiple value fields inside a group with "multiple values" (suggestions for a better name) checkbox checked will be automagically transmogrified into a table on the edit page.

Drupal 5 only. (so far)
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Turn multiple value fields on fieldgroups into tables.
7 */
8
9 /**
10 * Implementation of hook_form_alter().
11 */
12 function fieldgroup_table_form_alter($form_id, &$form) {
13 // Add our setting to the group edit form.
14 if ($form_id == 'fieldgroup_edit_group_form') {
15 $form['settings']['multiple'] = array(
16 '#type' => 'checkbox',
17 '#title' => t('Multiple values'),
18 '#default_value' => fieldgroup_table_get_multiple($form),
19 );
20 }
21
22 // Alter any fieldgroups we find.
23 if (isset($form['type']) && !$form['#programmed'] && $form['type']['#value'] .'_node_form' == $form_id) {
24 foreach (fieldgroup_groups($form['type']['#value']) as $group_name => $group) {
25 // Are we allowed to touch this fieldgroup?
26 if (isset($group['settings']['multiple']) && $group['settings']['multiple']) {
27 $fields = array();
28 $header = array();
29 // Hijack the fields with multiple values enabled.
30 foreach ($group['fields'] as $field_name => $field) {
31 if (isset($form[$group_name][$field_name])) {
32 if (fieldgroup_table_check_field_multiple($field_name)) {
33 $header[] = t($field['label']);
34 // @@@ Description is currently lost -- add legend?
35 $fields[$field_name] = $form[$group_name][$field_name];
36 // Yoink!
37 unset($form[$group_name][$field_name]);
38 }
39 }
40 }
41 $rows = array();
42 $delta = 0;
43 // Rewrite the array.
44 while (TRUE) {
45 $row = array();
46 foreach ($fields as $field_name => $field) {
47 if (isset($field[$delta])) {
48 $row[$field_name] = $field[$delta];
49 // Enable tree.
50 $row[$field_name]['#tree'] = TRUE;
51 // Override the parents with the "normal" path.
52 $row[$field_name]['#parents'] = array($field_name,$delta);
53 }
54 }
55 if (empty($row)) {
56 break;
57 }
58 $rows[] = $row;
59 $delta++;
60 }
61
62 $form[$group_name]['fieldgroup_table'] = array(
63 '#header' => $header,
64 '#type' => 'fieldgroup_table',
65 '#weight' => -10, // @@@ Configurable?
66 );
67
68 foreach ($rows as $row) {
69 $form[$group_name]['fieldgroup_table'][] = $row;
70 }
71 }
72 }
73 }
74 }
75
76 /**
77 * Implementation of hook_elements().
78 */
79 function fieldgroup_table_elements() {
80 return array(
81 'fieldgroup_table' => array(),
82 );
83 }
84
85 /**
86 * Theme callback for the fieldgroup_table type.
87 */
88 function theme_fieldgroup_table($element) {
89 $rows = array();
90 $delta = 0;
91 while ($result = $element[$delta]) {
92 $row = array();
93 foreach (element_children($result) as $field) {
94 $row[] = $result[$field]['#children'];
95 }
96 $rows[] = $row;
97 $delta++;
98 }
99 return theme('table',$element['#header'],$rows);
100 }
101
102 ///////////////////////////// Fragile functions //////////////////////////
103
104 /**
105 * Check if a fieldgroup is multiple value.
106 * Potentially fragile, so it's split into a seperate function.
107 */
108 function fieldgroup_table_get_multiple($form) {
109 // This is probabaly subject to change.
110 $type_name = $form['#parameters'][1]['type'];
111 $group_name = $form['#parameters'][2];
112 $result = db_query("SELECT settings FROM {node_group} WHERE type_name = '%s' AND group_name = '%s'",$type_name,$group_name);
113 if ($settings = db_result($result)) {
114 $settings = unserialize($settings);
115 return isset($settings['multiple']) ? $settings['multiple'] : FALSE;
116 }
117 return FALSE;
118 }
119
120 /**
121 * Check if a field is multiple value.
122 * Potentially fragile.
123 */
124 function fieldgroup_table_check_field_multiple($field_name) {
125 return db_result(db_query("SELECT multiple FROM {node_field} WHERE field_name = '%s'",$field_name));
126 }
127

  ViewVC Help
Powered by ViewVC 1.1.2