You are here

function formdefaults_alterform in Form Defaults 7

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. 5.2 formdefaults.module \formdefaults_alterform()
  5. 6.2 formdefaults.module \formdefaults_alterform()
  6. 6 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 236

Code

function formdefaults_alterform($formid, $fieldname, &$fieldvalue, $formreplace) {

  // Determine return location for links.
  $query = array();
  $query['destination'] = $_GET['q'];
  $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['#markup'] = check_markup($formreplace[$fieldname]['value'], @$formreplace[$fieldname]['input_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['#markup'] = l('[' . t('edit markup') . ']', 'formdefaults/' . $formid . '/' . $fieldname, array(
            'query' => $query,
          )) . ' ' . @$fieldvalue['#markup'];
        }
      }
    }

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