You are here

function _webform_render_name in Name Field 7

Implements Webform callback _webform_render_COMPONENT().

File

includes/webform.components.inc, line 203
Name Field module name component for the Webform module.

Code

function _webform_render_name($name, $value = NULL, $filter = TRUE) {
  global $user;
  $enabled_components = array_filter($name['extra']['enabled_components']);
  foreach ($name['extra']['components'] as $key => &$info) {
    if ($key == 'title' || $key == 'generational') {
      $field = array(
        'settings' => array(
          $key . '_options' => $info['options'],
          'max_length' => array(
            $key => $info['max_length'],
          ),
          'sort_options' => array(
            $key => $info['sort_options'],
          ),
        ),
      );
      $info['options'] = name_field_get_options($field, $key);
    }
    $info['maxlength'] = $info['max_length'];
    if (empty($enabled_components[$key])) {
      $info['exclude'] = TRUE;
    }
  }
  $default_value = array();
  if (empty($value)) {
    $found = FALSE;
    if (!empty($name['extra']['use_real_name'])) {
      if ($user_field_name = variable_get('name_user_preferred', '')) {
        name_username_alter_preload($user);
        if ($items = field_get_items('user', $user, $user_field_name)) {
          $item = reset($items);
          $found = TRUE;
          foreach ($enabled_components as $component_key) {
            $default_value[$component_key] = isset($item[$component_key]) ? $item[$component_key] : '';
          }
        }
      }
    }
    if (!$found) {
      $token_data = array(
        'user' => $user,
      );
      foreach ($enabled_components as $component_key) {
        if (!isset($name['extra']['components'][$component_key]['default_value'])) {
          $name['extra']['components'][$component_key]['default_value'] = '';
        }
        $default_value[$component_key] = $name['extra']['components'][$component_key]['default_value'];
        if (strlen($default_value[$component_key])) {
          $default_value[$component_key] = token_replace($default_value[$component_key], $token_data, array(
            'clear' => TRUE,
            'sanitize' => FALSE,
          ));
        }
      }
    }
    foreach ($enabled_components as $component_key) {
      $max_length = $name['extra']['components'][$component_key]['max_length'];
      if (drupal_strlen($default_value[$component_key]) > $max_length) {
        $default_value[$component_key] = drupal_substr($default_value[$component_key], 0, $max_length);
      }
    }
  }
  $form_item = array(
    '#type' => 'name_element',
    '#title' => $filter ? _webform_filter_xss($name['name']) : $name['name'],
    '#required' => $name['required'],
    '#weight' => $name['weight'],
    '#description' => $filter ? _webform_filter_descriptions($name['extra']['description']) : $name['extra']['description'],
    '#default_value' => $filter ? _webform_filter_values($name['value']) : $name['value'],
    '#prefix' => '<div class="webform-component-name" id="webform-component-' . $name['form_key'] . '">',
    '#suffix' => '</div>',
    '#minimum_components' => $name['extra']['minimum_components'],
    '#components' => $name['extra']['components'],
    '#default_value' => empty($value) ? $default_value : $value,
    '#theme_wrappers' => array(
      'webform_element',
    ),
  );
  return $form_item;
}