name.content.inc in Name Field 7
Same filename and directory in other branches
Provides additional Field functionality for the name module.
Most of these functions are related to setting configuration for the field, instance and formatter.
File
includes/name.content.incView source
<?php
/**
* @file
* Provides additional Field functionality for the name module.
*
* Most of these functions are related to setting configuration for the field,
* instance and formatter.
*/
define('NAME_FIELD_TAXONOMY_OPTION_REGEX', '/^\\[vocabulary:([0-9a-z\\_]{1,})\\]/');
/* ----------------------------- Field Code --------------------------------- */
/**
* Implements hook_field_settings_form().
*/
function _name_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array(
'#tree' => TRUE,
);
$components = _name_translations();
$form['components'] = array(
'#type' => 'checkboxes',
'#title' => t('Components'),
'#default_value' => $settings['components'],
'#required' => TRUE,
'#description' => t('Only selected components will be activated on this field. All non-selected components / component settings will be ignored.'),
'#options' => $components,
'#element_validate' => array(
'_name_field_minimal_component_requirements',
),
);
$form['minimum_components'] = array(
'#type' => 'checkboxes',
'#title' => t('Minimum components'),
'#default_value' => $settings['minimum_components'],
'#required' => TRUE,
'#element_validate' => array(
'_name_field_minimal_component_requirements',
),
'#description' => t('The minimal set of components required before the field is considered completed enough to save.'),
'#options' => $components,
);
$form['labels'] = array();
$form['max_length'] = array();
$autocomplete_sources = module_invoke_all('name_data_sources');
$autocomplete_sources_options = array();
foreach ($autocomplete_sources as $ac_source => $ac_settings) {
if (!empty($ac_settings['autocomplete callback'])) {
$autocomplete_sources_options[$ac_source] = $ac_settings['name'];
}
}
$has_data = field_has_data($field);
foreach ($components as $key => $title) {
$min_length = 1;
if ($has_data) {
$min_length = $settings['max_length'][$key];
if ($field['storage']['type'] == 'field_sql_storage') {
try {
$table = 'field_data_' . $field['field_name'];
$column = $field['storage']['details']['sql'][FIELD_LOAD_CURRENT][$table][$key];
$min_length = db_query("SELECT MAX(CHAR_LENGTH({$column})) AS len FROM {$table}")
->fetchField();
if ($min_length < 1) {
$min_length = 1;
}
} catch (Exception $e) {
}
}
}
$form['max_length'][$key] = array(
'#type' => 'textfield',
'#title' => t('Maximum length for !title', array(
'!title' => $title,
)),
'#default_value' => $settings['max_length'][$key],
'#required' => TRUE,
'#size' => 10,
'#min_size' => $min_length,
'#description' => t('The maximum length of the field in characters. This must be between !min and 255.', array(
'!min' => $min_length,
)),
'#element_validate' => array(
'_name_validate_varchar_range',
),
);
$form['labels'][$key] = array(
'#type' => 'textfield',
'#title' => t('Label for !title', array(
'!title' => $title,
)),
'#default_value' => $settings['labels'][$key],
'#required' => TRUE,
);
$form['autocomplete_source'][$key] = array(
'#type' => 'checkboxes',
'#title' => t('Autocomplete options'),
'#default_value' => $settings['autocomplete_source'][$key],
'#description' => t("This defines what autocomplete sources are available to the field."),
'#options' => $autocomplete_sources_options,
);
foreach ($autocomplete_sources as $ac_source => $ac_settings) {
if (!empty($ac_settings['components']) && !in_array($key, $ac_settings['components'])) {
unset($form['autocomplete_source'][$key]['#options'][$ac_source]);
}
}
$has_ac_options = count($form['autocomplete_source'][$key]['#options']);
$form['autocomplete_separator'][$key] = array(
'#type' => 'textfield',
'#title' => t('Autocomplete separator for !title', array(
'!title' => $title,
)),
'#default_value' => $has_ac_options ? $settings['autocomplete_separator'][$key] : '',
'#size' => 10,
);
if (!$has_ac_options) {
$form['autocomplete_separator'][$key]['#disabled'] = TRUE;
}
}
$form['allow_family_or_given'] = array(
'#type' => 'checkbox',
'#title' => t('Allow a single valid given or family value to fulfill the minimum component requirements for both given and family components.'),
'#default_value' => !empty($settings['allow_family_or_given']),
);
// @todo Grouping & grouping sort.
// @todo Allow reverse free tagging back into the vocabulary.
$title_options = implode("\n", array_filter(explode("\n", $settings['title_options'])));
$form['title_options'] = array(
'#type' => 'textarea',
'#title' => t('!title options', array(
'!title' => $components['title'],
)),
'#default_value' => $title_options,
'#required' => TRUE,
'#description' => t("Enter one !title per line. Prefix a line using '--' to specify a blank value text. For example: '--Please select a !title'.", array(
'!title' => $components['title'],
)),
);
$generational_options = implode("\n", array_filter(explode("\n", $settings['generational_options'])));
$form['generational_options'] = array(
'#type' => 'textarea',
'#title' => t('!generational options', array(
'!generational' => $components['generational'],
)),
'#default_value' => $generational_options,
'#required' => TRUE,
'#description' => t("Enter one !generational suffix option per line. Prefix a line using '--' to specify a blank value text. For example: '----'.", array(
'!generational' => $components['generational'],
)),
);
if (module_exists('taxonomy')) {
// @todo Make the labels more generic.
// Generational suffixes may be also imported from one or more vocabularies
// using the tag '[vocabulary:xxx]', where xxx is the vocabulary id. Terms
// that exceed the maximum length of the generational suffix are not added
// to the options list.
$form['title_options']['#description'] .= ' ' . t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary machine-name or id. Terms that exceed the maximum length of the %label are not added to the options list.", array(
'%label_plural' => t('Titles'),
'%label' => t('Title'),
));
$form['generational_options']['#description'] .= ' ' . t("%label_plural may be also imported from one or more vocabularies using the tag '[vocabulary:xxx]', where xxx is the vocabulary machine-name or id. Terms that exceed the maximum length of the %label are not added to the options list.", array(
'%label_plural' => t('Generational suffixes'),
'%label' => t('Generational suffix'),
));
}
$sort_options = is_array($settings['sort_options']) ? $settings['sort_options'] : array(
'title' => 'title',
'generational' => '',
);
$form['sort_options'] = array(
'#type' => 'checkboxes',
'#title' => t('Select field sort options'),
'#default_value' => $sort_options,
'#description' => t("This enables sorting on the options after the vocabulary terms are added and duplicate values are removed."),
'#options' => _name_translations(array(
'title' => '',
'generational' => '',
)),
);
$form['#element_validate'] = array(
'_name_field_settings_form_validate',
);
return $form;
}
/**
* Implements the validation callback for the name_field_settings_form() form.
*
* This is an #element_validate callback.
*
* @todo Ensure that the #parent path is correct in all instances. This avoids
* the need to pull out individual values from the $elements[xx][#value].
*/
function _name_field_settings_form_validate($elements, &$form_state, $form) {
$values = $form_state['values']['field']['settings'];
// Validates options against the title / generational sizes.
_name_options_validate($values['title_options'], $values['max_length']['title'], t('Title options'), 'field][settings][title_options');
_name_options_validate($values['generational_options'], $values['max_length']['generational'], t('Generational options'), 'field][settings][generational_options');
// Validates that a minimum component is not selected when that component is
// not selected.
_name_field_components_validate($values['components'], $values['minimum_components'], 'field][settings][minimum_components');
if (!empty($form_state['values']['instance'])) {
$instance_settings = $form_state['values']['instance']['settings'];
$instance_components = array_filter($instance_settings['components']);
$instance_minimum_components = array_filter($instance_settings['minimum_components']);
if ($instance_components && $instance_minimum_components) {
_name_field_components_validate($instance_components, $instance_minimum_components, 'instance[settings][minimum_components');
}
elseif ($instance_minimum_components) {
_name_field_components_validate($values['components'], $instance_minimum_components, 'instance[settings][minimum_components');
}
elseif ($instance_components) {
_name_field_components_validate($instance_components, $values['minimum_components'], 'instance[settings][components');
}
}
}
// @todo hook_field_views_data() if required.
/**
* Checks that individual values in the defined options list do not exceed the
* limits placed on the component.
*/
function _name_options_validate($options, $max, $label, $error_element) {
$values = array_filter(explode("\n", $options));
$long_options = array();
$valid_options = array();
$default_options = array();
foreach ($values as $value) {
$value = trim($value);
// Blank option - anything goes!
if (strpos($value, '--') === 0) {
$default_options[] = $value;
}
elseif (preg_match(NAME_FIELD_TAXONOMY_OPTION_REGEX, $value, $matches)) {
if (!module_exists('taxonomy')) {
form_set_error($error_element, t("The taxonomy module must be enabled before using the '%tag' tag in %label.", array(
'%tag' => $matches[0],
'%label' => $label,
)));
}
elseif ($value !== $matches[0]) {
form_set_error($error_element, t("The '%tag' tag in %label should be on a line by itself.", array(
'%tag' => $matches[0],
'%label' => $label,
)));
}
else {
if (_name_get_vocabulary_id_by_code_or_number($matches[1])) {
$valid_options[] = $value;
}
else {
form_set_error($error_element, t("The vocabulary '%tag' in %label could not be found.", array(
'%tag' => $matches[1],
'%label' => $label,
)));
}
}
}
elseif (drupal_strlen($value) > $max) {
$long_options[] = $value;
}
elseif (!empty($value)) {
$valid_options[] = $value;
}
}
if (count($long_options)) {
form_set_error($error_element, t('The following options exceed the maximun allowed %label length: %options', array(
'%options' => implode(', ', $long_options),
'%label' => $label,
)));
}
elseif (empty($valid_options)) {
form_set_error($error_element, t('%label are required.', array(
'%label' => $label,
)));
}
elseif (count($default_options) > 1) {
form_set_error($error_element, t('%label can only have one blank value assigned to it.', array(
'%label' => $label,
)));
}
}
/**
*
*/
function _name_field_components_validate($components, $minimum, $error_element) {
$diff = array_diff_key(array_filter($minimum), array_filter($components));
if (count($diff)) {
$components = array_intersect_key(_name_translations(), $diff);
form_set_error($error_element . '][' . key($diff), t('%components can not be selected for %label when they are not selected for %label2.', array(
'%label' => t('Minimum components'),
'%label2' => t('Components'),
'%components' => implode(', ', $components),
)));
}
}
/* ----------------------------- Widget Code -------------------------------- */
/**
* Implements hook_field_instance_settings_form().
*/
function _name_field_instance_settings_form($field, $instance = NULL) {
$settings = $instance['settings'];
_name_defaults($settings, 'instance_settings');
$components = _name_translations();
$autocomplete_sources = module_invoke_all('name_data_sources');
$form = array(
'size' => array(),
'title_display' => array(),
);
foreach ($components as $key => $title) {
$options = array(
'text' => t('Text field'),
);
$default_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'])) {
$options['select'] = t('Drop-down');
$default_field_type = 'select';
}
if (!empty($ac_settings['autocomplete callback'])) {
$options['autocomplete'] = t('Autocomplete');
}
}
}
$form['field_type'][$key] = array(
'#type' => 'radios',
'#title' => t('!title field type', array(
'!title' => $components['title'],
)),
// Provides backwards compatibility with Drupal 6 modules.
'#default_value' => isset($settings['field_type'][$key]) ? $settings['field_type'][$key] : (isset($settings[$key . '_field']) ? $settings[$key . '_field'] : $default_field_type),
'#required' => TRUE,
'#options' => $options,
);
$form['size'][$key] = array(
'#type' => 'textfield',
'#title' => t('HTML size property for !title', array(
'!title' => $title,
)),
'#default_value' => isset($settings['size']) ? $settings['size'][$key] : '',
'#required' => FALSE,
'#size' => 10,
'#description' => t('The maximum length of the field in characters. This must be between 1 and 255.'),
'#element_validate' => array(
'_element_validate_integer_positive',
),
);
$form['title_display'][$key] = array(
'#type' => 'radios',
'#title' => t('Label display for !title', array(
'!title' => $title,
)),
'#default_value' => isset($settings['title_display']) ? $settings['title_display'][$key] : 'description',
'#options' => array(
'title' => t('above'),
'description' => t('below'),
'placeholder' => t('placeholder'),
'none' => t('hidden'),
),
'#description' => t('This controls how the label of the component is displayed in the form.'),
);
$form['inline_css'][$key] = array(
'#type' => 'textfield',
'#title' => t('Additional inline styles for !title input element.', array(
'!title' => $title,
)),
'#default_value' => isset($settings['inline_css']) ? $settings['inline_css'][$key] : '',
'#size' => 8,
);
$form['labels'][$key] = array(
'#type' => 'textfield',
'#title' => t('Label for !title', array(
'!title' => $title,
)),
'#default_value' => isset($settings['labels']) ? $settings['labels'][$key] : '',
'#size' => 12,
);
}
$form['components'] = array(
'#type' => 'checkboxes',
'#title' => t('Components overrides'),
'#default_value' => $settings['components'],
'#options' => $components,
'#element_validate' => array(
'_name_field_minimal_component_requirements',
),
);
$form['minimum_components'] = array(
'#type' => 'checkboxes',
'#title' => t('Minimum components overrides'),
'#default_value' => $settings['minimum_components'],
'#element_validate' => array(
'_name_field_minimal_component_requirements',
),
'#options' => $components,
);
$form['component_css'] = array(
'#type' => 'textfield',
'#title' => t('Component separator CSS'),
'#default_value' => empty($settings['component_css']) ? '' : $settings['component_css'],
'#description' => t('Use this to override the default CSS used when rendering each component. Use "<none>" to prevent the use of inline CSS.'),
);
$items = array(
t('The order for Asian names is Family Middle Given Title'),
t('The order for Eastern names is Title Family Given Middle'),
t('The order for Western names is Title First Middle Surname'),
);
$layout_description = t('<p>This controls the order of the widgets that are displayed in the form.</p>') . theme('item_list', array(
'items' => $items,
)) . t('<p>Note that when you select the Asian names format, the Generational field is hidden and defaults to an empty string.</p>');
$form['component_layout'] = array(
'#type' => 'radios',
'#title' => t('Language layout'),
'#default_value' => isset($settings['component_layout']) ? $settings['component_layout'] : 'default',
'#options' => array(
'default' => t('Western names'),
'asian' => t('Asian names'),
'eastern' => t('Eastern names'),
),
'#description' => $layout_description,
);
$form['show_component_required_marker'] = array(
'#type' => 'checkbox',
'#title' => t('Show component required marker'),
'#default_value' => empty($settings['show_component_required_marker']) ? 0 : 1,
'#description' => t('Appends an asterisk after the component title if the component is required as part of a complete name.'),
);
$form['credentials_inline'] = array(
'#type' => 'checkbox',
'#title' => t('Show the credentials inline'),
'#default_value' => empty($settings['credentials_inline']) ? 0 : 1,
'#description' => t('The default position is to show the credentials on a line by themselves. This option overrides this to render the component inline.'),
);
// Add the overwrite user name option.
if ($instance['entity_type'] == 'user' && $instance['bundle'] == 'user') {
$preferred_field = variable_get('name_user_preferred', '');
$form['name_user_preferred'] = array(
'#type' => 'checkbox',
'#title' => t('Use this field to override the users login name?'),
'#default_value' => $preferred_field == $instance['field_name'] ? 1 : 0,
);
$form['override_format'] = array(
'#type' => 'select',
'#title' => t('User name override format to use'),
'#default_value' => isset($settings['override_format']) ? $settings['override_format'] : 'default',
'#options' => array(
'default' => t('Default'),
) + name_get_custom_format_options(),
);
}
else {
// We may extend this feature to Profile2 latter.
$form['override_format'] = array(
'#type' => 'value',
'#value' => isset($settings['override_format']) ? $settings['override_format'] : 'default',
);
}
$form['preferred_field_reference'] = array(
'#type' => 'select',
'#title' => t('Preferred component source'),
'#empty_option' => t('-- none --'),
'#options' => name_get_additional_sources($instance),
'#default_value' => empty($settings['preferred_field_reference']) ? '' : $settings['preferred_field_reference'],
'#description' => t('A data source to use as the preferred given name within the name formats. A common use-case would be for a users nickname.') . '<br>' . t('i.e. used in the name format options "q" and "v", plus the conditional "p", "x", "d" and "D".'),
);
return $form;
}
/**
* Implements hook_field_widget_form().
*/
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;
}
/**
*
*/
function _name_field_get_options($field, $key) {
_name_defaults($field['settings'], 'settings');
$fs = $field['settings'];
$options = array_filter(explode("\n", $fs[$key . '_options']));
foreach ($options as $index => $opt) {
if (preg_match(NAME_FIELD_TAXONOMY_OPTION_REGEX, trim($opt), $matches)) {
unset($options[$index]);
if (module_exists('taxonomy')) {
if ($vid = _name_get_vocabulary_id_by_code_or_number($matches[1])) {
$max_length = isset($fs['max_length'][$key]) ? $fs['max_length'][$key] : 255;
foreach (taxonomy_get_tree($vid) 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 (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 = drupal_substr($opt, 2);
}
}
$options = drupal_map_assoc(array_map('trim', $options));
if ($default !== FALSE) {
$options = array(
'' => $default,
) + $options;
}
return $options;
}
/* ---------------------------- Formatter Code ------------------------------ */
/* --------------------------- Helper Functions ----------------------------- */
/**
* Helper function to set the defaults for a name field / widget.
*/
function _name_defaults(&$settings, $key) {
if ($key == 'settings') {
$defaults = field_info_field_settings('name');
}
else {
$defaults = field_info_instance_settings('name');
}
$settings = isset($settings) ? (array) $settings : array();
foreach ($defaults as $index => $defaults) {
if (!isset($settings[$index])) {
if (is_array($defaults)) {
if (!array_key_exists($index, $settings)) {
$settings[$index] = array();
}
$settings[$index] += $defaults;
}
else {
$settings[$index] = $defaults;
}
}
}
}
/**
* Helper form element validator: integer > 0 && <= 255.
*/
function _name_validate_varchar_range($element, &$form_state) {
$value = $element['#value'];
$min = isset($element['#min_size']) ? $element['#min_size'] : 1;
if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value < $min || $value > 255)) {
form_error($element, t('%name must be a positive integer between !min and 255.', array(
'%name' => $element['#title'],
'!min' => $min,
)));
}
}
/**
* Helper function to get the vid from a machine name or number.
*/
function _name_get_vocabulary_id_by_code_or_number($value) {
$vid = FALSE;
if (empty($value)) {
return $vid;
}
if (intval($value) == $value && ($vocab = taxonomy_vocabulary_load($value))) {
$vid = $value;
}
elseif ($vocab = taxonomy_vocabulary_machine_name_load($value)) {
$vid = $vocab->vid;
}
return $vid;
}
Functions
Name | Description |
---|---|
_name_defaults | Helper function to set the defaults for a name field / widget. |
_name_field_components_validate | |
_name_field_get_options | |
_name_field_instance_settings_form | Implements hook_field_instance_settings_form(). |
_name_field_settings_form | Implements hook_field_settings_form(). |
_name_field_settings_form_validate | Implements the validation callback for the name_field_settings_form() form. |
_name_field_widget_form | Implements hook_field_widget_form(). |
_name_get_vocabulary_id_by_code_or_number | Helper function to get the vid from a machine name or number. |
_name_options_validate | Checks that individual values in the defined options list do not exceed the limits placed on the component. |
_name_validate_varchar_range | Helper form element validator: integer > 0 && <= 255. |
Constants
Name | Description |
---|---|
NAME_FIELD_TAXONOMY_OPTION_REGEX | @file Provides additional Field functionality for the name module. |