Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ContactDetailTypeForm | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| form | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
2 | |||
| actions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\crm\Form; |
| 6 | |
| 7 | use Drupal\Core\Entity\BundleEntityFormBase; |
| 8 | use Drupal\Core\Entity\EntityTypeInterface; |
| 9 | use Drupal\Core\Form\FormStateInterface; |
| 10 | use Drupal\crm\Entity\ContactDetailType; |
| 11 | |
| 12 | /** |
| 13 | * Form handler for CRM contact detail type forms. |
| 14 | */ |
| 15 | class ContactDetailTypeForm extends BundleEntityFormBase { |
| 16 | |
| 17 | const SAVED_NEW = 1; |
| 18 | const SAVED_UPDATED = 2; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | public function form(array $form, FormStateInterface $form_state): array { |
| 24 | $form = parent::form($form, $form_state); |
| 25 | $entity_type = $this->entity; |
| 26 | if ($this->operation === 'edit') { |
| 27 | $form['#title'] = $this->t('Edit %label CRM contact detail type', ['%label' => $entity_type->label()]); |
| 28 | } |
| 29 | |
| 30 | $form['label'] = [ |
| 31 | '#title' => $this->t('Label'), |
| 32 | '#type' => 'textfield', |
| 33 | '#default_value' => $entity_type->label(), |
| 34 | '#description' => $this->t('The human-readable name of this CRM contact detail type.'), |
| 35 | '#required' => TRUE, |
| 36 | ]; |
| 37 | |
| 38 | $form['id'] = [ |
| 39 | '#type' => 'machine_name', |
| 40 | '#default_value' => $entity_type->id(), |
| 41 | '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, |
| 42 | '#machine_name' => [ |
| 43 | 'exists' => [ContactDetailType::class, 'load'], |
| 44 | 'source' => ['label'], |
| 45 | ], |
| 46 | '#description' => $this->t('A unique machine-readable name for this CRM contact detail type. It must only contain lowercase letters, numbers, and underscores.'), |
| 47 | ]; |
| 48 | |
| 49 | $form['description'] = [ |
| 50 | '#type' => 'textarea', |
| 51 | '#title' => $this->t('Description'), |
| 52 | '#description' => $this->t('A short description of the contact detail.'), |
| 53 | '#default_value' => $entity_type->get('description'), |
| 54 | ]; |
| 55 | |
| 56 | return $this->protectBundleIdElement($form); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * {@inheritdoc} |
| 61 | */ |
| 62 | protected function actions(array $form, FormStateInterface $form_state): array { |
| 63 | $actions = parent::actions($form, $form_state); |
| 64 | $actions['submit']['#value'] = $this->t('Save CRM contact detail type'); |
| 65 | $actions['delete']['#value'] = $this->t('Delete CRM contact detail type'); |
| 66 | return $actions; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritdoc} |
| 71 | */ |
| 72 | public function save(array $form, FormStateInterface $form_state): int { |
| 73 | $result = parent::save($form, $form_state); |
| 74 | |
| 75 | $message_args = ['%label' => $this->entity->label()]; |
| 76 | $this->messenger()->addStatus( |
| 77 | match($result) { |
| 78 | self::SAVED_NEW => $this->t('The CRM contact detail type %label has been added.', $message_args), |
| 79 | self::SAVED_UPDATED => $this->t('The CRM contact detail type %label has been updated.', $message_args), |
| 80 | } |
| 81 | ); |
| 82 | $form_state->setRedirectUrl($this->entity->toUrl('collection')); |
| 83 | |
| 84 | return $result; |
| 85 | } |
| 86 | |
| 87 | } |