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