public function ProductTypeForm::form in Commerce Core 8.2
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- modules/
product/ src/ Form/ ProductTypeForm.php, line 66
Class
Namespace
Drupal\commerce_product\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
$product_type = $this->entity;
$variation_types = $this->variationTypeStorage
->loadMultiple();
// Create an empty product to get the default status value.
// @todo Clean up once https://www.drupal.org/node/2318187 is fixed.
if (in_array($this->operation, [
'add',
'duplicate',
])) {
$product = $this->entityTypeManager
->getStorage('commerce_product')
->create([
'type' => $product_type
->uuid(),
]);
$products_exist = FALSE;
}
else {
$storage = $this->entityTypeManager
->getStorage('commerce_product');
$product = $storage
->create([
'type' => $product_type
->id(),
]);
$products_exist = $storage
->getQuery()
->accessCheck(FALSE)
->condition('type', $product_type
->id())
->execute();
}
$form_state
->set('original_entity', $this->entity
->createDuplicate());
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $product_type
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $product_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_product\\Entity\\ProductType::load',
],
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#disabled' => !$product_type
->isNew(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#description' => $this
->t('This text will be displayed on the <em>Add product</em> page.'),
'#default_value' => $product_type
->getDescription(),
];
$form['variationType'] = [
'#type' => 'select',
'#title' => $this
->t('Product variation type'),
'#default_value' => $product_type
->getVariationTypeId(),
'#options' => EntityHelper::extractLabels($variation_types),
'#disabled' => $products_exist,
];
if ($product_type
->isNew()) {
$form['variationType']['#empty_option'] = $this
->t('- Create new -');
$form['variationType']['#description'] = $this
->t('If an existing product variation type is not selected, a new one will be created.');
}
$form['multipleVariations'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Allow each product to have multiple variations.'),
'#default_value' => $product_type
->allowsMultipleVariations(),
];
$form['injectVariationFields'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Inject product variation fields into the rendered product.'),
'#default_value' => $product_type
->shouldInjectVariationFields(),
];
$form['product_status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Publish new products of this type by default.'),
'#default_value' => $product
->isPublished(),
];
$form = $this
->buildTraitForm($form, $form_state);
if ($this->moduleHandler
->moduleExists('language')) {
$form['language'] = [
'#type' => 'details',
'#title' => $this
->t('Language settings'),
'#group' => 'additional_settings',
];
$form['language']['language_configuration'] = [
'#type' => 'language_configuration',
'#entity_information' => [
'entity_type' => 'commerce_product',
'bundle' => $product_type
->id(),
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_product', $product_type
->id()),
];
$form['#submit'][] = 'language_configuration_element_submit';
}
return $form;
}