You are here

protected function CommerceBundleEntityFormBase::buildTraitForm in Commerce Core 8.2

Builds the trait form.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The parent form with the trait form elements added.

5 calls to CommerceBundleEntityFormBase::buildTraitForm()
OrderItemTypeForm::form in modules/order/src/Form/OrderItemTypeForm.php
Gets the actual form array to be built.
OrderTypeForm::form in modules/order/src/Form/OrderTypeForm.php
Gets the actual form array to be built.
ProductTypeForm::form in modules/product/src/Form/ProductTypeForm.php
Gets the actual form array to be built.
ProductVariationTypeForm::form in modules/product/src/Form/ProductVariationTypeForm.php
Gets the actual form array to be built.
StoreTypeForm::form in modules/store/src/Form/StoreTypeForm.php
Gets the actual form array to be built.

File

src/Form/CommerceBundleEntityFormBase.php, line 49

Class

CommerceBundleEntityFormBase

Namespace

Drupal\commerce\Form

Code

protected function buildTraitForm(array $form, FormStateInterface $form_state) {
  $target_entity_type_id = $this->entity
    ->getEntityType()
    ->getBundleOf();

  /** @var \Drupal\commerce\Entity\CommerceBundleEntityInterface $entity */
  $entity = $this->entity;
  $used_traits = $entity
    ->getTraits();
  $traits = $this->traitManager
    ->getDefinitionsByEntityType($target_entity_type_id);
  $trait_options = array_map(function ($trait) {
    return $trait['label'];
  }, $traits);
  asort($trait_options);
  $form['original_traits'] = [
    '#type' => 'value',
    '#value' => $used_traits,
  ];
  $form['traits'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Traits'),
    '#options' => $trait_options,
    '#default_value' => $used_traits,
    '#access' => count($traits) > 0,
  ];

  // Disable options which cannot be unset because of existing data.
  $disabled_traits = [];
  if (!$entity
    ->isNew()) {
    foreach ($used_traits as $trait_id) {
      $trait = $this->traitManager
        ->createInstance($trait_id);
      if (!$this->traitManager
        ->canUninstallTrait($trait, $target_entity_type_id, $this->entity
        ->id())) {
        $form['traits'][$trait_id] = [
          '#disabled' => TRUE,
        ];
        $disabled_traits[] = $trait_id;
      }
    }
  }
  $form['disabled_traits'] = [
    '#type' => 'value',
    '#value' => $disabled_traits,
  ];
  return $form;
}