more module updates
This commit is contained in:
@@ -34,6 +34,33 @@ function addressfield_views_api() {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_module_implements_alter().
|
||||
*
|
||||
* Moves the hook_token_info_alter() implementation to the bottom so it is
|
||||
* invoked after all modules implementing the same hook.
|
||||
*/
|
||||
function addressfield_module_implements_alter(&$implementations, $hook) {
|
||||
if ($hook == 'token_info_alter') {
|
||||
// Make sure that the $implementations list is populated before altering it,
|
||||
// to work around a crash experienced by some people (#2181001).
|
||||
if (isset($implementations['addressfield'])) {
|
||||
$group = $implementations['addressfield'];
|
||||
unset($implementations['addressfield']);
|
||||
$implementations['addressfield'] = $group;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns TRUE if a field map array value represents an addressfield.
|
||||
*
|
||||
* Provided for use as a callback by array_filter().
|
||||
*/
|
||||
function addressfield_field_map_filter($field) {
|
||||
return !empty($field['type']) && $field['type'] == 'addressfield';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of format plugins.
|
||||
*/
|
||||
@@ -119,7 +146,10 @@ function addressfield_generate($address, array $handlers, array $context = array
|
||||
|
||||
ctools_include('plugins');
|
||||
$format = array();
|
||||
$format['#handlers'] = $handlers;
|
||||
// Add the handlers, ordered by weight.
|
||||
$plugins = addressfield_format_plugins();
|
||||
$format['#handlers'] = array_intersect(array_keys($plugins), $handlers);
|
||||
|
||||
foreach ($format['#handlers'] as $handler) {
|
||||
if ($callback = ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback')) {
|
||||
$callback($format, $address, $context);
|
||||
@@ -133,7 +163,6 @@ function addressfield_generate($address, array $handlers, array $context = array
|
||||
if ($context['mode'] == 'form') {
|
||||
$format['#addressfield'] = TRUE;
|
||||
$format['#process'][] = 'addressfield_process_format_form';
|
||||
$format['#required'] = FALSE;
|
||||
}
|
||||
elseif ($context['mode'] == 'render') {
|
||||
$format['#pre_render'][] = 'addressfield_render_address';
|
||||
@@ -152,39 +181,43 @@ function addressfield_process_format_form($format, &$form_state, $complete_form)
|
||||
ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback');
|
||||
}
|
||||
|
||||
_addressfield_process_format_form($format, $format['#address'], $format['#required']);
|
||||
_addressfield_process_format_form($format, $format['#address']);
|
||||
return $format;
|
||||
}
|
||||
|
||||
function _addressfield_process_format_form(&$format, $address, $required) {
|
||||
function _addressfield_process_format_form(&$format, $address) {
|
||||
foreach (element_children($format) as $key) {
|
||||
$child = &$format[$key];
|
||||
|
||||
// Automatically expand elements that matches one of the field of the
|
||||
// address structure.
|
||||
// Automatically convert any element in the format array to an appropriate
|
||||
// form element that matches one of the address component names.
|
||||
if (in_array($key, array('name_line', 'first_name', 'last_name', 'organisation_name', 'country', 'administrative_area', 'sub_administrative_area', 'locality', 'dependent_locality', 'postal_code', 'thoroughfare', 'premise', 'sub_premise'))) {
|
||||
// Set the type.
|
||||
// Set the form element type for the address component to whatever the
|
||||
// address format specified in its #widget_type property.
|
||||
if (isset($child['#widget_type'])) {
|
||||
$child['#type'] = $child['#widget_type'];
|
||||
}
|
||||
else {
|
||||
// If the element didn't specify a #widget_type and has options, turn it
|
||||
// into a select list and unset its #size value, which is typically used
|
||||
// to provide the width of a textfield.
|
||||
if (isset($child['#options'])) {
|
||||
$child['#type'] = 'select';
|
||||
$child['#size'] = 0;
|
||||
unset($child['#size']);
|
||||
}
|
||||
else {
|
||||
// Otherwise go ahead and make it a textfield.
|
||||
$child['#type'] = 'textfield';
|
||||
}
|
||||
}
|
||||
if (!$required) {
|
||||
unset($child['#required']);
|
||||
}
|
||||
|
||||
$child['#default_value'] = $address[$key];
|
||||
if (isset($address[$key])) {
|
||||
$child['#default_value'] = $address[$key];
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse through the child.
|
||||
_addressfield_process_format_form($child, $address, $required);
|
||||
// Recurse through the element's children if it has any.
|
||||
_addressfield_process_format_form($child, $address);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,8 +233,8 @@ function _addressfield_render_address(&$format, $address) {
|
||||
foreach (element_children($format) as $key) {
|
||||
$child = &$format[$key];
|
||||
|
||||
// Automatically expand elements that matches one of the field of the
|
||||
// address structure.
|
||||
// Automatically expand elements that match one of the fields of the address
|
||||
// structure.
|
||||
if (in_array($key, array('name_line', 'first_name', 'last_name', 'organisation_name', 'country', 'administrative_area', 'sub_administrative_area', 'locality', 'dependent_locality', 'postal_code', 'thoroughfare', 'premise', 'sub_premise'), TRUE)) {
|
||||
if (isset($child['#render_type'])) {
|
||||
$child['#type'] = $child['#render_type'];
|
||||
@@ -216,12 +249,15 @@ function _addressfield_render_address(&$format, $address) {
|
||||
// If the element instructs us to render the option value instead of the
|
||||
// raw address element value and its #options array has a matching key,
|
||||
// swap it out for the option value now.
|
||||
if (!empty($child['#render_option_value']) && isset($child['#options'][$address[$key]])) {
|
||||
if (!empty($child['#render_option_value']) && isset($address[$key]) && isset($child['#options'][$address[$key]])) {
|
||||
$child['#children'] = check_plain($child['#options'][$address[$key]]);
|
||||
}
|
||||
else {
|
||||
elseif (isset($address[$key])) {
|
||||
$child['#children'] = check_plain($address[$key]);
|
||||
}
|
||||
else {
|
||||
$child['#children'] = '';
|
||||
}
|
||||
|
||||
// Skip empty elements.
|
||||
if ((string) $child['#children'] === '') {
|
||||
@@ -261,8 +297,12 @@ function addressfield_theme() {
|
||||
*/
|
||||
function theme_addressfield_container($variables) {
|
||||
$element = $variables['element'];
|
||||
|
||||
$element['#children'] = trim($element['#children']);
|
||||
// Remove the autocomplete attributes because the W3C validator complains.
|
||||
// They are only used on forms anyway.
|
||||
unset($element['#attributes']['autocomplete']);
|
||||
unset($element['#attributes']['x-autocompletetype']);
|
||||
|
||||
if (strlen($element['#children']) > 0) {
|
||||
$output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
|
||||
$output .= $element['#children'];
|
||||
@@ -321,22 +361,27 @@ function addressfield_field_info() {
|
||||
|
||||
/**
|
||||
* Returns an array of default values for the addressfield form elements.
|
||||
*
|
||||
* @param $field
|
||||
* The field array.
|
||||
* @param $instance
|
||||
* The instance array.
|
||||
* @param $address
|
||||
* The current address values, if known. Allows for per-country defaults.
|
||||
*
|
||||
* @return
|
||||
* An array of default values.
|
||||
*/
|
||||
function addressfield_default_values($available_countries = NULL) {
|
||||
if (!isset($available_countries)) {
|
||||
$available_countries = _addressfield_country_options_list();
|
||||
}
|
||||
|
||||
// Use the default country of the site if possible.
|
||||
$default_country = variable_get('site_default_country', NULL);
|
||||
|
||||
// If the default country is undefined or not in the list of available countries,
|
||||
// just fallback to the first country in the list.
|
||||
if (!$default_country || !isset($available_countries[$default_country])) {
|
||||
function addressfield_default_values($field, $instance, array $address = array()) {
|
||||
$available_countries = _addressfield_country_options_list($field, $instance);
|
||||
$default_country = $instance['widget']['settings']['default_country'];
|
||||
// If the default country is not in the list of available countries,
|
||||
// fallback to the first country in the list.
|
||||
if ($default_country && !isset($available_countries[$default_country])) {
|
||||
$default_country = key($available_countries);
|
||||
}
|
||||
|
||||
return array(
|
||||
$default_values = array(
|
||||
'country' => $default_country,
|
||||
'name_line' => '',
|
||||
'first_name' => '',
|
||||
@@ -352,6 +397,16 @@ function addressfield_default_values($available_countries = NULL) {
|
||||
'sub_premise' => '',
|
||||
'data' => '',
|
||||
);
|
||||
|
||||
// Allow other modules to alter the default values.
|
||||
$context = array(
|
||||
'field' => $field,
|
||||
'instance' => $instance,
|
||||
'address' => $address,
|
||||
);
|
||||
drupal_alter('addressfield_default_values', $default_values, $context);
|
||||
|
||||
return $default_values;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -363,6 +418,35 @@ function addressfield_field_is_empty($item, $field) {
|
||||
return empty($item['country']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_presave().
|
||||
*/
|
||||
function addressfield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
|
||||
foreach ($items as $delta => &$item) {
|
||||
// If the first name and last name are set but the name line isn't...
|
||||
if (isset($item['first_name']) && isset($item['last_name']) && !isset($item['name_line'])) {
|
||||
// Combine the first and last name to be the name line.
|
||||
$items[$delta]['name_line'] = $items[$delta]['first_name'] . ' ' . $items[$delta]['last_name'];
|
||||
}
|
||||
elseif (isset($item['name_line'])) {
|
||||
// Otherwise if the name line is set, separate it out into a best guess at
|
||||
// the first and last name.
|
||||
$names = explode(' ', $item['name_line']);
|
||||
|
||||
$item['first_name'] = array_shift($names);
|
||||
$item['last_name'] = implode(' ', $names);
|
||||
}
|
||||
|
||||
// Trim whitespace from all of the address components and convert any double
|
||||
// spaces to single spaces.
|
||||
foreach ($item as $key => &$value) {
|
||||
if (!in_array($key, array('data')) && is_string($value)) {
|
||||
$value = trim(str_replace(' ', ' ', $value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_info()
|
||||
*/
|
||||
@@ -374,6 +458,7 @@ function addressfield_field_widget_info() {
|
||||
'field types' => array('addressfield'),
|
||||
'settings' => array(
|
||||
'available_countries' => array(),
|
||||
'default_country' => '',
|
||||
'format_handlers' => array('address'),
|
||||
),
|
||||
);
|
||||
@@ -399,7 +484,13 @@ function addressfield_field_widget_settings_form($field, $instance) {
|
||||
'#options' => _addressfield_country_options_list(),
|
||||
'#default_value' => $settings['available_countries'],
|
||||
);
|
||||
|
||||
$form['default_country'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Default country'),
|
||||
'#options' => _addressfield_country_options_list(),
|
||||
'#default_value' => $settings['default_country'],
|
||||
'#empty_value' => '',
|
||||
);
|
||||
$form['format_handlers'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Format handlers'),
|
||||
@@ -411,41 +502,38 @@ function addressfield_field_widget_settings_form($field, $instance) {
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_form_BASE_FORM_ID_alter().
|
||||
*
|
||||
* Removes the default values form from the field settings page.
|
||||
* Allows the module to implement its own, more predictable default value
|
||||
* handling, getting around #1253820 and other bugs.
|
||||
*/
|
||||
function addressfield_form_field_ui_field_edit_form_alter(&$form, $form_state) {
|
||||
if ($form['#field']['type'] == 'addressfield') {
|
||||
$form['instance']['default_value_widget']['#access'] = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_widget_form()
|
||||
*/
|
||||
function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
|
||||
$settings = $instance['widget']['settings'];
|
||||
|
||||
// Generate a specific key used to identify this element to restore a default
|
||||
// value upon AJAX submission regardless of where this element is in the
|
||||
// $form array.
|
||||
$element_key = implode('|', array($element['#entity_type'], $element['#bundle'], $element['#field_name'], $element['#language'], $element['#delta']));
|
||||
|
||||
// Store the key in the element array as a value so it can be easily retrieved
|
||||
// in context in the $form_state['values'] array in the element validator.
|
||||
$element['element_key'] = array(
|
||||
'#type' => 'value',
|
||||
'#value' => $element_key,
|
||||
);
|
||||
|
||||
// Get the default address used to build the widget form elements, looking
|
||||
// first in the form state, then in the stored value for the field, and then
|
||||
// in the default values of the instance.
|
||||
$address = array();
|
||||
|
||||
if (!empty($form_state['addressfield'][$element_key])) {
|
||||
// Use the value from the form_state if available.
|
||||
$address = $form_state['addressfield'][$element_key];
|
||||
// If the form has been rebuilt via AJAX, use the form state values.
|
||||
// $form_state['values'] is empty because of #limit_validation_errors, so
|
||||
// $form_state['input'] needs to be used instead.
|
||||
$parents = array_merge($element['#field_parents'], array($element['#field_name'], $langcode, $delta));
|
||||
$input_address = drupal_array_get_nested_value($form_state['input'], $parents);
|
||||
if (!empty($input_address)) {
|
||||
$address = $input_address;
|
||||
}
|
||||
elseif (!empty($items[$delta]['country'])) {
|
||||
// Else use the saved value for the field.
|
||||
$address = $items[$delta];
|
||||
}
|
||||
else {
|
||||
// Otherwise use the instance default.
|
||||
$address = (array) $instance['default_value'][0];
|
||||
}
|
||||
|
||||
// Determine the list of available countries, and if the currently selected
|
||||
// country is not in it, unset it so it can be reset to the default country.
|
||||
@@ -454,8 +542,8 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance,
|
||||
unset($address['country']);
|
||||
}
|
||||
|
||||
// Merge in default values to provide a value for every expected array key.
|
||||
$address += addressfield_default_values($countries);
|
||||
// Merge in default values.
|
||||
$address += addressfield_default_values($field, $instance, $address);
|
||||
|
||||
// Add the form elements for the standard widget, which includes a country
|
||||
// select list at the top that reloads the available address elements when the
|
||||
@@ -466,6 +554,19 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance,
|
||||
// in that context, and it is overridable if necessary.
|
||||
$element['#type'] = 'fieldset';
|
||||
|
||||
if (!empty($instance['description'])) {
|
||||
// Checkout panes convert the fieldset into a container, causing
|
||||
// #description to not be rendered. Hence, a real element is added and
|
||||
// the old #description is removed.
|
||||
$element['#description'] = '';
|
||||
$element['element_description'] = array(
|
||||
'#markup' => $instance['description'],
|
||||
'#prefix' => '<div class="fieldset-description">',
|
||||
'#suffix' => '</div>',
|
||||
'#weight' => -999,
|
||||
);
|
||||
}
|
||||
|
||||
// Generate the address form.
|
||||
$context = array(
|
||||
'mode' => 'form',
|
||||
@@ -476,32 +577,39 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance,
|
||||
);
|
||||
$element += addressfield_generate($address, $settings['format_handlers'], $context);
|
||||
|
||||
// Mark the form element as required if necessary.
|
||||
$element['#required'] = $delta == 0 && $instance['required'];
|
||||
// Remove any already saved default value.
|
||||
// See addressfield_form_field_ui_field_edit_form_alter() for the reasoning.
|
||||
if ($form_state['build_info']['form_id'] == 'field_ui_field_edit_form') {
|
||||
$element['#address'] = array('country' => '');
|
||||
}
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Element validate callback: rebuilds the form on country change and stores the
|
||||
* current address value in the $form_state for retrieval on rebuild.
|
||||
* Element validate callback: rebuilds the form on country change.
|
||||
*/
|
||||
function addressfield_standard_country_validate($element, &$form_state) {
|
||||
// If the country was changed, rebuild the form.
|
||||
if ($element['#default_value'] != $element['#value']) {
|
||||
$parents = $element['#parents'];
|
||||
array_pop($parents);
|
||||
$address = drupal_array_get_nested_value($form_state['values'], $parents);
|
||||
|
||||
// Clear the country-specific field values.
|
||||
$country_specific_data = array(
|
||||
'dependent_locality' => '',
|
||||
'locality' => '',
|
||||
'administrative_area' => '',
|
||||
'postal_code' => '',
|
||||
);
|
||||
$address = array_diff_key($address, $country_specific_data);
|
||||
|
||||
drupal_array_set_nested_value($form_state['values'], $parents, $address);
|
||||
drupal_array_set_nested_value($form_state['input'], $parents, $address);
|
||||
|
||||
$form_state['rebuild'] = TRUE;
|
||||
}
|
||||
|
||||
$parents = $element['#parents'];
|
||||
array_pop($parents);
|
||||
|
||||
// Search through the form values to find the current address.
|
||||
$address = drupal_array_get_nested_value($form_state['values'], $parents);
|
||||
|
||||
// Store the present address values in the form state for retrieval by the
|
||||
// widget form regardless of where the widget sits in the $form array.
|
||||
$form_state['addressfield'][$address['element_key']] = array_diff_key($address, array('element_key' => ''));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -531,6 +639,9 @@ function addressfield_standard_widget_refresh($form, $form_state) {
|
||||
// new country select list.
|
||||
$commands[] = ajax_command_replace(NULL, render($element));
|
||||
$commands[] = ajax_command_invoke('#' . $element['country']['#id'], 'focus');
|
||||
// Add the status messages inside the new addressfield's wrapper element,
|
||||
// just like core does.
|
||||
$commands[] = ajax_command_prepend(NULL, theme('status_messages'));
|
||||
|
||||
// Allow other modules to add arbitrary AJAX commands on the refresh.
|
||||
drupal_alter('addressfield_standard_widget_refresh', $commands, $form, $form_state);
|
||||
@@ -679,12 +790,8 @@ function addressfield_property_info_callback(&$info, $entity_type, $field, $inst
|
||||
*
|
||||
* @see addressfield_property_info_callback()
|
||||
*/
|
||||
function addressfield_auto_creation() {
|
||||
// We can't call addressfield_default_values() directly, because it has an
|
||||
// optional array argument that will receive an invalid value when Entity API
|
||||
// tries to call it directly with its usual $property_name and $context
|
||||
// arguments.
|
||||
return addressfield_default_values();
|
||||
function addressfield_auto_creation($property_name, $context) {
|
||||
return addressfield_default_values($context['field'], $context['instance']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -730,6 +837,9 @@ function addressfield_data_property_info($name = NULL) {
|
||||
'premise' => array(
|
||||
'label' => t('Premise (i.e. Apartment / Suite number)'),
|
||||
),
|
||||
'sub_premise' => array(
|
||||
'label' => t('Sub Premise (i.e. Suite, Apartment, Floor, Unknown.'),
|
||||
),
|
||||
);
|
||||
|
||||
// Add the default values for each of the address field properties.
|
||||
@@ -746,13 +856,16 @@ function addressfield_data_property_info($name = NULL) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps country_get_list() for use as an Entity API options list.
|
||||
* Returns the country list in a format suitable for use as an options list.
|
||||
*/
|
||||
function _addressfield_country_options_list($field = NULL, $instance = NULL) {
|
||||
// Necessary for country_get_list().
|
||||
require_once DRUPAL_ROOT . '/includes/locale.inc';
|
||||
|
||||
$countries = country_get_list();
|
||||
if (module_exists('countries')) {
|
||||
$countries = countries_get_countries('name', array('enabled' => COUNTRIES_ENABLED));
|
||||
}
|
||||
else {
|
||||
require_once DRUPAL_ROOT . '/includes/locale.inc';
|
||||
$countries = country_get_list();
|
||||
}
|
||||
|
||||
if (isset($field)) {
|
||||
// If the instance is not specified, loop against all the instances of the field.
|
||||
@@ -771,10 +884,6 @@ function _addressfield_country_options_list($field = NULL, $instance = NULL) {
|
||||
foreach ($instances as $instance) {
|
||||
if (!empty($instance['widget']['settings']['available_countries'])) {
|
||||
$countries = array_intersect_key($countries, $instance['widget']['settings']['available_countries']);
|
||||
}
|
||||
else {
|
||||
// This instance allow all the countries.
|
||||
$countries = country_get_list();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user