You are here

function name_element_pre_render in Name Field 6

Same name and namespace in other branches
  1. 8 name.module \name_element_pre_render()
  2. 7 name.module \name_element_pre_render()

This function forces the error class attribute onto each of the empty element components as required, themes the element and controls the title display.

TODO: There is a lot of WTF here. Why does this need such a hack to propagate the error class down? Is the error element name wrong??

1 string reference to 'name_element_pre_render'
name_element_expand in ./name.module
The #process callback to create the element.

File

./name.module, line 457
Defines an API for displaying and inputing names.

Code

function name_element_pre_render($element) {
  $error_element = empty($element['_error_element']['#value']) ? '' : $element['_error_element']['#value'] . '][';
  $error_element .= $element['#name'];
  $errors = (array) form_set_error();
  foreach ($errors as $error_key => $error) {
    if (strpos($error_key, $error_element) === 0) {
      foreach (_name_translations() as $key => $title) {
        if (isset($element[$key]) && empty($element[$key]['#value'])) {
          $element[$key]['#attributes']['class'] .= ' error';
        }
      }
    }
  }

  // Add a wrapper to single fields that have a description to prevent wrapping.
  if (!empty($element['#description']) && !empty($element['#description'])) {
    $field = NULL;
    if (isset($element['#field_name']) && isset($element['#type_name'])) {
      $field = content_fields($element['#field_name'], $element['#type_name']);
    }
    if (!$field || !$field['multiple']) {
      $element['_name'] = array(
        '#prefix' => '<div class="clear-block">',
        '#suffix' => '</div>',
      );
      foreach (_name_translations() as $key => $title) {
        if (isset($element[$key])) {
          $element['_name'][$key] = $element[$key];
          unset($element[$key]);
        }
      }
      $element['_description'] = array(
        '#value' => '<div class="clear-block description">' . $element['#description'] . '</div>',
      );
      unset($element['#description']);
    }
  }
  foreach (_name_translations() as $key => $title) {
    if (isset($element['_name']) && isset($element['_name'][$key])) {
      if (isset($element['_name'][$key]['#title_display'])) {
        if ($element['_name'][$key]['#title_display'] == 'description') {
          $element['_name'][$key]['#description'] = $element['_name'][$key]['#title'];
        }
        if ($element['_name'][$key]['#title_display'] == 'description' || $element['_name'][$key]['#title_display'] == 'none') {
          $element['_name'][$key]['#title'] = NULL;
        }
      }
    }
    elseif (isset($element[$key])) {
      if (isset($element[$key]['#title_display'])) {
        if ($element[$key]['#title_display'] == 'description') {
          $element[$key]['#description'] = $element[$key]['#title'];
        }
        if ($element[$key]['#title_display'] == 'description' || $element[$key]['#title_display'] == 'none') {
          $element[$key]['#title'] = NULL;
        }
      }
    }
  }
  return $element;
}