public function PromotionForm::form in Commerce Core 8.2
Gets the actual form array to be built.
Overrides ContentEntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- modules/
promotion/ src/ Form/ PromotionForm.php, line 37
Class
- PromotionForm
- Defines the promotion add/edit form.
Namespace
Drupal\commerce_promotion\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['#tree'] = TRUE;
// By default an offer is preselected on the add form because the field
// is required. Select an empty value instead, to force the user to choose.
$user_input = $form_state
->getUserInput();
if ($this->operation == 'add' && $this->entity
->get('offer')
->isEmpty()) {
if (!empty($form['offer']['widget'][0]['target_plugin_id'])) {
$form['offer']['widget'][0]['target_plugin_id']['#empty_value'] = '';
if (empty($user_input['offer'][0]['target_plugin_id'])) {
$form['offer']['widget'][0]['target_plugin_id']['#default_value'] = '';
unset($form['offer']['widget'][0]['target_plugin_configuration']);
}
}
}
$translating = !$this
->isDefaultFormLangcode($form_state);
$hide_non_translatable_fields = $this->entity
->isDefaultTranslationAffectedOnly();
// The second column is empty when translating with non-translatable
// fields hidden, so there's no reason to add it.
if ($translating && $hide_non_translatable_fields) {
return $form;
}
$form['#theme'] = [
'commerce_promotion_form',
];
$form['#attached']['library'][] = 'commerce_promotion/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',
];
$form['usage_details'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Usage limits'),
'#group' => 'advanced',
];
$form['compatibility_details'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Compatibility'),
'#group' => 'advanced',
];
$field_details_mapping = [
'status' => 'option_details',
'weight' => 'option_details',
'order_types' => 'option_details',
'stores' => 'option_details',
'start_date' => 'date_details',
'end_date' => 'date_details',
'usage_limit' => 'usage_details',
'usage_limit_customer' => 'usage_details',
'compatibility' => 'compatibility_details',
];
foreach ($field_details_mapping as $field => $group) {
if (isset($form[$field])) {
$form[$field]['#group'] = $group;
}
}
return $form;
}