modules/acm_checkout/src/Plugin/CheckoutPane/StoredAddressTrait.php
protected function buildStoredAddressOptions(array $pane_form, FormStateInterface $form_state, $type = 'billing', array $current_address = []) {
    $field_name = "{$type}_address_options";
    $options = $this->buildAddressOptions();

    $user_input = $form_state->getUserInput();
    $values = NestedArray::getValue($user_input, $pane_form['#parents']);
    $default_option = NULL;
    if (!empty($values[$field_name])) {
      // The form was rebuilt via AJAX, use the submitted value.
      $default_option = $values[$field_name];
    }
    else {
      $default_option = $this->getDefaultAddressOption($type, $options);

      // Check if the saved address matches an option, if so use that as the
      // default.
      if ($default_option != 'new_address') {
        if (isset($current_address['address_id']) && isset($options[$current_address['address_id']])) {
          $default_option = $current_address['address_id'];
        }
        elseif (!empty($current_address)) {
          $default_option = 'new_address';
        }
      }
    }

    // Prepare the form for ajax.
    $pane_form['#wrapper_id'] = "{$type}-information-wrapper";
    $pane_form['#prefix'] = '<div id="' . $pane_form['#wrapper_id'] . '">';
    $pane_form['#suffix'] = '</div>';

    $pane_form[$field_name] = [
      '#type' => 'radios',
      '#options' => array_column($options, 'label', 'id'),
      '#default_value' => $default_option,
      '#ajax' => [
        'callback' => [get_class($this), 'ajaxRefresh'],
        'wrapper' => $pane_form['#wrapper_id'],
      ],
      '#access' => count($options) > 1,
    ];

    $pane_form['address'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => ['address_wrapper'],
      ],
      '#access' => ($default_option == 'new_address') ? TRUE : FALSE,
    ];

    $checkout_config = \Drupal::config('acm_checkout.settings');
    $validate_address = $checkout_config->get("validate_{$type}_address");
    $address_review_text = $checkout_config->get("{$type}_address_review_text");
    $address_failed_text = $checkout_config->get("{$type}_address_failed_text");

    $pane_form['address']['address_fields'] = [
      '#type' => 'acm_address',
      '#default_value' => $current_address,
      '#display_telephone' => TRUE,
      '#display_title' => TRUE,
      '#display_firstname' => TRUE,
      '#display_lastname' => TRUE,
      '#validate_address' => $validate_address,
      '#address_review_text' => $address_review_text,
      '#address_failed_text' => $address_failed_text,
    ];

    return $pane_form;
  }
protected function buildStoredAddressOptions(array $pane_form, FormStateInterface $form_state, $type = 'billing', array $current_address = []) {
    $field_name = "{$type}_address_options";
    $options = $this->buildAddressOptions();

    $user_input = $form_state->getUserInput();
    $values = NestedArray::getValue($user_input, $pane_form['#parents']);
    $default_option = NULL;
    if (!empty($values[$field_name])) {
      // The form was rebuilt via AJAX, use the submitted value.
      $default_option = $values[$field_name];
    }
    else {
      $default_option = $this->getDefaultAddressOption($type, $options);

      // Check if the saved address matches an option, if so use that as the
      // default.
      if ($default_option != 'new_address') {
        if (isset($current_address['address_id']) && isset($options[$current_address['address_id']])) {
          $default_option = $current_address['address_id'];
        }
        elseif (!empty($current_address)) {
          $default_option = 'new_address';
        }
      }
    }

    // Prepare the form for ajax.
    $pane_form['#wrapper_id'] = "{$type}-information-wrapper";
    $pane_form['#prefix'] = '<div id="' . $pane_form['#wrapper_id'] . '">';
    $pane_form['#suffix'] = '</div>';

    $pane_form[$field_name] = [
      '#type' => 'radios',
      '#options' => array_column($options, 'label', 'id'),
      '#default_value' => $default_option,
      '#ajax' => [
        'callback' => [get_class($this), 'ajaxRefresh'],
        'wrapper' => $pane_form['#wrapper_id'],
      ],
      '#access' => count($options) > 1,
    ];

    $pane_form['address'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => ['address_wrapper'],
      ],
      '#access' => ($default_option == 'new_address') ? TRUE : FALSE,
    ];

    $checkout_config = \Drupal::config('acm_checkout.settings');
    $validate_address = $checkout_config->get("validate_{$type}_address");
    $address_review_text = $checkout_config->get("{$type}_address_review_text");
    $address_failed_text = $checkout_config->get("{$type}_address_failed_text");

    $pane_form['address']['address_fields'] = [
      '#type' => 'acm_address',
      '#default_value' => $current_address,
      '#display_telephone' => TRUE,
      '#display_title' => TRUE,
      '#display_firstname' => TRUE,
      '#display_lastname' => TRUE,
      '#validate_address' => $validate_address,
      '#address_review_text' => $address_review_text,
      '#address_failed_text' => $address_failed_text,
    ];

    return $pane_form;
  }
protected function buildStoredAddressOptions ( array $ pane_form , FormStateInterface $ form_state , $ type = 'billing' , array $ current_address = [ ] ) { $ field_name = "{$type}_address_options" ; $ options = $ this -> buildAddressOptions ( ) ; $ user_input = $ form_state -> getUserInput ( ) ; $ values = NestedArray :: getValue ( $ user_input , $ pane_form [ '#parents' ] ) ; $ default_option = NULL ; if ( ! empty ( $ values [ $ field_name ] ) ) { // The form was rebuilt via AJAX, use the submitted value. $ default_option = $ values [ $ field_name ] ; } else { $ default_option = $ this -> getDefaultAddressOption ( $ type , $ options ) ; // Check if the saved address matches an option, if so use that as the // default. if ( $ default_option != 'new_address' ) { if ( isset ( $ current_address [ 'address_id' ] ) && isset ( $ options [ $ current_address [ 'address_id' ] ] ) ) { $ default_option = $ current_address [ 'address_id' ] ; } elseif ( ! empty ( $ current_address ) ) { $ default_option = 'new_address' ; } } } // Prepare the form for ajax. $ pane_form [ '#wrapper_id' ] = "{$type}-information-wrapper" ; $ pane_form [ '#prefix' ] = '<div id="' . $ pane_form [ '#wrapper_id' ] . '">' ; $ pane_form [ '#suffix' ] = '</div>' ; $ pane_form [ $ field_name ] = [ '#type' => 'radios' , '#options' => array_column ( $ options , 'label' , 'id' ) , '#default_value' => $ default_option , '#ajax' => [ 'callback' => [ get_class ( $ this ) , 'ajaxRefresh' ] , 'wrapper' => $ pane_form [ '#wrapper_id' ] , ] , '#access' => count ( $ options ) > 1 , ] ; $ pane_form [ 'address' ] = [ '#type' => 'container' , '#attributes' => [ 'id' => [ 'address_wrapper' ] , ] , '#access' => ( $ default_option == 'new_address' ) ? TRUE : FALSE , ] ; $ checkout_config = \ Drupal :: config ( 'acm_checkout.settings' ) ; $ validate_address = $ checkout_config -> get ( "validate_{$type}_address" ) ; $ address_review_text = $ checkout_config -> get ( "{$type}_address_review_text" ) ; $ address_failed_text = $ checkout_config -> get ( "{$type}_address_failed_text" ) ; $ pane_form [ 'address' ] [ 'address_fields' ] = [ '#type' => 'acm_address' , '#default_value' => $ current_address , '#display_telephone' => TRUE , '#display_title' => TRUE , '#display_firstname' => TRUE , '#display_lastname' => TRUE , '#validate_address' => $ validate_address , '#address_review_text' => $ address_review_text , '#address_failed_text' => $ address_failed_text , ] ; return $ pane_form ; }
Decorates the form with the stored address options.

@param array $pane_form
The pane form, containing the following basic properties:
- #parents: Identifies the position of the pane form in the overall
parent form, and identifies the location where the field values are
placed within $form_state->getValues().
@param \Drupal\Core\Form\FormStateInterface $form_state
The form state of the parent form.
@param string $type
Whether it's shipping or billing address.
@param array $current_address
The current set address.

@return array
The updated form array.
Decorates the form with the stored address options .