View source
<?php
function formdefaults_permission() {
return array(
'change form labels' => array(
'title' => t('Change defaults labels and text an all drupal forms'),
),
);
}
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' => 'Edit Form',
'description' => '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/structure/formdefaults'] = array(
'title' => 'Form Labels and Text',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'formdefaults_enable',
),
'description' => '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/structure/formdefaults/control'] = array(
'title' => 'Control',
'type' => MENU_DEFAULT_LOCAL_TASK,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/structure/formdefaults/manage'] = array(
'title' => 'Manage',
'page arguments' => array(
'formdefaults_manage',
),
'type' => MENU_LOCAL_TASK,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/structure/formdefaults/export'] = array(
'title' => 'Export',
'page arguments' => array(
'formdefaults_export',
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'access callback' => 'user_access',
'access arguments' => array(
'change form labels',
),
);
$items['admin/structure/formdefaults/import'] = array(
'title' => '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,
'#markup' => '<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'];
if (is_array($query)) {
$query = drupal_http_build_query($query);
$options['query'] = $query;
}
$options = array();
$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['#markup'];
$fieldvalue['#markup'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, $options) . ' ' . @$fieldvalue['#markup'];
}
else {
$fieldarray[$fieldname]['title'] = @$fieldvalue['#title'];
$fieldarray[$fieldname]['description'] = @$fieldvalue['#description'];
$fieldvalue['#description'] = l('[' . t('edit @type', array(
'@type' => @$fieldvalue['#type'],
)) . ']', 'formdefaults/' . $formid . '/' . $fieldname, $options) . ' ' . @$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'];
$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['#markup'] = check_markup($formreplace[$fieldname]['value'], @$formreplace[$fieldname]['input_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['#markup'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
'query' => $query,
)) . ' ' . @$fieldvalue['#markup'];
}
}
}
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 = :formid', array(
':formid' => $formid,
));
$formarray = array();
foreach ($result as $form) {
$formarray = unserialize($form->formdata);
}
return $formarray;
}
function formdefaults_enable() {
$form['instructions'] = array(
'#type' => 'markup',
'#markup' => '<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(t('Form defaults editor is now disabled'));
}
else {
$_SESSION['formdefaults_enabled'] = TRUE;
drupal_set_message(t('Form defaults editor is now enabled'));
}
}