compact_forms.module in Compact Forms 5
Same filename and directory in other branches
Compact Forms Drupal module.
File
compact_forms.moduleView source
<?php
/**
* @file
* Compact Forms Drupal module.
*/
/**
* Implementation of hook_help().
*/
function compact_forms_help($section) {
switch ($section) {
case 'admin/help#compact_forms':
return '<p>' . t('Makes form fields compact by overlaying the field label on top the field itself using jQuery.') . '</p>';
}
}
/**
* Implementation of hook_perm().
*/
function compact_forms_perm() {
return array(
'administer Compact Forms',
);
}
/**
* Implementation of hook_menu().
*/
function compact_forms_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'title' => t('Compact Forms'),
'path' => 'admin/settings/compact_forms',
'description' => t('Configure Compact Forms settings.'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'compact_forms_admin_form',
),
'access' => user_access('administer Compact Forms'),
);
}
else {
if (strpos($_GET['q'], 'admin/settings/compact_forms') === 0) {
require_once drupal_get_path('module', 'compact_forms') . '/compact_forms.admin.inc';
}
}
return $items;
}
/**
* Implementation of hook_form_alter().
*/
function compact_forms_form_alter($form_id, &$form) {
static $css_ids, $form_ids, $loaded, $field_size, $descriptions;
// Prepare CSS form ids.
if (!isset($css_ids)) {
$css_ids = explode("\n", variable_get('compact_forms_ids', 'user-login-form'));
$css_ids = array_filter(array_map('trim', $css_ids));
}
// Prepare Form API form ids.
if (!isset($form_ids) && !empty($css_ids)) {
$form_ids = array();
foreach ($css_ids as $id) {
$form_ids[] = strtr($id, array(
'-' => '_',
));
}
}
// Prepare form alteration settings.
if (!isset($field_size)) {
$field_size = variable_get('compact_forms_field_size', '');
$descriptions = variable_get('compact_forms_descriptions', 1);
}
if (in_array($form_id, $form_ids) || isset($form['#id']) && in_array($form['#id'], $css_ids)) {
// Load our page requisites and JavaScript settings.
if (!isset($loaded)) {
_compact_forms_include_js($css_ids);
$loaded = TRUE;
}
// Only alter the form if a custom field size is configured or form element
// descriptions shall be hidden.
if (!empty($field_size) || !$descriptions) {
_compact_forms_resize_fields($form, $field_size, $descriptions);
}
}
}
/**
* Helper function to recursively alter form elements.
*
* @todo Perform this in #after_build instead. - Or use hook_elements() to
* append a #process function to all supported elements.
*/
function _compact_forms_resize_fields(&$form, $field_size, $descriptions) {
if (empty($form) || !is_array($form)) {
return;
}
foreach (element_children($form) as $key) {
if (!isset($form[$key]['#type'])) {
continue;
}
switch ($form[$key]['#type']) {
case 'fieldset':
_compact_forms_resize_fields($form[$key], $field_size, $descriptions);
break;
case 'textfield':
case 'textarea':
case 'password':
case 'password_confirm':
if (!empty($field_size)) {
$form[$key]['#size'] = $field_size;
}
if (!$descriptions) {
unset($form[$key]['#description']);
}
break;
}
}
}
/**
* Include JavaScript and CSS and attach behaviors to all selected forms.
*
* @param $css_ids
* An array containing CSS form ids.
*/
function _compact_forms_include_js($css_ids) {
$path = drupal_get_path('module', 'compact_forms');
drupal_add_js($path . '/compact_forms.js');
drupal_add_css($path . '/compact_forms.css');
$settings = array(
'compactForms' => array(
'forms' => $css_ids,
'stars' => (int) variable_get('compact_forms_stars', 2),
'colons' => (int) variable_get('compact_forms_colons', 0),
),
);
drupal_add_js($settings, 'setting');
}
Functions
Name![]() |
Description |
---|---|
compact_forms_form_alter | Implementation of hook_form_alter(). |
compact_forms_help | Implementation of hook_help(). |
compact_forms_menu | Implementation of hook_menu(). |
compact_forms_perm | Implementation of hook_perm(). |
_compact_forms_include_js | Include JavaScript and CSS and attach behaviors to all selected forms. |
_compact_forms_resize_fields | Helper function to recursively alter form elements. |