Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
59 / 59 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
DetailTypeForm | |
100.00% |
59 / 59 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
form | |
100.00% |
49 / 49 |
|
100.00% |
1 / 1 |
4 | |||
save | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Form; |
4 | |
5 | use Drupal\Core\Entity\EntityForm; |
6 | use Drupal\Core\Form\FormStateInterface; |
7 | |
8 | /** |
9 | * Detail Type form. |
10 | * |
11 | * @property \Drupal\crm\CrmDetailTypeInterface $entity |
12 | */ |
13 | class DetailTypeForm extends EntityForm { |
14 | |
15 | const SAVED_NEW = 1; |
16 | const SAVED_UPDATED = 2; |
17 | |
18 | /** |
19 | * {@inheritdoc} |
20 | */ |
21 | public function form(array $form, FormStateInterface $form_state) { |
22 | |
23 | $form = parent::form($form, $form_state); |
24 | |
25 | $form['label'] = [ |
26 | '#type' => 'textfield', |
27 | '#title' => $this->t('Label'), |
28 | '#maxlength' => 255, |
29 | '#default_value' => $this->entity->label(), |
30 | '#description' => $this->t('Label for the detail type.'), |
31 | '#required' => TRUE, |
32 | ]; |
33 | |
34 | $form['id'] = [ |
35 | '#type' => 'machine_name', |
36 | '#default_value' => $this->entity->id(), |
37 | '#machine_name' => [ |
38 | 'exists' => '\Drupal\crm\Entity\DetailType::load', |
39 | ], |
40 | '#disabled' => !$this->entity->isNew(), |
41 | ]; |
42 | |
43 | $form['status'] = [ |
44 | '#type' => 'checkbox', |
45 | '#title' => $this->t('Enabled'), |
46 | '#default_value' => $this->entity->status(), |
47 | ]; |
48 | |
49 | $form['description'] = [ |
50 | '#type' => 'textarea', |
51 | '#title' => $this->t('Description'), |
52 | '#default_value' => $this->entity->get('description'), |
53 | '#description' => $this->t('Description of the detail type.'), |
54 | ]; |
55 | |
56 | $bundles = $this->entityTypeManager->getStorage('crm_contact_detail_type')->loadMultiple(); |
57 | $bundle_options = array_map(function ($type) { |
58 | return $type->label(); |
59 | }, $bundles); |
60 | |
61 | $form['bundles'] = [ |
62 | '#type' => 'checkboxes', |
63 | '#title' => $this->t('Bundles'), |
64 | '#options' => $bundle_options, |
65 | '#default_value' => $this->entity->get('bundles') ?: [], |
66 | '#description' => empty($bundle_options) |
67 | ? $this->t('No contact detail types are available. Create contact detail types first.') |
68 | : $this->t('Select the bundles that this detail type applies to.'), |
69 | '#multiple' => TRUE, |
70 | ]; |
71 | |
72 | $form['negate'] = [ |
73 | '#type' => 'checkbox', |
74 | '#title' => $this->t('Negate'), |
75 | '#default_value' => $this->entity->get('negate') ?: FALSE, |
76 | '#description' => $this->t('If checked, this detail type will be applied to all bundles except those selected.'), |
77 | ]; |
78 | |
79 | return $form; |
80 | } |
81 | |
82 | /** |
83 | * {@inheritdoc} |
84 | */ |
85 | public function save(array $form, FormStateInterface $form_state) { |
86 | |
87 | $bundles = $form_state->getValue('bundles'); |
88 | $this->entity->set('bundles', array_filter(array_values($bundles))); |
89 | |
90 | $result = parent::save($form, $form_state); |
91 | $message_args = ['%label' => $this->entity->label()]; |
92 | $message = $result == self::SAVED_NEW |
93 | ? $this->t('Created new detail type %label.', $message_args) |
94 | : $this->t('Updated detail type %label.', $message_args); |
95 | $this->messenger()->addStatus($message); |
96 | $form_state->setRedirectUrl($this->entity->toUrl('collection')); |
97 | return $result; |
98 | } |
99 | |
100 | } |