function name_widget in Name Field 6
Implementation of hook_widget().
File
- ./
name.module, line 993 - Defines an API for displaying and inputing names.
Code
function name_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$widget = $field['widget'];
_name_defaults($widget, 'instance_settings');
_name_defaults($field, 'field_settings');
$fs = $field['field_settings'];
$ws = $widget['instance_settings'];
$element = array(
'#type' => 'name_element',
'#title' => $widget['label'],
'#label' => $widget['label'],
'#components' => array(),
'#minimum_components' => array_filter($fs['minimum_components']),
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
);
// Field only property. Do the calculations here.
$inline_css = $ws['inline_css'];
$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';
$element['#components'][$key]['title'] = check_plain($fs['labels'][$key]);
$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;
if (isset($ws[$key . '_field']) && $ws[$key . '_field'] == 'select') {
$element['#components'][$key]['type'] = 'select';
$element['#components'][$key]['size'] = 1;
$options = array_filter(explode("\n", $fs[$key . '_options']));
foreach ($options as $index => $opt) {
if (preg_match('/^\\[vocabulary:([0-9]{1,})\\]/', trim($opt), $matches)) {
unset($options[$index]);
$max_length = isset($fs['max_length'][$key]) ? $fs['max_length'][$key] : 255;
foreach (taxonomy_get_tree($matches[1]) as $term) {
if (drupal_strlen($term->name) <= $max_length) {
$options[] = $term->name;
}
}
}
}
// Options could come from multiple sources, filter duplicates.
$options = array_unique($options);
if ($fs && isset($fs['sort_options']) && !empty($fs['sort_options'][$key])) {
natcasesort($options);
}
$default = FALSE;
foreach ($options as $index => $opt) {
if (strpos($opt, '--') === 0) {
unset($options[$index]);
$default = substr($opt, 2);
}
}
$options = drupal_map_assoc(array_map('trim', $options));
if ($default !== FALSE) {
$options = array(
'' => $default,
) + $options;
}
$element['#components'][$key]['options'] = $options;
}
elseif (isset($ws[$key . '_field']) && $ws[$key . '_field'] == 'autocomplete') {
// TODO $element['#components'][$key]['autocomplete'] = '';
}
if (isset($ws['inline_css_enabled'][$key]) && $ws['inline_css_enabled'][$key]) {
$width = $size * $inline_css['multiplier'];
switch ($inline_css['unit']) {
case 'px':
$width = round($width) . 'px';
break;
case 'em':
default:
$width = round($width, 1) . 'em';
break;
}
$element['#components'][$key]['attributes'] = array();
$element['#components'][$key]['attributes']['style'] = 'width:' . $width . ';';
}
}
else {
$element['#components'][$key]['exclude'] = TRUE;
}
}
// Used so that hook_field('validate') knows where to
// flag an error in deeply nested forms.
if (empty($form['#parents'])) {
$form['#parents'] = array();
}
$element['_error_element'] = array(
'#type' => 'value',
'#value' => implode('][', $form['#parents']),
);
return $element;
}