View source
<?php
namespace Drupal\commerce_order\Form;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
trait CustomerFormTrait {
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 {
$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,
];
$form['customer']['password']['password_confirm_wrapper'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="generate"]' => [
'checked' => FALSE,
],
],
],
];
$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;
}
public function customerFormAjax(array $form, FormStateInterface $form_state) {
return $form['customer'];
}
public function submitCustomerForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if ($values['customer_type'] == 'existing') {
$values['mail'] = User::load($values['uid'])
->getEmail();
}
else {
$user = User::create([
'name' => $values['mail'],
'mail' => $values['mail'],
'pass' => $values['generate'] ? user_password() : $values['pass'],
'status' => TRUE,
]);
$user
->save();
$values['uid'] = $user
->id();
if ($values['notify']) {
_user_mail_notify('register_admin_created', $user);
}
}
$form_state
->setValues($values);
}
}