ife.module in Inline Form Errors 6
Same filename and directory in other branches
Drupal hooks
@author Stijn De Meyere
File
ife.moduleView source
<?php
/**
* @file
* Drupal hooks
*
* @author Stijn De Meyere
*/
/**
* Implementation of hook_menu().
*/
function ife_menu() {
$items = array();
$items['admin/settings/ife'] = array(
'title' => 'Inline Form Errors',
'description' => 'Administer which forms to use with field messages.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ife_settings_form',
),
'access arguments' => array(
'administer inline form errors',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'ife.settings.inc',
);
$items['admin/settings/ife/%ife_form_id/delete'] = array(
'title' => 'Delete form_id',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ife_form_id_delete_form',
3,
),
'access arguments' => array(
'administer inline form errors',
),
'type' => MENU_CALLBACK,
'file' => 'ife.settings.inc',
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function ife_perm() {
return array(
'administer inline form errors',
);
}
/**
* Implementation of hook_help().
*/
function ife_help($path, $arg) {
switch ($path) {
case 'admin/help#ife':
$output = '<p>' . t('IFE or Inline Form Errors allows you to place form submission error inline with the form elements. Three options are provided for setting your inline error behaviour. You can configure the default behaviour or override the behaviour on a per form basis. You can add as many forms as you like.') . '</p>';
$output .= '<p>' . t('IFE provides three behaviours for the configured forms') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('<strong>Leave the messages in place</strong>, this option will copy the error messages and place them inline. The original error messages set by Drupal will remain in place') . '</li>';
$output .= '<li>' . t("<strong>Show an alternate message</strong>, this option will replace the original messages with a generic error message such as 'Please correct all errors.'. This message can be set in the IFE configuration page. The original error messages are placed inline with the form elements") . '</li>';
$output .= '<li>' . t('<strong>Remove all messages</strong>, this option will remove all error messages and place them inline with the form element') . '</li>';
$output .= '</ul>';
$output .= '<p>' . t('In all cases only the messages related to the form will be touched. All other messages will remain in tact.') . '</p>';
return $output;
case 'admin/settings/ife':
return '<p>' . t('This page provides the interface for adding new forms to use inline errors. Just add the form_id of the forms you wish to alter. The default settings can be overridden on a per form basis.') . '</p>';
}
}
/**
* Menu loader function to fetch a form id.
*/
function ife_form_id_load($form_id) {
$form_ids = ife_load_form_ids();
if (array_key_exists($form_id, $form_ids)) {
return $form_ids[$form_id];
}
else {
return FALSE;
}
}
/**
* Implementation of hook_theme().
*/
function ife_theme() {
return array(
'ife_settings_form_ids' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'ife.theme.inc',
),
'ife_form_element' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'ife.theme.inc',
'path' => drupal_get_path('module', 'ife'),
'template' => 'ife-form-element',
),
);
}
/**
* Load all form ids from the data base
*/
function ife_load_form_ids() {
static $ife_form_ids;
if ($ife_form_ids) {
$form_ids = $ife_form_ids;
}
else {
$form_ids = cache_get('ife_form_ids', 'cache');
$form_ids = $form_ids ? $form_ids->data : NULL;
}
if (empty($form_ids)) {
$result = db_query("SELECT form_id, field_types, status, display FROM {ife} ORDER BY form_id");
$form_ids = array();
while ($form_id = db_fetch_object($result)) {
$form_ids[$form_id->form_id] = $form_id;
}
cache_set('ife_form_ids', $form_ids, 'cache');
}
$ife_form_ids = $form_ids;
return $ife_form_ids;
}
/**
* Helper function to determine the display settings of a form
*/
function ife_form_id_display($form_id) {
if ($form_id->display == -1) {
$display = variable_get('ife_display', 1);
}
else {
$display = $form_id->display;
}
return $display;
}
/**
* Implementation of hook_form_alter().
*/
function ife_form_alter(&$form, $form_state, $form_id) {
$ife_options = ife_form_id_load($form_id);
if (!empty($ife_options->status) && $ife_options->status) {
$display = ife_form_id_display($ife_options);
ife_filter_form($form, $display);
//if a general message is set run the validator
if ($display == 1) {
$form['#validate'][] = 'ife_form_validator';
}
}
//print form_ids
if (variable_get('ife_show_form_ids', 0)) {
$form['ife_form_id'] = array(
'#type' => 'item',
'#value' => t('Form ID: @form_id', array(
'@form_id' => $form_id,
)),
'#weight' => -1000,
'#access' => user_access('administer inline form errors'),
);
}
}
/**
* Function to set the general error mesage if set
*/
function ife_form_validator($form, &$form_state) {
$form_errors = form_get_errors();
if (!empty($form_errors)) {
$message = filter_xss_admin(variable_get('ife_general_message', 'Please correct all highlighted errors and try again.'));
drupal_set_message($message, 'error');
}
}
/**
* Function to recursivly go through a form and alter all valid field type elements
*/
function ife_filter_form(&$form, $display) {
//get all possible children
$keys = element_children($form);
$field_types = ife_field_types();
foreach ($keys as $key) {
$element_type = isset($form[$key]['#type']) ? $form[$key]['#type'] : NULL;
if (in_array($element_type, $field_types)) {
ife_alter_form_element($form[$key], $display);
}
elseif ($element_type == 'fieldset' || !$element_type) {
ife_filter_form($form[$key], $display);
}
}
}
/**
* Function to add our custom theme to the element
*/
function ife_alter_form_element(&$element, $display) {
// Keep an existing theme function for use in ife theme preprocessing.
if (!empty($element['#theme'])) {
$element['#original_theme'] = $element['#theme'];
}
$element['#theme'] = 'ife_form_element';
//resend the element type, drupal sets the #type marker to markup
$element['#field_type'] = $element['#type'];
//add display type to element to avoid extra queries
$element['#display_type'] = $display;
}
/**
* Function to determine element errors
*/
function ife_element_get_error($element, $debug = FALSE) {
//check for errors and settings
if ($error_message = form_get_error($element)) {
//get error id
$error_id = array_search($error_message, $_SESSION['messages']['error']);
if ($error_id !== FALSE) {
if ($element['#display_type'] !== 0) {
unset($_SESSION['messages']['error'][$error_id]);
$_SESSION['messages']['error'] = array_values($_SESSION['messages']['error']);
}
if (count($_SESSION['messages']['error']) <= 0) {
unset($_SESSION['messages']['error']);
}
//return error message
return $error_message;
}
}
}
/**
* Helper function that identifies the different field types default in drupal
*/
function ife_field_types() {
$expandable = ife_expandable_field_types();
$extra = array(
'checkbox',
'file',
'password',
'radio',
'select',
'textarea',
'textfield',
'weight',
'webform_email',
);
return array_merge($expandable, $extra);
}
/**
* Helper function that identifies the different expandable field types default in drupal
*/
function ife_expandable_field_types() {
$core = array(
'checkboxes',
'date',
'password_confirm',
'radios',
);
$cck = array(
'date_combo',
'date_popup',
'date_repeat_rrule',
'email_textfield',
'filefield_widget',
'imagefield_widget',
'link',
'nodereference_select',
'nodereference_buttons',
'nodereference_autocomplete',
'number',
'optionwidgets_select',
'optionwidgets_buttons',
'optionwidgets_onoff',
'text_textfield',
'text_textarea',
'userreference_select',
'userreference_buttons',
'userreference_autocomplete',
'userreference_formatter_default',
'userreference_formatter_plain',
);
$contrib = array(
'captcha',
);
return array_merge($core, $cck, $contrib);
}
Functions
Name![]() |
Description |
---|---|
ife_alter_form_element | Function to add our custom theme to the element |
ife_element_get_error | Function to determine element errors |
ife_expandable_field_types | Helper function that identifies the different expandable field types default in drupal |
ife_field_types | Helper function that identifies the different field types default in drupal |
ife_filter_form | Function to recursivly go through a form and alter all valid field type elements |
ife_form_alter | Implementation of hook_form_alter(). |
ife_form_id_display | Helper function to determine the display settings of a form |
ife_form_id_load | Menu loader function to fetch a form id. |
ife_form_validator | Function to set the general error mesage if set |
ife_help | Implementation of hook_help(). |
ife_load_form_ids | Load all form ids from the data base |
ife_menu | Implementation of hook_menu(). |
ife_perm | Implementation of hook_perm(). |
ife_theme | Implementation of hook_theme(). |