public function ProductBundleTypeForm::form in Commerce Product Bundle 8
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- src/
Form/ ProductBundleTypeForm.php, line 49
Class
- ProductBundleTypeForm
- Provides the product bundle type form.
Namespace
Drupal\commerce_product_bundle\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_product_bundle\Entity\BundleTypeInterface $product_bundle_type */
$product_bundle_type = $this->entity;
$bundle_item_types = $this->bundleItemTypeStorage
->loadMultiple();
$bundle_item_types = array_map(function ($bundle_item_type) {
return $bundle_item_type
->label();
}, $bundle_item_types);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $product_bundle_type
->label(),
'#description' => $this
->t("Label for the product bundle type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $product_bundle_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_product_bundle\\Entity\\ProductBundleType::load',
],
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'#disabled' => !$product_bundle_type
->isNew(),
];
$form['description'] = [
'#type' => 'textarea',
'#title' => $this
->t('Description'),
'#description' => $this
->t('This text will be displayed on the <em>Add product bundle</em> page.'),
'#default_value' => $product_bundle_type
->getDescription(),
];
$form['bundleItemType'] = [
'#type' => 'select',
'#title' => $this
->t('Product bundle item type'),
'#default_value' => $product_bundle_type
->getBundleItemTypeId(),
'#options' => $bundle_item_types,
'#required' => TRUE,
'#disabled' => !$product_bundle_type
->isNew(),
];
if ($this->moduleHandler
->moduleExists('commerce_order')) {
// Prepare a list of order item types used to purchase product bundles.
$order_item_type_storage = $this->entityTypeManager
->getStorage('commerce_order_item_type');
$order_item_types = $order_item_type_storage
->loadMultiple();
$order_item_types = array_filter($order_item_types, function ($order_item_type) {
return $order_item_type
->getPurchasableEntityTypeId() == 'commerce_product_bundle';
});
$order_item_types = array_map(function ($order_item_type) {
return $order_item_type
->label();
}, $order_item_types);
$form['orderItemType'] = [
'#type' => 'select',
'#title' => $this
->t('Order item type'),
'#default_value' => $product_bundle_type
->getOrderItemTypeId(),
'#options' => $order_item_types,
'#empty_value' => '',
'#required' => TRUE,
];
}
return $form;
}