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($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'formdefaults',
'title' => t('Edit Form'),
'description' => t('Edit a form'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'formdefaults_edit',
),
'access' => user_access('change form labels'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/formdefaults',
'title' => t('Form Labels and Text'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'formdefaults_enable',
),
'description' => t('Turns on or off the editing controls to allow editing form labels and descriptions'),
'access' => user_access('change form labels'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'admin/build/formdefaults/control',
'title' => 'Control',
'callback' => 'formdefaults_admin',
'callback arguments' => array(
'enable',
),
'access' => user_access('change form labels'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/formdefaults/manage',
'title' => 'Manage',
'callback' => 'formdefaults_admin',
'callback arguments' => array(
'manage',
),
'access' => user_access('change form labels'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/formdefaults/import',
'title' => 'Import',
'callback' => 'formdefaults_admin',
'callback arguments' => array(
'import',
),
'access' => user_access('change form labels'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
$items[] = array(
'path' => 'admin/build/formdefaults/export',
'title' => 'Export',
'callback' => 'formdefaults_admin',
'callback arguments' => array(
'export',
),
'access' => user_access('change form labels'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
}
return $items;
}
function formdefaults_form_alter($formid, &$form) {
$enabled = $_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';
$form['formdefaults_header'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => -10,
);
$form['formdefaults_footer'] = array(
'#type' => 'markup',
'#value' => '',
'#weight' => 10,
);
$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;
}
$savedform = formdefaults_getform($formid);
if ($savedform) {
foreach ($form as $fieldname => $value) {
formdefaults_alterform($formid, $fieldname, $form[$fieldname], $savedform);
}
}
}
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') . ']', 'formdefaults/' . $formid . '/' . $fieldname, null, $query) . ' ' . $fieldvalue['#value'];
}
else {
$fieldarray[$fieldname]['title'] = $fieldvalue['#title'];
$fieldarray[$fieldname]['description'] = $fieldvalue['#description'];
$fieldvalue['#description'] = l('[' . t('edit') . ']', 'formdefaults/' . $formid . '/' . $fieldname, null, $query) . ' ' . $fieldvalue['#description'];
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
_formdefaults_get_field($formid, $key, $fieldvalue[$key], $fieldarray);
}
}
}
}
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])) {
if ($formreplace[$fieldname]['hide_it'] && !$_SESSION['formdefaults_enabled']) {
$fieldvalue['#type'] = 'hidden';
$fieldvalue['#value'] = $fieldvalue['#default_value'];
}
if ($formreplace[$fieldname]['weight'] != 'unset') {
$fieldvalue['#weight'] = $formreplace[$fieldname]['weight'];
}
if ($fieldvalue['#type'] == 'markup') {
$fieldvalue['#value'] = check_markup($formreplace[$fieldname]['value'], $formreplace[$fieldname]['format'], false);
}
else {
$fieldvalue['#title'] = $formreplace[$fieldname]['title'];
$fieldvalue['#description'] = $formreplace[$fieldname]['description'];
}
if (user_access('change form labels') && $enabled) {
if ($fieldvalue['#type'] != 'markup') {
$fieldvalue['#description'] = l('[' . t('edit') . ']', 'formdefaults/' . $formid . '/' . $fieldname, '', $query) . ' ' . $fieldvalue['#description'];
}
else {
$fieldvalue['#value'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, '', $query) . ' ' . $fieldvalue['#value'];
}
}
}
foreach ($fieldvalue as $key => $value) {
if (!(strpos($key, '#') === 0)) {
formdefaults_alterform($formid, $key, $fieldvalue[$key], $formreplace);
}
}
}
}
function formdefaults_edit() {
$form_array = $_SESSION['formdefaults_forms'];
$formid = arg(1);
$fieldname = arg(2);
$originalfields = $form_array[$formid][$fieldname];
$savedform = formdefaults_getform($formid);
$weight_range = range(-10, 10);
$weights = array(
'unset' => 'unset',
);
foreach ($weight_range as $weight) {
$weights[(string) $weight] = (string) $weight;
}
if (is_array($savedform[$fieldname])) {
$formfields = array_merge($originalfields, $savedform[$fieldname]);
}
else {
$formfields = $originalfields;
}
$form['formid'] = array(
'#type' => 'hidden',
'#value' => $formid,
);
$form['fieldname'] = array(
'#type' => 'hidden',
'#value' => $fieldname,
);
$form['type'] = array(
'#type' => 'hidden',
'#title' => 'Field Type',
'#value' => $originalfields['type'],
);
$form['warning'] = array(
'#type' => 'markup',
'#value' => 'Some text to edit',
);
$form['hide_it'] = array(
'#type' => 'checkbox',
'#title' => 'Hide this field',
'#description' => 'Checking this box will convert the field to a hidden field.' . ' Fields will not actually be hidden while the form editor is enabled. ' . '(Otherwise you wouldn\'t be able to unhide them). ',
'#default_value' => $formfields['hide_it'],
);
if ($originalfields['type'] == 'markup') {
$form['value'] = array(
'#type' => 'textarea',
'#title' => 'Text or markup',
'#rows' => 30,
'#cols' => 80,
'#default_value' => $formfields['value'],
);
$form['format'] = filter_form($formfields['format']);
$form['value_original'] = array(
'#type' => item,
'#title' => 'Original value',
'#value' => $originalfields['value'],
);
}
else {
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Field Title',
'#default_value' => $formfields['title'],
);
$form['title_old'] = array(
'#type' => 'item',
'#title' => 'Original Title',
'#value' => $originalfields['title'],
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => 'Field Description',
'#default_value' => $formfields['description'],
'#rows' => 30,
'#cols' => 80,
);
$form['description_old'] = array(
'#type' => 'item',
'#title' => 'Original Description',
'#value' => $originalfields['description'],
);
}
$form['weight'] = array(
'#type' => 'select',
'#title' => 'Weight',
'#options' => $weights,
'#default_value' => $formfields['weight'],
'#description' => 'Higher values appear near at the top of the form, lower values at the bottom.',
);
$form['weight_old'] = array(
'#type' => 'item',
'#title' => 'Original Weight',
'#value' => $originalfields['weight'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
);
return $form;
}
function formdefaults_edit_submit($formid, $form_values) {
$formid = $form_values['formid'];
$fieldname = $form_values['fieldname'];
$formarray = formdefaults_getform($formid);
$baseform = $formarray;
if ($form_values['type'] == 'markup') {
$formarray[$fieldname]['value'] = $form_values['value'];
$formarray[$fieldname]['format'] = $form_values['format'];
}
else {
$formarray[$fieldname]['title'] = $form_values['title'];
$formarray[$fieldname]['description'] = $form_values['description'];
}
$formarray[$fieldname]['hide_it'] = $form_values['hide_it'];
$formarray[$fieldname]['weight'] = $form_values['weight'];
$formdata = serialize($formarray);
if ($baseform) {
db_query('update {formdefaults_forms} set formdata=\'%s\'
where formid=\'%s\'', $formdata, $formid);
}
else {
db_query('insert into {formdefaults_forms}(formid,formdata) values (\'%s\',\'%s\')', $formid, $formdata);
}
}
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 defaults associated with a form, enable the forms module below and look for the [edit] ' . 'controls in the description of each form field. Navigate to the form and click [edit] link on the field ' . 'you want to edit. You\'ll be allowed to edit field titles, markup fields (like this one) as well as the textual descriptions ' . 'with each field.</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_values) {
$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;
}
function formdefaults_admin($action = '') {
require_once 'formdefaults.admin.inc';
return drupal_get_form('formdefaults_' . $action);
}