You are here

function formdefaults_alterform in Form Defaults 8

Same name and namespace in other branches
  1. 5.3 formdefaults.module \formdefaults_alterform()
  2. 5 formdefaults.module \formdefaults_alterform()
  3. 5.2 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'];
  $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.
      $url
        ->setOption('query', $query);
      if (Drupal::currentUser()
        ->hasPermission('change form labels') && $enabled) {
        if ($fieldvalue['#type'] != 'markup') {
          $fieldvalue['#suffix'] = Link::createFromRoute('[' . t('edit') . ' ' . t($fieldvalue['#type']) . ']', 'formdefaults.edit', [
            'formid' => $formid,
            'field' => $fieldname,
          ])
            ->toString();
        }
        else {
          $link = Link::createFromRoute('[' . t('edit markup') . ']', 'formdefaults.edit', [
            'formid' => $formid,
            'field' => $fieldname,
          ])
            ->toString();
          $fieldvalue['#markup'] = $link . ' ' . @$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);
      }
    }
  }
}