ProductTypeForm.php 6.14 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\commerce_product\Form;

5 6
use Drupal\commerce\EntityTraitManagerInterface;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Entity\EntityFieldManagerInterface;
9
use Drupal\Core\Entity\EntityTypeInterface;
bojanz's avatar
bojanz committed
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\language\Entity\ContentLanguageSettings;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13

14
class ProductTypeForm extends CommerceBundleEntityFormBase {
bojanz's avatar
bojanz committed
15

16 17 18 19 20 21 22
  /**
   * The variation type storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $variationTypeStorage;

23 24 25 26 27 28 29
  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

30 31 32
  /**
   * Creates a new ProductTypeForm object.
   *
33 34
   * @param \Drupal\commerce\EntityTraitManagerInterface $trait_manager
   *   The entity trait manager.
35
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
36
   *   The entity type manager.
37
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
38
   *   The entity field manager.
39
   */
40 41 42
  public function __construct(EntityTraitManagerInterface $trait_manager, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
    parent::__construct($trait_manager);

43 44
    $this->variationTypeStorage = $entity_type_manager->getStorage('commerce_product_variation_type');
    $this->entityFieldManager = $entity_field_manager;
45 46 47 48 49 50
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
51
    return new static(
52
      $container->get('plugin.manager.commerce_entity_trait'),
53 54 55
      $container->get('entity_type.manager'),
      $container->get('entity_field.manager')
    );
56 57
  }

58 59 60
  /**
   * {@inheritdoc}
   */
bojanz's avatar
bojanz committed
61
  public function form(array $form, FormStateInterface $form_state) {
62
    $form = parent::form($form, $form_state);
63 64 65
    /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
    $product_type = $this->entity;
    $variation_types = $this->variationTypeStorage->loadMultiple();
66
    $variation_types = array_map(function ($variation_type) {
67 68
      return $variation_type->label();
    }, $variation_types);
69 70 71
    // Create an empty product to get the default status value.
    // @todo Clean up once https://www.drupal.org/node/2318187 is fixed.
    if ($this->operation == 'add') {
72
      $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $product_type->uuid()]);
73 74
    }
    else {
75
      $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $product_type->id()]);
76
    }
77

78
    $form['label'] = [
79 80 81
      '#type' => 'textfield',
      '#title' => $this->t('Label'),
      '#maxlength' => 255,
82
      '#default_value' => $product_type->label(),
83
      '#required' => TRUE,
84 85
    ];
    $form['id'] = [
86
      '#type' => 'machine_name',
87
      '#default_value' => $product_type->id(),
88
      '#machine_name' => [
89
        'exists' => '\Drupal\commerce_product\Entity\ProductType::load',
90
      ],
91
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
92 93
    ];
    $form['description'] = [
bojanz's avatar
bojanz committed
94
      '#type' => 'textarea',
95
      '#title' => $this->t('Description'),
bojanz's avatar
bojanz committed
96
      '#description' => $this->t('This text will be displayed on the <em>Add product</em> page.'),
97
      '#default_value' => $product_type->getDescription(),
98
    ];
99 100 101
    $form['variationType'] = [
      '#type' => 'select',
      '#title' => $this->t('Product variation type'),
102
      '#default_value' => $product_type->getVariationTypeId(),
103
      '#options' => $variation_types,
104
      '#required' => TRUE,
105
      '#disabled' => !$product_type->isNew(),
106
    ];
107 108 109 110 111
    $form['injectVariationFields'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Inject product variation fields into the rendered product.'),
      '#default_value' => $product_type->shouldInjectVariationFields(),
    ];
112 113 114 115 116
    $form['product_status'] = [
      '#type' => 'checkbox',
      '#title' => t('Publish new products of this type by default.'),
      '#default_value' => $product->isPublished(),
    ];
117
    $form = $this->buildTraitForm($form, $form_state);
118

119 120 121 122 123 124 125 126 127 128
    if ($this->moduleHandler->moduleExists('language')) {
      $form['language'] = [
        '#type' => 'details',
        '#title' => $this->t('Language settings'),
        '#group' => 'additional_settings',
      ];
      $form['language']['language_configuration'] = [
        '#type' => 'language_configuration',
        '#entity_information' => [
          'entity_type' => 'commerce_product',
129
          'bundle' => $product_type->id(),
130
        ],
131
        '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_product', $product_type->id()),
132 133 134 135
      ];
      $form['#submit'][] = 'language_configuration_element_submit';
    }

136
    return $this->protectBundleIdElement($form);
137 138
  }

139 140 141 142 143 144 145
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $this->validateTraitForm($form, $form_state);
  }

146 147 148
  /**
   * {@inheritdoc}
   */
bojanz's avatar
bojanz committed
149
  public function save(array $form, FormStateInterface $form_state) {
150
    $status = $this->entity->save();
151 152 153 154 155 156 157 158
    // Update the default value of the status field.
    $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $this->entity->id()]);
    $value = (bool) $form_state->getValue('product_status');
    if ($product->status->value != $value) {
      $fields = $this->entityFieldManager->getFieldDefinitions('commerce_product', $this->entity->id());
      $fields['status']->getConfig($this->entity->id())->setDefaultValue($value)->save();
      $this->entityFieldManager->clearCachedFieldDefinitions();
    }
159
    $this->submitTraitForm($form, $form_state);
160

161 162 163
    drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()]));
    $form_state->setRedirect('entity.commerce_product_type.collection');
    if ($status == SAVED_NEW) {
164
      commerce_product_add_stores_field($this->entity);
bojanz's avatar
bojanz committed
165 166
      commerce_product_add_body_field($this->entity);
      commerce_product_add_variations_field($this->entity);
167 168
    }
  }
bojanz's avatar
bojanz committed
169

170
}