abstract class AddressPaneBase in Ubercart 8.4
Same name in this branch
- 8.4 uc_order/src/Plugin/Ubercart/OrderPane/AddressPaneBase.php \Drupal\uc_order\Plugin\Ubercart\OrderPane\AddressPaneBase
- 8.4 uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php \Drupal\uc_cart\Plugin\Ubercart\CheckoutPane\AddressPaneBase
Provides a generic address pane that can be extended as required.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\uc_cart\Plugin\Ubercart\CheckoutPane\AddressPaneBase
- class \Drupal\uc_cart\CheckoutPanePluginBase implements CheckoutPanePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AddressPaneBase
File
- uc_cart/
src/ Plugin/ Ubercart/ CheckoutPane/ AddressPaneBase.php, line 16
Namespace
Drupal\uc_cart\Plugin\Ubercart\CheckoutPaneView source
abstract class AddressPaneBase extends CheckoutPanePluginBase {
/**
* Source pane for "copy address" checkbox.
*
* @var string
*/
protected static $sourcePaneId;
/**
* Returns additional text to display as a description.
*
* @return string
* The fieldset description.
*/
protected abstract function getDescription();
/**
* Returns text to display for the 'copy address' field.
*
* @return string
* The text to display.
*/
protected abstract function getCopyAddressText();
/**
* {@inheritdoc}
*/
public function view(OrderInterface $order, array $form, FormStateInterface $form_state) {
$user = \Drupal::currentUser();
$pane = $this->pluginDefinition['id'];
$source = $this
->sourcePaneId();
$contents['#description'] = $this
->getDescription();
if ($source != $pane) {
$contents['copy_address'] = [
'#type' => 'checkbox',
'#title' => $this
->getCopyAddressText(),
'#default_value' => $this->configuration['default_same_address'],
'#ajax' => [
'callback' => [
$this,
'ajaxRender',
],
'wrapper' => $pane . '-address-pane',
'progress' => [
'type' => 'throbber',
],
],
];
}
if ($user
->isAuthenticated() && ($addresses = uc_select_addresses($user
->id(), $pane))) {
$contents['select_address'] = [
'#type' => 'select',
'#title' => $this
->t('Saved addresses'),
'#options' => $addresses['#options'],
'#ajax' => [
'callback' => [
$this,
'ajaxRender',
],
'wrapper' => $pane . '-address-pane',
'progress' => [
'type' => 'throbber',
],
],
'#states' => [
'invisible' => [
'input[name="panes[' . $pane . '][copy_address]"]' => [
'checked' => TRUE,
],
],
],
];
}
$contents['address'] = [
'#type' => 'uc_address',
'#default_value' => $order
->getAddress($pane),
'#parents' => [
'panes',
$pane,
],
'#prefix' => '<div id="' . $pane . '-address-pane">',
'#suffix' => '</div>',
];
if ($form_state
->hasValue([
'panes',
$pane,
'copy_address',
])) {
$contents['address']['#hidden'] = !$form_state
->isValueEmpty([
'panes',
$pane,
'copy_address',
]);
}
elseif (isset($contents['copy_address'])) {
$contents['address']['#hidden'] = $this->configuration['default_same_address'];
}
// If this was an Ajax request, update form input values for the
// copy and select address features.
if ($element = $form_state
->getTriggeringElement()) {
$input = $form_state
->getUserInput();
if ($element['#name'] == "panes[{$pane}][copy_address]") {
$address =& $form_state
->getValue([
'panes',
$source,
]);
foreach ($address as $field => $value) {
$input['panes'][$pane][$field] = $value;
}
}
if ($element['#name'] == "panes[{$pane}][select_address]" && isset($addresses[$element['#value']])) {
$address = $addresses[$element['#value']];
foreach ($address as $field => $value) {
$input['panes'][$pane][$field] = $value;
}
$contents['address']['#default_value'] = $order
->getAddress($pane);
}
$form_state
->setUserInput($input);
}
return $contents;
}
/**
* {@inheritdoc}
*/
public function process(OrderInterface $order, array $form, FormStateInterface $form_state) {
$pane = $this->pluginDefinition['id'];
$source = $this
->sourcePaneId();
$address = Address::create();
$panes =& $form_state
->getValue('panes');
foreach ($panes[$pane] as $field => $value) {
if (isset($address->{$field})) {
if (!empty($panes[$pane]['copy_address'])) {
$address->{$field} = $panes[$source][$field];
}
else {
$address->{$field} = $value;
}
}
}
if (isset($panes[$pane]['select_address']) && $panes[$pane]['select_address'] >= 0) {
$addresses = uc_select_addresses(\Drupal::currentUser()
->id(), $pane);
foreach ($addresses[$panes[$pane]['select_address']] as $field => $value) {
$address->{$field} = $value;
}
}
$order
->setAddress($pane, $address);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function review(OrderInterface $order) {
$pane = $this->pluginDefinition['id'];
$address = $order
->getAddress($pane);
$review[] = [
'title' => $this
->t('Address'),
'data' => [
'#markup' => $address,
],
];
if (uc_address_field_enabled('phone') && !empty($address
->getPhone())) {
$review[] = [
'title' => $this
->t('Phone'),
'data' => [
'#plain_text' => $address
->getPhone(),
],
];
}
return $review;
}
/**
* Returns the ID of the source (first) address pane for copying.
*/
protected function sourcePaneId() {
if (!isset(self::$sourcePaneId)) {
self::$sourcePaneId = $this->pluginDefinition['id'];
}
return self::$sourcePaneId;
}
/**
* Ajax callback to re-render the full address element.
*/
public function ajaxRender(array $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];
}
$response = new AjaxResponse();
$id = $this->pluginDefinition['id'] . '-address-pane';
$response
->addCommand(new ReplaceCommand('#' . $id, trim(drupal_render($element['address']))));
$status_messages = [
'#type' => 'status_messages',
];
$response
->addCommand(new PrependCommand('#' . $id, drupal_render($status_messages)));
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddressPaneBase:: |
protected static | property | Source pane for "copy address" checkbox. | |
AddressPaneBase:: |
public | function | Ajax callback to re-render the full address element. | |
AddressPaneBase:: |
abstract protected | function | Returns text to display for the 'copy address' field. | 2 |
AddressPaneBase:: |
abstract protected | function | Returns additional text to display as a description. | 2 |
AddressPaneBase:: |
public | function |
Processes a checkout pane. Overrides CheckoutPanePluginBase:: |
|
AddressPaneBase:: |
public | function |
Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
AddressPaneBase:: |
protected | function | Returns the ID of the source (first) address pane for copying. | |
AddressPaneBase:: |
public | function |
Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
protected | property | Whether the pane is enabled or not. | |
CheckoutPanePluginBase:: |
protected | property | The weight of the checkout pane. | |
CheckoutPanePluginBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Prepares a pane for display. Overrides CheckoutPanePluginInterface:: |
1 |
CheckoutPanePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
CheckoutPanePluginBase:: |
public | function |
Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface:: |
3 |
CheckoutPanePluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
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 | |
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. | |
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. |