function countries_field_widget_form in Countries 7.2
Same name and namespace in other branches
- 8 countries.fields.inc \countries_field_widget_form()
- 7 countries.module \countries_field_widget_form()
Implements hook_field_widget_form().
File
- ./
countries.fields.inc, line 281 - All field related code.
Code
function countries_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'countries_continent') {
$field_name = $field['field_name'];
$settings = $field['settings'];
$required = !empty($element['#required']);
$continents = countries_get_continents();
if (!empty($settings['continents'])) {
$continents = array_intersect_key($continents, $settings['continents']);
}
// Widget may be rendered individually, in a group or even a sub-form.
$field_parents = empty($form['#parents']) ? array() : $form['#parents'];
$parents = array_merge($field_parents, array(
$field_name,
$langcode,
$delta,
));
$continent_parents = array_merge($parents, array(
'continent',
));
$iso2 = empty($items[$delta]['iso2']) ? '' : $items[$delta]['iso2'];
$selected_continent = '';
if (isset($form_state['values'])) {
// This override the settings that are passed onto the options filter
// query. This is not called when JScript is disabled.
$selected_continent = drupal_array_get_nested_value($form_state['values'], $continent_parents);
if (!$selected_continent) {
// Fallback to the raw user post. A workaround for AJAX submissions.
$user_data = drupal_array_get_nested_value($form_state['input'], $continent_parents);
if (array_key_exists($user_data, $continents)) {
$selected_continent = $user_data;
}
}
// Do not override the continent settings when empty.
if ($selected_continent) {
$settings['continents'] = array(
$selected_continent,
);
}
}
elseif (!empty($iso2) && ($country = country_load($iso2))) {
$selected_continent = $country->continent;
}
// Work out the best ID to exactly target the element.
$wrapper_parents = array_merge($parents, array(
'iso-wrapper',
));
$wrapper_id = implode('-', $wrapper_parents);
// Assist themers with floating DIVs.
$element['#type'] = 'container';
$element['#attributes']['class'][] = 'clearfix countries-continent-wrapper';
$element['continent'] = array(
'#type' => 'select',
'#title' => t('Continent'),
'#options' => $continents,
'#empty_option' => t('-- None selected --'),
'#empty_value' => 'none',
'#default_value' => $selected_continent,
'#required' => $required,
'#ajax' => array(
'callback' => '_ajax_countries_continent_widget_callback',
'wrapper' => $wrapper_id,
),
);
$first = array_shift($continent_parents);
$element['iso2'] = array(
'#type' => 'country',
'#title' => t('Country'),
'#default_value' => $iso2,
'#prefix' => '<div id="' . $wrapper_id . '" class="countries-ajax-wrapper">',
'#suffix' => '</div>',
'#empty_value' => '',
'#hide_empty' => TRUE,
'#filters' => $settings,
'#required' => $required,
'#states' => array(
'visible' => array(
':input[name="' . $first . '[' . implode('][', $continent_parents) . ']"]' => array(
'!value' => 'none',
),
),
),
);
}
return $element;
}