commerce-8.x-2.8/modules/store/src/Form/StoreTypeForm.php
modules/store/src/Form/StoreTypeForm.php
<?php
namespace Drupal\commerce_store\Form;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\language\Entity\ContentLanguageSettings;
class StoreTypeForm extends CommerceBundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_store\Entity\StoreTypeInterface $store_type */
$store_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $store_type->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $store_type->id(),
'#machine_name' => [
'exists' => '\Drupal\commerce_store\Entity\StoreType::load',
],
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this->t('Description'),
'#default_value' => $store_type->getDescription(),
];
$form = $this->buildTraitForm($form, $form_state);
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_store',
'bundle' => $store_type->id(),
],
'#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('commerce_store', $store_type->id()),
];
$form['#submit'][] = 'language_configuration_element_submit';
}
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->validateTraitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->submitTraitForm($form, $form_state);
$this->messenger()->addMessage($this->t('Saved the %label store type.', [
'%label' => $this->entity->label(),
]));
$form_state->setRedirect('entity.commerce_store_type.collection');
}
}
