You are here

formdefaults.module in Form Defaults 6.2

File

formdefaults.module
View source
<?php

/**
 * Provides a mechanism to override the description and title fields on drupal forms.
 * Note that this is not too useful for node fields because they all are node_edit. 
**/

/**
 * Implementation of hook_perm().
 */
function formdefaults_perm() {
  return array(
    'change form labels',
  );
}

/**
 * Implementation of hook_help().
 */
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.");
  }
}

/**
* Implements hook_menu.
*
*/
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;
}

// Adds on add-on
function _formdefaults_addon_fields($form_def, &$form) {

  // Add a header and a footer to all forms for instructions
  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,
    );
  }

  // add on arbitrary fields created by the form editor.
  foreach ((array) $form_def['#formdefaults_addon_fields'] as $key => $field) {
    $form[$key] = $field;
  }
}

/**
 * implements hook_form_alter
 */
function formdefaults_form_alter(&$form, $form_state, $formid) {
  $savedform = formdefaults_getform($formid);
  _formdefaults_addon_fields($savedform, $form);
  $enabled = $_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';

  //If the user has the right privs, add the links to alter the form
  $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>',
    );
  }

  //drupal_set_message('<pre>'. print_r($form,1) .'</pre>');
}

/**
 * Recursively iterate through the form array to save  titles and descirptions.
 * The the resulting title/description array is intentionally flattened to make
 * indexing easier during the "replacement" portion of the code. 
 * Also tack on the [edit] link.
 * @param $formid the formid of the form we're altering
 * @param $fieldname the fieldname of the last form field we found.
 * @param $fieldvalue The array of field values that we found last.
 * @param $fieldarray We store the array of titles and desciptions in this array for storing later.
 */
function _formdefaults_get_field($formid, $fieldname, &$fieldvalue, &$fieldarray) {
  if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {

    // Determine return location for links.
    $query = array();
    $query['destination'] = $_GET['q'];
    $query = drupal_query_string_encode($query);
    $type = $fieldvalue['#type'];
    $fieldarray[$fieldname]['type'] = $type;

    // Skip submit and hidden fields cause they're too dangerous to alter.
    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);
      }
    }
  }
}

/**
 * Hide a field by turning it into a value or markup field. 
 *
 * @param array $fieldvalue
 */
function _formdefaults_hide_field(&$fieldvalue) {

  //drupal_set_message($fieldname.":".$formreplace[$fieldname]['hide_it']);
  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'];
  }
}

/**
 * Recursively iterate through all form elements and hide all children. 
 *
 * @param array $form_element
 */
function _formdefaults_hide_recursive(&$form_element) {

  // recursively iterate through all children and hide them.
  foreach ((array) $form_element as $key => $field) {
    if (strpos($key, '#') === FALSE) {
      _formdefaults_hide_recursive($form_element[$key]);
    }
  }
  _formdefaults_hide_field($form_element);
}

/**
 * Alters the form based on the form replacement items passed.
 * @param $formid the form id of the form that we're altering
 * @param $fieldname the field name of the last field we found
 * @param $fieldvalues the field value array form the last field we found.
 * @param $formreplace the replacement data for the form. 
*/
function formdefaults_alterform($formid, $fieldname, &$fieldvalue, $formreplace) {

  // Determine return location for links.
  $query = array();
  $query['destination'] = $_GET['q'];
  $query = drupal_query_string_encode($query);
  $enabled = $_SESSION['formdefaults_enabled'] && $formid != 'formdefaults_edit';

  // Need to make sure we aren't processing attributes.
  if (is_array($fieldvalue) && !(strpos($fieldname, '#') === 0)) {
    if (is_array($formreplace[$fieldname])) {

      // perform the replacement.
      // fo stands for field override
      $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'];
      }

      // add back on the edit control if appropriate.
      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'];
        }
      }
    }

    // If this is a fieldset, we're going to need to recurse.
    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();
  }
}

/**
 * Retrieve from alterations from the database
 * @formid the form id to retrieve.
 */
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;
}

/**
 *Toggle the form editor controls on and off
 */
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;
}

Functions

Namesort descending Description
formdefaults_alterform Alters the form based on the form replacement items passed.
formdefaults_edit
formdefaults_enable
formdefaults_enable_submit Toggle the form editor controls on and off
formdefaults_form_alter implements hook_form_alter
formdefaults_getform Retrieve from alterations from the database @formid the form id to retrieve.
formdefaults_help Implementation of hook_help().
formdefaults_menu Implements hook_menu.
formdefaults_perm Implementation of hook_perm().
_formdefaults_addon_fields
_formdefaults_get_field Recursively iterate through the form array to save titles and descirptions. The the resulting title/description array is intentionally flattened to make indexing easier during the "replacement" portion of the code. Also tack on the [edit]…
_formdefaults_hide_field Hide a field by turning it into a value or markup field.
_formdefaults_hide_recursive Recursively iterate through all form elements and hide all children.