View source
<?php
function cshs_element_info() {
$type['cshs'] = array(
'#input' => TRUE,
'#multiple' => FALSE,
'#process' => array(
'form_process_select',
'ajax_process_form',
'cshs_element_process',
),
'#element_validate' => array(
'cshs_element_validate',
),
'#theme' => 'cshs_select',
'#theme_wrappers' => array(
'form_element',
),
'#none_value' => CSHS_DEFAULT_NONE_VALUE,
'#none_label' => t(CSHS_DEFAULT_NONE_LABEL),
'#parent' => 0,
'#force_deepest' => FALSE,
'#labels' => array(),
'#label' => '',
);
return $type;
}
function cshs_element_process($element, $form_state) {
$path = drupal_get_path('module', 'cshs');
$element += array(
'#attached' => array(
'css' => array(
"{$path}/css/cshs.css",
),
'js' => array(
"{$path}/js/libs/jquery.simpler-select.js",
"{$path}/js/cshs.js",
),
),
);
$id = $element['#id'];
$settings_js = array(
'cshs' => array(
$id => array(
'noneLabel' => $element['#none_label'],
'noneValue' => $element['#none_value'],
'labels' => $element['#labels'],
),
),
);
$element['#attached']['js'][] = array(
'data' => $settings_js,
'type' => 'setting',
);
return $element;
}
function cshs_element_validate(&$element, &$form_state) {
$value = $element['#value'];
$title = $element['#label'];
if (module_exists('i18n_field') && isset($element['#field_name'])) {
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#entity']->type);
$title = i18n_field_translate_property($instance, 'label');
}
$force_deepest = $element['#force_deepest'];
$none_value = $element['#none_value'];
if ($value == $none_value) {
form_set_value($element, NULL, $form_state);
}
if ($form_state['complete form']['#form_id'] == 'field_ui_field_edit_form') {
return;
}
if ($element['#required'] && $value == $none_value) {
form_error($element, t('!name field is required.', array(
'!name' => $title,
)));
}
if ($force_deepest && $value != $none_value) {
$term = taxonomy_term_load($value);
$children = array();
if ($term) {
$vid = $term->vid;
$children = taxonomy_get_children($value, $vid);
}
if ($value == $none_value || count($children)) {
form_error($element, t('You need to select a term from the deepest level in field %field_name.', array(
'%field_name' => $title,
)));
}
}
}