You are here

public function OrderTypeForm::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/OrderTypeForm.php, line 56

Class

OrderTypeForm
Provides an order type form.

Namespace

Drupal\commerce_order\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  $order_type = $this->entity;
  $workflows = $this->workflowManager
    ->getGroupedLabels('commerce_order');
  $number_pattern_storage = $this->entityTypeManager
    ->getStorage('commerce_number_pattern');
  $number_patterns = $number_pattern_storage
    ->loadByProperties([
    'targetEntityType' => 'commerce_order',
  ]);
  $form['#tree'] = TRUE;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $order_type
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $order_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_order\\Entity\\OrderType::load',
      'source' => [
        'label',
      ],
    ],
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => !$order_type
      ->isNew(),
  ];
  $form['workflow'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Workflow'),
    '#options' => $workflows,
    '#default_value' => $order_type
      ->getWorkflowId(),
    '#description' => $this
      ->t('Used by all orders of this type.'),
  ];
  $form['generate_number'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Generate a sequential order number when the order is placed'),
    '#default_value' => (bool) $order_type
      ->getNumberPatternId(),
  ];
  $form['numberPattern'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Number pattern'),
    '#default_value' => $order_type
      ->getNumberPatternId(),
    '#options' => EntityHelper::extractLabels($number_patterns),
    '#states' => [
      'visible' => [
        ':input[name="generate_number"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form = $this
    ->buildTraitForm($form, $form_state);
  $form['refresh'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Order refresh'),
    '#weight' => 5,
    '#open' => TRUE,
    '#tree' => FALSE,
  ];
  $form['refresh']['refresh_intro'] = [
    '#markup' => '<p>' . $this
      ->t('These settings let you control how draft orders are refreshed, the process during which prices are recalculated.') . '</p>',
  ];
  $form['refresh']['refresh_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Order refresh mode'),
    '#options' => [
      OrderType::REFRESH_ALWAYS => $this
        ->t('Refresh a draft order when it is loaded regardless of who it belongs to.'),
      OrderType::REFRESH_CUSTOMER => $this
        ->t('Only refresh a draft order when it is loaded if it belongs to the current user.'),
    ],
    '#default_value' => $order_type
      ->isNew() ? OrderType::REFRESH_CUSTOMER : $order_type
      ->getRefreshMode(),
  ];
  $form['refresh']['refresh_frequency'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Order refresh frequency'),
    '#description' => $this
      ->t('Draft orders will only be refreshed if more than the specified number of seconds have passed since they were last refreshed.'),
    '#default_value' => $order_type
      ->isNew() ? 300 : $order_type
      ->getRefreshFrequency(),
    '#required' => TRUE,
    '#min' => 1,
    '#size' => 10,
    '#field_suffix' => $this
      ->t('seconds'),
  ];
  $form['emails'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Emails'),
    '#weight' => 5,
    '#open' => TRUE,
    '#collapsible' => TRUE,
    '#tree' => FALSE,
  ];
  $form['emails']['notice'] = [
    '#markup' => '<p>' . $this
      ->t('Emails are sent in the HTML format. You will need a module such as <a href="https://www.drupal.org/project/swiftmailer">Swiftmailer</a> to send HTML emails.') . '</p>',
  ];
  $form['emails']['sendReceipt'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Email the customer a receipt when an order is placed'),
    '#default_value' => $order_type
      ->isNew() ? TRUE : $order_type
      ->shouldSendReceipt(),
  ];
  $form['emails']['receiptBcc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Send a copy of the receipt to this email:'),
    '#default_value' => $order_type
      ->isNew() ? '' : $order_type
      ->getReceiptBcc(),
    '#states' => [
      'visible' => [
        ':input[name="sendReceipt"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return $form;
}