You are here

function _name_field_widget_form in Name Field 7

Implements hook_field_widget_form().

1 call to _name_field_widget_form()
name_field_widget_form in ./name.module
Implements hook_field_widget_form().

File

includes/name.content.inc, line 446
Provides additional Field functionality for the name module.

Code

function _name_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  form_load_include($form_state, 'inc', 'name', 'includes/name.content');
  $widget = $instance['widget'];
  _name_defaults($instance['settings'], 'instance_settings');
  _name_defaults($field['settings'], 'settings');
  $fs = $field['settings'];
  $ws = $instance['settings'];
  $ws += array(
    'components' => array(),
    'minimum_components' => array(),
  );

  // Use populated overrides if present.
  $fs['components'] = name_get_instance_components($fs['components'], $ws['components']);
  $fs['minimum_components'] = name_get_instance_components($fs['minimum_components'], $ws['minimum_components']);
  $element += array(
    '#type' => 'name_element',
    '#title' => check_plain($instance['label']),
    '#label' => $instance['label'],
    '#components' => array(),
    '#minimum_components' => array_filter($fs['minimum_components']),
    '#allow_family_or_given' => !empty($fs['allow_family_or_given']),
    '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
    '#field' => $field,
    '#credentials_inline' => empty($ws['credentials_inline']) ? 0 : 1,
    '#component_css' => empty($ws['component_css']) ? '' : $ws['component_css'],
    '#component_layout' => empty($ws['component_layout']) ? 'default' : $ws['component_layout'],
    '#show_component_required_marker' => !empty($ws['show_component_required_marker']),
  );

  // Ensure that we have a title for any validation.
  if (empty($element['#title'])) {
    $element['#title'] = check_plain($instance['label']);
    $element['#title_display'] = 'invisible';
  }
  $autocomplete_sources = module_invoke_all('name_data_sources');
  $components = array_filter($fs['components']);
  foreach (_name_translations() as $key => $title) {
    if (in_array($key, $components)) {
      $element['#components'][$key]['type'] = 'textfield';
      $size = !empty($ws['size'][$key]) ? $ws['size'][$key] : 60;
      $title_display = isset($ws['title_display'][$key]) ? $ws['title_display'][$key] : 'description';
      $component_label = empty($ws['labels'][$key]) ? $fs['labels'][$key] : $ws['labels'][$key];
      $element['#components'][$key]['title'] = check_plain($component_label);
      $element['#components'][$key]['title_display'] = $title_display;
      $element['#components'][$key]['size'] = $size;
      $element['#components'][$key]['maxlength'] = !empty($fs['max_length'][$key]) ? $fs['max_length'][$key] : 255;
      $field_type = 'text';
      foreach ($autocomplete_sources as $ac_source => $ac_settings) {
        if (empty($ac_settings['components']) || in_array($key, $ac_settings['components'])) {
          if (!empty($ac_settings['list callback'])) {
            $field_type = 'select';
          }
        }
      }

      // Provides backwards compatibility with Drupal 6 modules.
      $field_type = isset($ws['field_type'][$key]) ? $ws['field_type'][$key] : (isset($ws[$key . '_field']) ? $ws[$key . '_field'] : $field_type);
      if ($field_type == 'select') {
        $element['#components'][$key]['type'] = 'select';
        $element['#components'][$key]['size'] = 1;

        // This is a bit bung, we can only use one, but more than one
        // source could be avialable. Pick the first one that returns
        // any data.
        $options = array();
        foreach ($autocomplete_sources as $ac_source => $ac_settings) {
          if (empty($ac_settings['components']) || in_array($key, $ac_settings['components'])) {
            if (!empty($ac_settings['list callback']) && function_exists($ac_settings['list callback'])) {
              $func = $ac_settings['list callback'];
              $arguments = empty($ac_settings['list arguments']) ? array() : $ac_settings['list arguments'];
              if ($_options = $func($field, $key, $arguments)) {
                $options = $_options;
                break;
              }
            }
          }
        }
        $element['#components'][$key]['options'] = $options;
      }
      elseif ($field_type == 'autocomplete') {
        if ($sources = $field['settings']['autocomplete_source'][$key]) {
          $sources = array_filter($sources);
          if (!empty($sources)) {
            $element['#components'][$key]['autocomplete'] = 'name/autocomplete/' . str_replace('_', '-', $field['field_name']) . '/' . $key;
          }
        }
      }
      if (isset($ws['inline_css'][$key]) && drupal_strlen($ws['inline_css'][$key])) {
        $element['#components'][$key]['attributes'] = array(
          'style' => $ws['inline_css'][$key],
        );
      }
    }
    else {
      $element['#components'][$key]['exclude'] = TRUE;
    }
  }
  return $element;
}