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

Contents of /contributions/modules/compact_forms/compact_forms.module

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


Revision 1.9 - (show annotations) (download) (as text)
Wed Sep 30 03:52:36 2009 UTC (8 weeks, 3 days ago) by sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +10 -6 lines
File MIME type: text/x-php
by sun: Ported to Drupal 7.
1 <?php
2 // $Id: compact_forms.module,v 1.8 2009/08/06 20:27:22 sun Exp $
3
4 /**
5 * @file
6 * Compact Forms Drupal module.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function compact_forms_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#compact_forms':
15 return '<p>' . t('Makes form fields compact by overlaying the field label on top the field itself using jQuery.') . '</p>';
16 }
17 }
18
19 /**
20 * Implementation of hook_permission().
21 */
22 function compact_forms_permission() {
23 return array(
24 'administer Compact Forms' => array(
25 'title' => t('Administer Compact Forms'),
26 'description' => t('Configure forms to display compact and their behavior.'),
27 ),
28 );
29 }
30
31 /**
32 * Implementation of hook_menu().
33 */
34 function compact_forms_menu() {
35 $items['admin/config/content/compact_forms'] = array(
36 'title' => 'Compact Forms',
37 'description' => 'Configure Compact Forms settings.',
38 'page callback' => 'drupal_get_form',
39 'page arguments' => array('compact_forms_admin_form'),
40 'access arguments' => array('administer Compact Forms'),
41 'file' => 'compact_forms.admin.inc',
42 );
43 return $items;
44 }
45
46 /**
47 * Implementation of hook_form_alter().
48 */
49 function compact_forms_form_alter(&$form, $form_state, $form_id) {
50 static $css_ids, $form_ids, $loaded, $field_size, $descriptions;
51
52 // Prepare CSS form ids.
53 if (!isset($css_ids)) {
54 $css_ids = explode("\n", variable_get('compact_forms_ids', 'user-login-form'));
55 $css_ids = array_filter(array_map('trim', $css_ids));
56 }
57 // Prepare Form API form ids.
58 if (!isset($form_ids) && !empty($css_ids)) {
59 $form_ids = array();
60 foreach ($css_ids as $id) {
61 $form_ids[] = strtr($id, array('-' => '_'));
62 }
63 }
64 // Prepare form alteration settings.
65 if (!isset($field_size)) {
66 $field_size = variable_get('compact_forms_field_size', '');
67 $descriptions = variable_get('compact_forms_descriptions', 1);
68 }
69
70 if (in_array($form_id, $form_ids) || (isset($form['#id']) && in_array($form['#id'], $css_ids))) {
71 // Load our page requisites and JavaScript settings.
72 if (!isset($loaded)) {
73 _compact_forms_include_js($css_ids);
74 $loaded = TRUE;
75 }
76 // Only alter the form if a custom field size is configured or form element
77 // descriptions shall be hidden.
78 if (!empty($field_size) || !$descriptions) {
79 _compact_forms_resize_fields($form, $field_size, $descriptions);
80 }
81 }
82 }
83
84 /**
85 * Helper function to recursively alter form elements.
86 *
87 * @todo Perform this in #after_build instead. - Or use hook_elements() to
88 * append a #process function to all supported elements.
89 */
90 function _compact_forms_resize_fields(&$form, $field_size, $descriptions) {
91 if (empty($form) || !is_array($form)) {
92 return;
93 }
94 foreach (element_children($form) as $key) {
95 if (!isset($form[$key]['#type'])) {
96 continue;
97 }
98 switch ($form[$key]['#type']) {
99 case 'fieldset':
100 _compact_forms_resize_fields($form[$key], $field_size, $descriptions);
101 break;
102
103 case 'textfield':
104 case 'textarea':
105 case 'password':
106 case 'password_confirm':
107 if (!empty($field_size)) {
108 $form[$key]['#size'] = $field_size;
109 }
110 if (!$descriptions) {
111 unset($form[$key]['#description']);
112 }
113 break;
114 }
115 }
116 }
117
118 /**
119 * Include JavaScript and CSS and attach behaviors to all selected forms.
120 *
121 * @param $css_ids
122 * An array containing CSS form ids.
123 */
124 function _compact_forms_include_js($css_ids) {
125 $path = drupal_get_path('module', 'compact_forms');
126 drupal_add_js($path . '/compact_forms.js');
127 drupal_add_css($path . '/compact_forms.css');
128
129 $settings = array(
130 'compactForms' => array(
131 'forms' => $css_ids,
132 'stars' => (int) variable_get('compact_forms_stars', 2),
133 ),
134 );
135 drupal_add_js($settings, 'setting');
136 }
137

  ViewVC Help
Powered by ViewVC 1.1.2