3 namespace Drupal\{{ machine_name }}\Form;
5 use Drupal\Core\Entity\BundleEntityFormBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Form handler for {{ entity_type_label|lower }} type forms.
14 class {{ class_prefix }}TypeForm extends BundleEntityFormBase {
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityManager;
24 * Constructs the {{ class_prefix }}TypeForm object.
26 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
29 public function __construct(EntityManagerInterface $entity_manager) {
30 $this->entityManager = $entity_manager;
36 public static function create(ContainerInterface $container) {
38 $container->get('entity.manager')
45 public function form(array $form, FormStateInterface $form_state) {
46 $form = parent::form($form, $form_state);
48 $entity_type = $this->entity;
49 if ($this->operation == 'add') {
50 $form['#title'] = $this->t('Add {{ entity_type_label|lower }} type');
53 $form['#title'] = $this->t(
54 'Edit %label {{ entity_type_label|lower }} type',
55 ['%label' => $entity_type->label()]
60 '#title' => $this->t('Label'),
61 '#type' => 'textfield',
62 '#default_value' => $entity_type->label(),
63 '#description' => $this->t('The human-readable name of this {{ entity_type_label|lower }} type.'),
69 '#type' => 'machine_name',
70 '#default_value' => $entity_type->id(),
71 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
73 'exists' => ['Drupal\{{ machine_name }}\Entity\{{ class_prefix }}Type', 'load'],
74 'source' => ['label'],
76 '#description' => $this->t('A unique machine-readable name for this {{ entity_type_label|lower }} type. It must only contain lowercase letters, numbers, and underscores.'),
79 return $this->protectBundleIdElement($form);
85 protected function actions(array $form, FormStateInterface $form_state) {
86 $actions = parent::actions($form, $form_state);
87 $actions['submit']['#value'] = $this->t('Save {{ entity_type_label|lower }} type');
88 $actions['delete']['#value'] = $this->t('Delete {{ entity_type_label|lower }} type');
95 public function save(array $form, FormStateInterface $form_state) {
96 $entity_type = $this->entity;
98 $entity_type->set('id', trim($entity_type->id()));
99 $entity_type->set('label', trim($entity_type->label()));
101 $status = $entity_type->save();
103 $t_args = ['%name' => $entity_type->label()];
105 if ($status == SAVED_UPDATED) {
106 $message = 'The {{ entity_type_label|lower }} type %name has been updated.';
108 elseif ($status == SAVED_NEW) {
109 $message = 'The {{ entity_type_label|lower }} type %name has been added.';
111 drupal_set_message($this->t($message, $t_args));
113 $this->entityManager->clearCachedFieldDefinitions();
114 $form_state->setRedirectUrl($entity_type->urlInfo('collection'));