You are here

abstract class AddressPaneBase in Ubercart 8.4

Same name in this branch
  1. 8.4 uc_order/src/Plugin/Ubercart/OrderPane/AddressPaneBase.php \Drupal\uc_order\Plugin\Ubercart\OrderPane\AddressPaneBase
  2. 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

Expanded class hierarchy of AddressPaneBase

File

uc_cart/src/Plugin/Ubercart/CheckoutPane/AddressPaneBase.php, line 16

Namespace

Drupal\uc_cart\Plugin\Ubercart\CheckoutPane
View 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

Namesort descending Modifiers Type Description Overrides
AddressPaneBase::$sourcePaneId protected static property Source pane for "copy address" checkbox.
AddressPaneBase::ajaxRender public function Ajax callback to re-render the full address element.
AddressPaneBase::getCopyAddressText abstract protected function Returns text to display for the 'copy address' field. 2
AddressPaneBase::getDescription abstract protected function Returns additional text to display as a description. 2
AddressPaneBase::process public function Processes a checkout pane. Overrides CheckoutPanePluginBase::process
AddressPaneBase::review public function Returns the review contents of a checkout pane. Overrides CheckoutPanePluginInterface::review
AddressPaneBase::sourcePaneId protected function Returns the ID of the source (first) address pane for copying.
AddressPaneBase::view public function Returns the contents of a checkout pane. Overrides CheckoutPanePluginInterface::view
CheckoutPanePluginBase::$status protected property Whether the pane is enabled or not.
CheckoutPanePluginBase::$weight protected property The weight of the checkout pane.
CheckoutPanePluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 3
CheckoutPanePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPanePluginBase::getTitle public function Returns the title of the pane, to be displayed on the checkout form. Overrides CheckoutPanePluginInterface::getTitle
CheckoutPanePluginBase::getWeight public function Returns the weight of the checkout pane. Overrides CheckoutPanePluginInterface::getWeight
CheckoutPanePluginBase::isEnabled public function Returns whether the checkout pane is enabled. Overrides CheckoutPanePluginInterface::isEnabled
CheckoutPanePluginBase::prepare public function Prepares a pane for display. Overrides CheckoutPanePluginInterface::prepare 1
CheckoutPanePluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPanePluginBase::settingsForm public function Returns the settings form for a checkout pane. Overrides CheckoutPanePluginInterface::settingsForm 3
CheckoutPanePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.