You are here

public function ShipmentTypeForm::form in Commerce Shipping 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

src/Form/ShipmentTypeForm.php, line 49

Class

ShipmentTypeForm

Namespace

Drupal\commerce_shipping\Form

Code

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

  /** @var \Drupal\commerce_shipping\Entity\ShipmentTypeInterface $shipment_type */
  $shipment_type = $this->entity;
  $profile_types = $this->addressBook
    ->loadTypes();
  $shipments_exist = FALSE;
  if (!$this->entity
    ->isNew()) {
    $shipment_storage = $this->entityTypeManager
      ->getStorage('commerce_shipment');
    $shipments_exist = (bool) $shipment_storage
      ->getQuery()
      ->accessCheck(FALSE)
      ->condition('type', $shipment_type
      ->id())
      ->execute();
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $shipment_type
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $shipment_type
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_shipping\\Entity\\ShipmentType::load',
    ],
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
  ];
  $form['profileType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Profile type'),
    '#default_value' => $shipment_type
      ->getProfileTypeId(),
    '#options' => EntityHelper::extractLabels($profile_types),
    '#required' => TRUE,
    '#disabled' => $shipments_exist,
  ];
  $form['emails']['sendConfirmation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Send customer an email confirmation when shipped'),
    '#default_value' => $shipment_type
      ->isNew() ? TRUE : $shipment_type
      ->shouldSendConfirmation(),
  ];
  $form['emails']['confirmationBcc'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Send a copy of the shipment confirmation to this email:'),
    '#default_value' => $shipment_type
      ->isNew() ? '' : $shipment_type
      ->getConfirmationBcc(),
    '#states' => [
      'visible' => [
        ':input[name="sendConfirmation"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form = $this
    ->buildTraitForm($form, $form_state);
  return $this
    ->protectBundleIdElement($form);
}