public function ProductTypeForm::save in Commerce Core 8.2
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
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
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- modules/
product/ src/ Form/ ProductTypeForm.php, line 205
Class
Namespace
Drupal\commerce_product\FormCode
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
$product_type = $this->entity;
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $original_product_type */
$original_product_type = $form_state
->get('original_entity');
$product_type
->save();
$this
->postSave($product_type, $this->operation);
$this
->submitTraitForm($form, $form_state);
// Create the needed fields.
if ($this->operation == 'add') {
commerce_product_add_body_field($product_type);
}
// Update the widget for the variations field.
$form_display = commerce_get_entity_display('commerce_product', $product_type
->id(), 'form');
if ($product_type
->allowsMultipleVariations() && !$original_product_type
->allowsMultipleVariations()) {
// When multiple variations are allowed, the variations tab is used
// to manage them, no widget is needed.
$form_display
->removeComponent('variations');
$form_display
->save();
}
elseif (!$product_type
->allowsMultipleVariations() && $original_product_type
->allowsMultipleVariations()) {
// When only a single variation is allowed, use the dedicated widget.
$form_display
->setComponent('variations', [
'type' => 'commerce_product_single_variation',
'weight' => 2,
]);
$form_display
->save();
}
// Update the default value of the status field.
$product_type_id = $product_type
->id();
$product = $this->entityTypeManager
->getStorage('commerce_product')
->create([
'type' => $product_type_id,
]);
$value = (bool) $form_state
->getValue('product_status');
if ($product->status->value != $value) {
$fields = $this->entityFieldManager
->getFieldDefinitions('commerce_product', $product_type_id);
$fields['status']
->getConfig($product_type_id)
->setDefaultValue($value)
->save();
$this->entityFieldManager
->clearCachedFieldDefinitions();
}
$this
->messenger()
->addMessage($this
->t('The product type %label has been successfully saved.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirect('entity.commerce_product_type.collection');
}