PriceListTypeForm.php in Commerce Pricelist 8
File
src/Form/PriceListTypeForm.php
View source
<?php
namespace Drupal\commerce_pricelist\Form;
use Drupal\commerce\EntityTraitManagerInterface;
use Drupal\commerce\Form\CommerceBundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PriceListTypeForm extends CommerceBundleEntityFormBase {
protected $messenger;
public function __construct(EntityTraitManagerInterface $trait_manager, MessengerInterface $messenger) {
parent::__construct($trait_manager);
$this->traitManager = $trait_manager;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.commerce_entity_trait'), $container
->get('messenger'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$price_list_type = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $price_list_type
->label(),
'#description' => $this
->t("Label for the Price list type."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $price_list_type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_pricelist\\Entity\\PriceListType::load',
],
'#disabled' => !$price_list_type
->isNew(),
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $price_list_type
->getDescription(),
];
$form = $this
->buildTraitForm($form, $form_state);
return $this
->protectBundleIdElement($form);
}
public function save(array $form, FormStateInterface $form_state) {
$price_list_type = $this->entity;
$status = $price_list_type
->save();
switch ($status) {
case SAVED_NEW:
$this->messenger
->addMessage($this
->t('Created the %label Price list type.', [
'%label' => $price_list_type
->label(),
]));
break;
default:
$this->messenger
->addMessage($this
->t('Saved the %label Price list type.', [
'%label' => $price_list_type
->label(),
]));
}
$form_state
->setRedirectUrl($price_list_type
->urlInfo('collection'));
}
}