public function PriceListForm::form in Commerce Pricelist 8.2
Same name and namespace in other branches
- 8 src/Form/PriceListForm.php \Drupal\commerce_pricelist\Form\PriceListForm::form()
Gets the actual form array to be built.
Overrides ContentEntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ PriceListForm.php, line 34
Class
Namespace
Drupal\commerce_pricelist\FormCode
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_pricelist\Entity\PriceListInterface $price_list */
$price_list = $this->entity;
$form = parent::form($form, $form_state);
$form['#tree'] = TRUE;
$form['#theme'] = [
'commerce_pricelist_form',
];
$form['#attached']['library'][] = 'commerce_pricelist/form';
$form['advanced'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity-meta',
],
],
'#weight' => 99,
];
$form['option_details'] = [
'#type' => 'container',
'#title' => $this
->t('Options'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#weight' => -100,
];
$form['date_details'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Dates'),
'#group' => 'advanced',
];
$field_details_mapping = [
'status' => 'option_details',
'weight' => 'option_details',
'start_date' => 'date_details',
'end_date' => 'date_details',
];
foreach ($field_details_mapping as $field => $group) {
if (isset($form[$field])) {
$form[$field]['#group'] = $group;
}
}
// Hide the customer/customer_roles fields behind a set of radios, to
// emphasize that they are mutually exclusive.
$default_value = 'everyone';
if (!$price_list
->get('customers')
->isEmpty()) {
$default_value = 'customers';
}
elseif ($price_list
->getCustomerRoles()) {
$default_value = 'customer_roles';
}
$form['customer_eligibility'] = [
'#type' => 'radios',
'#title' => $this
->t('Customer eligibility'),
'#options' => [
'everyone' => $this
->t('Everyone'),
'customers' => $this
->t('Specific customers'),
'customer_roles' => $this
->t('Customer roles'),
],
'#default_value' => $default_value,
'#weight' => 10,
];
$form['customers']['#states']['visible'] = [
'input[name="customer_eligibility"]' => [
'value' => 'customers',
],
];
$form['customer_roles']['widget']['#states']['visible'] = [
'input[name="customer_eligibility"]' => [
'value' => 'customer_roles',
],
];
// Remove the '- None -' option from the customer roles dropdown.
if ($form['customer_roles']['widget']['#type'] == 'select') {
unset($form['customer_roles']['widget']['#options']['_none']);
}
// EntityFormDisplay::processForm() overwrites any widget #weight set
// here, so the new weights must be assigned in a #process of our own.
$form['#process'][] = [
get_class($this),
'modifyCustomerFieldWeights',
];
return $form;
}