public static function UcAddress::processAddress in Ubercart 8.4
Callback for the address field #process property.
Parameters
array $element: An associative array containing the properties and children of the generic input element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
File
- uc_store/
src/ Element/ UcAddress.php, line 50
Class
- UcAddress
- Provides a form element for Ubercart address input.
Namespace
Drupal\uc_store\ElementCode
public static function processAddress(&$element, FormStateInterface $form_state, &$complete_form) {
$labels = [
'first_name' => t('First name'),
'last_name' => t('Last name'),
'company' => t('Company'),
'street1' => t('Street address'),
'street2' => ' ',
'city' => t('City'),
'zone' => t('State/Province'),
'country' => t('Country'),
'postal_code' => t('Postal code'),
'phone' => t('Phone number'),
'email' => t('E-mail'),
];
$element['#tree'] = TRUE;
$config = \Drupal::config('uc_store.settings')
->get('address_fields');
/** @var \Drupal\uc_store\AddressInterface $value */
$value = $element['#value'];
$hide = array_flip($element['#hide']);
$wrapper = Html::getClass('uc-address-' . $element['#name'] . '-zone-wrapper');
$country_names = \Drupal::service('country_manager')
->getEnabledList();
// Force the selected country to a valid one, so the zone dropdown matches.
if ($country_keys = array_keys($country_names)) {
if (isset($value->country) && !isset($country_names[$value->country])) {
$value->country = $country_keys[0];
}
}
// Iterating on the Address object excludes non-public properties, which
// is exactly what we want to do.
$address = Address::create();
foreach ($address as $field => $field_value) {
switch ($field) {
case 'country':
if ($country_names) {
$subelement = [
'#type' => 'select',
'#options' => $country_names,
'#ajax' => [
'callback' => [
get_class(),
'updateZone',
],
'wrapper' => $wrapper,
'progress' => [
'type' => 'throbber',
],
],
];
}
else {
$subelement = [
'#type' => 'hidden',
'#required' => FALSE,
];
}
break;
case 'zone':
$subelement = [
'#prefix' => '<div id="' . $wrapper . '">',
'#suffix' => '</div>',
];
$zones = $value->country ? \Drupal::service('country_manager')
->getZoneList($value->country) : [];
if ($zones) {
natcasesort($zones);
$subelement += [
'#type' => 'select',
'#options' => $zones,
'#empty_value' => '',
'#after_build' => [
[
get_class(),
'resetZone',
],
],
];
}
else {
$subelement += [
'#type' => 'hidden',
'#value' => '',
'#required' => FALSE,
];
}
break;
case 'postal_code':
$subelement = [
'#type' => 'textfield',
'#size' => 10,
'#maxlength' => 10,
];
break;
case 'phone':
$subelement = [
'#type' => 'tel',
'#size' => 16,
'#maxlength' => 32,
];
break;
case 'email':
$subelement = [
'#type' => 'email',
'#size' => 16,
];
break;
default:
$subelement = [
'#type' => 'textfield',
'#size' => 32,
];
}
// Copy JavaScript states from the parent element.
if (isset($element['#states'])) {
$subelement['#states'] = $element['#states'];
}
// Set common values for all address fields.
$element[$field] = $subelement + [
'#title' => $labels[$field],
'#default_value' => $value->{$field},
'#access' => !$element['#hidden'] && !empty($config[$field]['status']) && !isset($hide[$field]),
'#required' => $element['#required'] && !empty($config[$field]['required']),
'#weight' => isset($config[$field]['weight']) ? $config[$field]['weight'] : 0,
];
}
return $element;
}