cc-1.0.x-dev/modules/cc_account/src/Form/CcAccountTypeForm.php
modules/cc_account/src/Form/CcAccountTypeForm.php
<?php
namespace Drupal\cc_account\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Form handler for cryptocurrency account type forms.
*/
class CcAccountTypeForm extends BundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$entity_type = $this->entity;
if ($this->operation == 'add') {
$form['#title'] = $this->t('Add cryptocurrency account type');
}
else {
$form['#title'] = $this->t('Edit %label cryptocurrency account type', ['%label' => $entity_type->label()]);
}
$form['label'] = [
'#title' => $this->t('Label'),
'#type' => 'textfield',
'#default_value' => $entity_type->label(),
'#description' => $this->t('The human-readable name of this cryptocurrency account type.'),
'#required' => TRUE,
'#size' => 30,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $entity_type->id(),
'#maxlength' => 32,
'#disabled' => !$entity_type->isNew(),
'#machine_name' => [
'exists' => ['Drupal\cc_account\Entity\CcAccountType', 'load'],
'source' => ['label'],
],
'#description' => $this->t('A unique machine-readable name for this cryptocurrency account type. It must only contain lowercase letters, numbers, and underscores.'),
];
$form['description'] = [
'#title' => $this->t('Description'),
'#type' => 'textarea',
'#default_value' => $entity_type->getDescription(),
'#description' => $this->t('A brief description of this cryptocurrency account type.'),
];
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save account type');
$actions['delete']['#value'] = $this->t('Delete account type');
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity_type = $this->entity;
$status = $entity_type->save();
$message_arguments = ['%label' => $entity_type->label()];
if ($status == SAVED_UPDATED) {
$message = $this->t('The cryptocurrency account type %label has been updated.', $message_arguments);
}
else {
$message = $this->t('The cryptocurrency account type %label has been added.', $message_arguments);
}
$this->messenger()->addStatus($message);
$form_state->setRedirectUrl($entity_type->toUrl('collection'));
return $status;
}
}
