You are here

public function ProductAttributeForm::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/product/src/Form/ProductAttributeForm.php, line 56

Class

ProductAttributeForm

Namespace

Drupal\commerce_product\Form

Code

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

  /** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */
  $attribute = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#maxlength' => 255,
    '#default_value' => $attribute
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $attribute
      ->id(),
    '#machine_name' => [
      'exists' => '\\Drupal\\commerce_product\\Entity\\ProductAttribute::load',
    ],
    // Attribute field names are constructed as 'attribute_' + id, and must
    // not be longer than 32 characters. Account for that prefix length here.
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH - 10,
    '#disabled' => !$attribute
      ->isNew(),
  ];
  $form['elementType'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Element type'),
    '#description' => $this
      ->t('Controls how the attribute is displayed on the add to cart form.'),
    '#options' => [
      'radios' => $this
        ->t('Radio buttons'),
      'select' => $this
        ->t('Select list'),
      'commerce_product_rendered_attribute' => $this
        ->t('Rendered attribute'),
    ],
    '#default_value' => $attribute
      ->getElementType(),
  ];
  $attribute_field_map = $this->attributeFieldManager
    ->getFieldMap();
  $variation_type_storage = $this->entityTypeManager
    ->getStorage('commerce_product_variation_type');
  $variation_types = $variation_type_storage
    ->loadMultiple();

  // Allow the attribute to be assigned to a product variation type.
  $form['original_variation_types'] = [
    '#type' => 'value',
    '#value' => [],
  ];
  $form['variation_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Product variation types'),
    '#options' => EntityHelper::extractLabels($variation_types),
    '#access' => count($variation_types) > 0,
  ];
  $disabled_variation_types = [];
  foreach ($variation_types as $variation_type_id => $variation_type) {
    if (!$attribute
      ->isNew() && isset($attribute_field_map[$variation_type_id])) {
      $used_attributes = array_column($attribute_field_map[$variation_type_id], 'attribute_id');
      if (in_array($attribute
        ->id(), $used_attributes)) {
        $form['original_variation_types']['#value'][$variation_type_id] = $variation_type_id;
        $form['variation_types']['#default_value'][$variation_type_id] = $variation_type_id;
        if (!$this->attributeFieldManager
          ->canDeleteField($attribute, $variation_type_id)) {
          $form['variation_types'][$variation_type_id] = [
            '#disabled' => TRUE,
          ];
          $disabled_variation_types[] = $variation_type_id;
        }
      }
    }
  }
  $form['disabled_variation_types'] = [
    '#type' => 'value',
    '#value' => $disabled_variation_types,
  ];
  if ($this->moduleHandler
    ->moduleExists('content_translation')) {
    $enabled = TRUE;
    if (!$attribute
      ->isNew()) {
      $translation_manager = \Drupal::service('content_translation.manager');
      $enabled = $translation_manager
        ->isEnabled('commerce_product_attribute_value', $attribute
        ->id());
    }
    $form['enable_value_translation'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable attribute value translation'),
      '#default_value' => $enabled,
    ];
  }

  // The attribute acts as a bundle for attribute values, so the values can't
  // be created until the attribute is saved.
  if (!$attribute
    ->isNew()) {
    $form = $this
      ->buildValuesForm($form, $form_state);
  }
  return $form;
}