You are here

public function OrderItemTypeForm::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/order/src/Form/OrderItemTypeForm.php, line 19

Class

OrderItemTypeForm

Namespace

Drupal\commerce_order\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $order_item_type = $this->entity;

  // Prepare the list of purchasable entity types.
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $purchasable_entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
    return $entity_type
      ->entityClassImplements(PurchasableEntityInterface::class);
  });
  $purchasable_entity_types = array_map(function (EntityTypeInterface $entity_type) {
    return $entity_type
      ->getLabel();
  }, $purchasable_entity_types);
  $order_types = $this->entityTypeManager
    ->getStorage('commerce_order_type')
    ->loadMultiple();
  $form['#tree'] = TRUE;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $order_item_type
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $order_item_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_order\\Entity\\OrderItemType::load',
      'source' => [
        'label',
      ],
    ],
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => !$order_item_type
      ->isNew(),
  ];
  $form['purchasableEntityType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Purchasable entity type'),
    '#default_value' => $order_item_type
      ->getPurchasableEntityTypeId(),
    '#options' => $purchasable_entity_types,
    '#empty_value' => '',
    '#disabled' => !$order_item_type
      ->isNew(),
  ];
  $form['orderType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Order type'),
    '#default_value' => $order_item_type
      ->getOrderTypeId(),
    '#options' => EntityHelper::extractLabels($order_types),
    '#required' => TRUE,
  ];
  $form = $this
    ->buildTraitForm($form, $form_state);
  return $form;
}