View source
<?php
function formdefaults_perm() {
return array(
'change form labels',
);
}
function formdefaults_help($section) {
switch ($section) {
case 'admin/modules#description':
return t("Lets the site administrator edit the description, title and markup attributes of a form.");
}
}
function formdefaults_menu() {
global $user;
$items = array();
$items['formdefaults'] = array(
'title' => t('Edit Form'),
'description' => t('Edit a form'),
'page callback' => 'formdefaults_edit',
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
'file' => 'formdefaults.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/build/formdefaults'] = array(
'title' => t('Form Labels and Text'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'formdefaults_enable',
),
'description' => t('Turns on or off the editing controls to allow editing form labels and descriptions'),
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'formdefaults.admin.inc',
);
$items['admin/build/formdefaults/control'] = array(
'title' => t('Control'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/build/formdefaults/manage'] = array(
'title' => t('Manage'),
'page arguments' => array(
'formdefaults_manage',
),
'type' => MENU_LOCAL_TASK,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/build/formdefaults/export'] = array(
'title' => t('Export'),
'page arguments' => array(
'formdefaults_export',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/build/formdefaults/import'] = array(
'title' => t('Import'),
'page arguments' => array(
'formdefaults_import',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
return $items;
}
function _formdefaults_addon_fields($form_def, &$form) {
if ($form_def['formdefaults_header']) {
$form['formdefaults_header'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => -50,
);
}
if ($form_def['formdefaults_footer']) {
$form['formdefaults_footer'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => 50,
);
}
foreach ((array) $form_def['#formdefaults_addon_fields'] as $key => $field) {
$form[$key] = $field;
}
}
function formdefaults_form_alter(&$form, $form_state, $formid) {
$savedform = formdefaults_getform($formid);
_formdefaults_addon_fields($savedform, $form);
$enabled = $_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';
$formfieldsarray = array();
if (user_access('change form labels') && $enabled) {
$forms = $_SESSION['formdefaults_forms'];
foreach ($form as $fieldname => $value) {
_formdefaults_get_field($formid, $fieldname, $form[$fieldname], $formfieldsarray);
}
$forms[$formid] = $formfieldsarray;
$_SESSION['formdefaults_forms'] = $forms;
}
if ($savedform) {
foreach ($form as $fieldname => $value) {
formdefaults_alterform($formid, $fieldname, $form[$fieldname], $savedform);
}
}
if ($enabled) {
$form['formdefaults_edit_form'] = array(
'#type' => 'markup',
'#weight' => -50,
'#value' => '<p>' . l('[' . t('edit form') . ']', 'formdefaults/' . $formid) . '</p>',
);
}
}
function _formdefaults_get_field($formid, $fieldname, &$fieldvalue, &$fieldarray) {
if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {
$query = array();
$query['destination'] = $_GET['q'];
$query = drupal_query_string_encode($query);
$type = $fieldvalue['#type'];
$fieldarray[$fieldname]['type'] = $type;
if ($type != 'submit' && $type != 'hidden') {
if (array_key_exists('#weight', $fieldvalue)) {
$fieldarray[$fieldname]['weight'] = $fieldvalue['#weight'];
}
else {
$fieldarray[$fieldname]['weight'] = 'unset';
}
if ($type == 'markup') {
$fieldarray[$fieldname]['value'] = $fieldvalue['#value'];
$fieldvalue['#value'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
'query' => $query,
)) . ' ' . $fieldvalue['#value'];
}
else {
$fieldarray[$fieldname]['title'] = $fieldvalue['#title'];
$fieldarray[$fieldname]['description'] = $fieldvalue['#description'];
$fieldvalue['#description'] = l('[' . t('edit @type', array(
'@type' => $fieldvalue['#type'],
)) . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
'query' => $query,
)) . ' ' . $fieldvalue['#description'];
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
_formdefaults_get_field($formid, $key, $fieldvalue[$key], $fieldarray);
}
}
}
}
function _formdefaults_hide_field(&$fieldvalue) {
switch ($fieldvalue['#type']) {
case "fieldset":
case "markup":
$h_type = 'value';
$fieldvalue['#value'] = '';
break;
default:
$h_type = 'value';
break;
}
$fieldvalue['#type'] = $h_type;
if (!array_key_exists('#value', $fieldvalue)) {
$fieldvalue['#value'] = $fieldvalue['#default_value'];
}
}
function _formdefaults_hide_recursive(&$form_element) {
foreach ((array) $form_element as $key => $field) {
if (strpos($key, '#') === FALSE) {
_formdefaults_hide_recursive($form_element[$key]);
}
}
_formdefaults_hide_field($form_element);
}
function formdefaults_alterform($formid, $fieldname, &$fieldvalue, $formreplace) {
$query = array();
$query['destination'] = $_GET['q'];
$query = drupal_query_string_encode($query);
$enabled = $_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';
if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {
if (is_array($formreplace[$fieldname])) {
$fo = $formreplace[$fieldname];
if ($formreplace[$fieldname]['hide_it']) {
_formdefaults_hide_recursive($fieldvalue);
}
if ($fo['weight'] != 'unset') {
$fieldvalue['#weight'] = $fo['weight'];
}
if ($fieldvalue['#type'] == 'fieldset' && array_key_exists('collapsible', $fo)) {
$fieldvalue['#collapsible'] = $fo['collapsible'];
}
if ($fieldvalue['#type'] == 'fieldset' && array_key_exists('collapsed', $fo)) {
$fieldvalue['#collapsed'] = $fo['collapsed'];
}
if ($fieldvalue['#type'] == 'markup') {
$fieldvalue['#value'] = check_markup($formreplace[$fieldname]['value'], $formreplace[$fieldname]['format'], false);
}
else {
$fieldvalue['#title'] = $fo['title'];
$fieldvalue['#description'] = $fo['description'];
}
if (user_access('change form labels') && $enabled) {
if ($fieldvalue['#type'] != 'markup') {
$fieldvalue['#description'] = l('[' . t('edit') . ' ' . t($fieldvalue['#type']) . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
'query' => $query,
)) . ' ' . $fieldvalue['#description'];
}
else {
$fieldvalue['#value'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
'query' => $query,
)) . ' ' . $fieldvalue['#value'];
}
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
formdefaults_alterform($formid, $key, $fieldvalue[$key], $formreplace);
}
}
}
}
function formdefaults_edit($formid = '', $field = '') {
if (!$field) {
return drupal_get_form('formdefaults_edit_form');
}
elseif ($formid) {
return drupal_get_form('formdefaults_edit_field');
}
else {
drupal_not_found();
}
}
function formdefaults_getform($formid) {
$result = db_query('SELECT * FROM {formdefaults_forms} WHERE formid=\'%s\'', $formid);
$form = db_fetch_object($result);
if ($form) {
$formarray = unserialize($form->formdata);
}
else {
$formarray = array();
}
return $formarray;
}
function formdefaults_enable() {
$form['instructions'] = array(
'#type' => markup,
'#value' => '<p>To alter the default labels and text descriptions associated with a form, enable the form ' . 'defaults editor below. Navigate to the form you wish to modify and click the [edit] link on ' . 'the field you want to edit. You\'ll be allowed to edit field titles and markup fields ' . '(like this one), as well as the textual descriptions for each field. It\'s also possible to ' . 'add form elements and hide those already present.</p>' . '<p>Use the controls above to manage those forms you\'ve modified.</p>',
);
if ($_SESSION['formdefaults_enabled']) {
$form['disable'] = array(
'#type' => 'submit',
'#value' => 'disable',
);
}
else {
$form['enable'] = array(
'#type' => 'submit',
'#value' => 'enable',
);
}
return $form;
}
function formdefaults_enable_submit($formid, $form_state) {
$editor_enabled = $_SESSION['formdefaults_enabled'];
if ($editor_enabled) {
$_SESSION['formdefaults_enabled'] = false;
drupal_set_message('Form defaults editor is now disabled');
}
else {
$_SESSION['formdefaults_enabled'] = true;
drupal_set_message('Form defaults editor is now enabled');
}
return $output;
}