class UcAddress in Ubercart 8.4
Provides a form element for Ubercart address input.
Plugin annotation
@FormElement("uc_address");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\uc_store\Element\UcAddress
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of UcAddress
8 #type uses of UcAddress
- AddressForm::buildForm in shipping/
uc_fulfillment/ src/ Form/ AddressForm.php - Form constructor.
- AddressPaneBase::buildForm in uc_order/
src/ Plugin/ Ubercart/ OrderPane/ AddressPaneBase.php - Form constructor.
- AddressPaneBase::view in uc_cart/
src/ Plugin/ Ubercart/ CheckoutPane/ AddressPaneBase.php - Returns the contents of a checkout pane.
- Check::buildConfigurationForm in payment/
uc_payment_pack/ src/ Plugin/ Ubercart/ PaymentMethod/ Check.php - Form constructor.
- QuoteSettingsForm::buildForm in shipping/
uc_quote/ src/ Form/ QuoteSettingsForm.php - Form constructor.
File
- uc_store/
src/ Element/ UcAddress.php, line 16
Namespace
Drupal\uc_store\ElementView source
class UcAddress extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#required' => TRUE,
'#hide' => [],
'#process' => [
[
$class,
'processAddress',
],
],
'#attributes' => [
'class' => [
'uc-store-address-field',
],
],
'#theme_wrappers' => [
'container',
],
'#hidden' => FALSE,
];
}
/**
* Callback for the address field #process property.
*
* @param array $element
* An associative array containing the properties and children of the
* generic input element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed element.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE) {
return Address::create($input);
}
elseif ($element['#default_value'] instanceof AddressInterface) {
return $element['#default_value'];
}
elseif (is_array($element['#default_value'])) {
// @todo Remove when all callers supply objects.
return Address::create($element['#default_value']);
}
else {
return Address::create();
}
}
/**
* Ajax callback: updates the zone select box when the country is changed.
*/
public static function updateZone($form, FormStateInterface $form_state) {
$element =& $form;
$triggering_element = $form_state
->getTriggeringElement();
foreach (array_slice($triggering_element['#array_parents'], 0, -1) as $field) {
$element =& $element[$field];
}
return $element['zone'];
}
/**
* Resets the zone dropdown when the country is changed.
*/
public static function resetZone($element, FormStateInterface $form_state) {
if (!isset($element['#options'][$element['#default_value']])) {
$element['#value'] = $element['#empty_value'];
}
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UcAddress:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
UcAddress:: |
public static | function | Callback for the address field #process property. | |
UcAddress:: |
public static | function | Resets the zone dropdown when the country is changed. | |
UcAddress:: |
public static | function | Ajax callback: updates the zone select box when the country is changed. | |
UcAddress:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |