You are here

function formdefaults_alterform in Form Defaults 5.2

Same name and namespace in other branches
  1. 8 formdefaults.module \formdefaults_alterform()
  2. 5.3 formdefaults.module \formdefaults_alterform()
  3. 5 formdefaults.module \formdefaults_alterform()
  4. 6.2 formdefaults.module \formdefaults_alterform()
  5. 6 formdefaults.module \formdefaults_alterform()
  6. 7 formdefaults.module \formdefaults_alterform()

Alters the form based on the form replacement items passed.

Parameters

$formid the form id of the form that we're altering:

$fieldname the field name of the last field we found:

$fieldvalues the field value array form the last field we found.:

$formreplace the replacement data for the form. :

1 call to formdefaults_alterform()
formdefaults_form_alter in ./formdefaults.module
implements hook_form_alter

File

./formdefaults.module, line 185

Code

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.
      if ($formreplace[$fieldname]['hide_it'] && !$_SESSION['formdefaults_enabled']) {

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

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

    // 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);
      }
    }
  }
}