You are here

public function CustomerFormTrait::buildCustomerForm in Commerce Core 8.2

Builds the customer form.

Parameters

array $form: The parent form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

\Drupal\commerce_order\Entity\OrderInterface $order: The current order, if known.

Return value

array The parent form with the customer form elements added.

2 calls to CustomerFormTrait::buildCustomerForm()
OrderAddForm::buildForm in modules/order/src/Form/OrderAddForm.php
Form constructor.
OrderReassignForm::buildForm in modules/order/src/Form/OrderReassignForm.php
Form constructor.

File

modules/order/src/Form/CustomerFormTrait.php, line 30

Class

CustomerFormTrait
Provides a form for selecting the order's customer (uid and mail fields).

Namespace

Drupal\commerce_order\Form

Code

public function buildCustomerForm(array $form, FormStateInterface $form_state, OrderInterface $order = NULL) {
  $selected_customer_type = $form_state
    ->getValue([
    'customer_type',
  ], 'existing');
  $wrapper_id = Html::getUniqueId('customer-fieldset-wrapper');
  $form['customer'] = [
    '#type' => 'fieldset',
    '#title' => t('Customer'),
    '#prefix' => '<div id="' . $wrapper_id . '">',
    '#suffix' => '</div>',
  ];
  $form['customer']['customer_type'] = [
    '#type' => 'radios',
    '#title' => t('Order for'),
    '#title_display' => 'invisible',
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#required' => TRUE,
    '#options' => [
      'existing' => t('Existing customer'),
      'new' => t('New customer'),
    ],
    '#default_value' => $selected_customer_type,
    '#ajax' => [
      'callback' => [
        $this,
        'customerFormAjax',
      ],
      'wrapper' => $wrapper_id,
    ],
  ];
  if ($selected_customer_type == 'existing') {
    $form['customer']['uid'] = [
      '#type' => 'entity_autocomplete',
      '#title' => t('Search'),
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
      '#placeholder' => t('Search by username or email address'),
      '#target_type' => 'user',
      '#required' => TRUE,
      '#selection_settings' => [
        'match_operator' => 'CONTAINS',
        'include_anonymous' => FALSE,
      ],
    ];
  }
  else {

    // New customer.
    $form['customer']['uid'] = [
      '#type' => 'value',
      '#value' => 0,
    ];
    $form['customer']['mail'] = [
      '#type' => 'email',
      '#title' => t('Email'),
      '#required' => TRUE,
    ];
    $form['customer']['password'] = [
      '#type' => 'container',
    ];
    $form['customer']['password']['generate'] = [
      '#type' => 'checkbox',
      '#title' => t('Generate password'),
      '#default_value' => 1,
    ];

    // The password_confirm element needs to be wrapped in order for #states
    // to work properly. See https://www.drupal.org/node/1427838.
    $form['customer']['password']['password_confirm_wrapper'] = [
      '#type' => 'container',
      '#states' => [
        'visible' => [
          ':input[name="generate"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];

    // We cannot make this required due to HTML5 validation.
    $form['customer']['password']['password_confirm_wrapper']['pass'] = [
      '#type' => 'password_confirm',
      '#size' => 25,
    ];
    $form['customer']['notify'] = [
      '#type' => 'checkbox',
      '#title' => t('Notify user of new account'),
    ];
  }
  return $form;
}