Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
114 / 114 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ContactTypeForm | |
100.00% |
114 / 114 |
|
100.00% |
6 / 6 |
16 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
form | |
100.00% |
67 / 67 |
|
100.00% |
1 / 1 |
7 | |||
actions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
save | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
3 | |||
getRelationshipTypeOptions | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Form; |
4 | |
5 | use Drupal\Core\Entity\BundleEntityFormBase; |
6 | use Drupal\Core\Entity\EntityStorageInterface; |
7 | use Drupal\Core\Entity\EntityTypeInterface; |
8 | use Drupal\Core\Form\FormStateInterface; |
9 | use Symfony\Component\DependencyInjection\ContainerInterface; |
10 | |
11 | /** |
12 | * Form handler for contact type forms. |
13 | */ |
14 | class ContactTypeForm extends BundleEntityFormBase { |
15 | |
16 | const SAVED_NEW = 1; |
17 | const SAVED_UPDATED = 2; |
18 | |
19 | /** |
20 | * The entity storage. |
21 | * |
22 | * @var \Drupal\Core\Entity\EntityStorageInterface |
23 | */ |
24 | protected $storage; |
25 | |
26 | /** |
27 | * Constructs a ContactTypeForm object. |
28 | * |
29 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
30 | * The entity storage. |
31 | */ |
32 | public function __construct(EntityStorageInterface $storage) { |
33 | $this->storage = $storage; |
34 | } |
35 | |
36 | /** |
37 | * {@inheritdoc} |
38 | */ |
39 | final public static function create(ContainerInterface $container) { |
40 | return new static( |
41 | $container->get('entity_type.manager')->getStorage('crm_relationship_type'), |
42 | ); |
43 | } |
44 | |
45 | /** |
46 | * {@inheritdoc} |
47 | */ |
48 | public function form(array $form, FormStateInterface $form_state) { |
49 | $form = parent::form($form, $form_state); |
50 | |
51 | $entity_type = $this->entity; |
52 | |
53 | $form['label'] = [ |
54 | '#title' => $this->t('Label'), |
55 | '#type' => 'textfield', |
56 | '#default_value' => $entity_type->label(), |
57 | '#description' => $this->t('The human-readable name of this contact type.'), |
58 | '#required' => TRUE, |
59 | '#size' => 30, |
60 | ]; |
61 | |
62 | $form['id'] = [ |
63 | '#type' => 'machine_name', |
64 | '#default_value' => $entity_type->id(), |
65 | '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, |
66 | '#machine_name' => [ |
67 | 'exists' => ['Drupal\crm\Entity\ContactType', 'load'], |
68 | 'source' => ['label'], |
69 | ], |
70 | '#description' => $this->t('A unique machine-readable name for this contact type. It must only contain lowercase letters, numbers, and underscores.'), |
71 | ]; |
72 | |
73 | $form['description'] = [ |
74 | '#type' => 'textarea', |
75 | '#title' => $this->t('Description'), |
76 | '#description' => $this->t('A short description of the contact.'), |
77 | '#default_value' => $entity_type->get('description') ?: '', |
78 | ]; |
79 | $form['additional_settings'] = [ |
80 | '#type' => 'vertical_tabs', |
81 | ]; |
82 | |
83 | $date = $entity_type->get('date'); |
84 | $form['start_date'] = [ |
85 | '#type' => 'details', |
86 | '#title' => $this->t('Start Date'), |
87 | '#group' => 'additional_settings', |
88 | ]; |
89 | |
90 | $form['start_date']['start_date_label'] = [ |
91 | '#type' => 'textfield', |
92 | '#title' => $this->t('Start Date Label'), |
93 | '#description' => $this->t('The label for the start date field.'), |
94 | '#default_value' => isset($date['start_date']) ? $date['start_date']['label'] : '', |
95 | ]; |
96 | |
97 | $form['start_date']['start_date_description'] = [ |
98 | '#type' => 'textarea', |
99 | '#title' => $this->t('Start Date Description'), |
100 | '#description' => $this->t('The description for the start date field.'), |
101 | '#default_value' => isset($date['start_date']) ? $date['start_date']['description'] : '', |
102 | ]; |
103 | |
104 | $form['end_date'] = [ |
105 | '#type' => 'details', |
106 | '#title' => $this->t('End Date'), |
107 | '#group' => 'additional_settings', |
108 | ]; |
109 | |
110 | $form['end_date']['end_date_label'] = [ |
111 | '#type' => 'textfield', |
112 | '#title' => $this->t('End Date Label'), |
113 | '#description' => $this->t('The label for the end date field.'), |
114 | '#default_value' => isset($date['end_date']) ? $date['end_date']['label'] : '', |
115 | ]; |
116 | |
117 | $form['end_date']['end_date_description'] = [ |
118 | '#type' => 'textarea', |
119 | '#title' => $this->t('End Date Description'), |
120 | '#description' => $this->t('The description for the end date field.'), |
121 | '#default_value' => isset($date['end_date']) ? $date['end_date']['description'] : '', |
122 | ]; |
123 | |
124 | if ($this->operation == 'edit') { |
125 | $form['#title'] = $this->t('Edit %label contact type', ['%label' => $entity_type->label()]); |
126 | } |
127 | |
128 | return $this->protectBundleIdElement($form); |
129 | } |
130 | |
131 | /** |
132 | * {@inheritdoc} |
133 | */ |
134 | protected function actions(array $form, FormStateInterface $form_state) { |
135 | $actions = parent::actions($form, $form_state); |
136 | $actions['submit']['#value'] = $this->t('Save contact type'); |
137 | $actions['delete']['#value'] = $this->t('Delete contact type'); |
138 | |
139 | return $actions; |
140 | } |
141 | |
142 | /** |
143 | * {@inheritdoc} |
144 | */ |
145 | public function save(array $form, FormStateInterface $form_state) { |
146 | /** @var \Drupal\crm\Entity\ContactType $entity_type */ |
147 | $entity_type = $this->entity; |
148 | |
149 | $entity_type->set('id', trim($entity_type->id())); |
150 | $entity_type->set('label', trim($form_state->getValue('label'))); |
151 | $entity_type->set('description', trim($form_state->getValue('description'))); |
152 | $date = [ |
153 | 'start_date' => [ |
154 | 'label' => $form_state->getValue('start_date_label'), |
155 | 'description' => $form_state->getValue('start_date_description'), |
156 | ], |
157 | 'end_date' => [ |
158 | 'label' => $form_state->getValue('end_date_label'), |
159 | 'description' => $form_state->getValue('end_date_description'), |
160 | ], |
161 | ]; |
162 | $entity_type->set('date', $date); |
163 | |
164 | $status = parent::save($form, $form_state); |
165 | $t_args = ['%name' => $entity_type->label()]; |
166 | if ($status == self::SAVED_UPDATED) { |
167 | $message = $this->t('The contact type %name has been updated.', $t_args); |
168 | $this->messenger()->addStatus($message); |
169 | } |
170 | elseif ($status == self::SAVED_NEW) { |
171 | $message = $this->t('The contact type %name has been added.', $t_args); |
172 | $this->messenger()->addStatus($message); |
173 | } |
174 | |
175 | $form_state->setRedirectUrl($entity_type->toUrl('collection')); |
176 | |
177 | return $status; |
178 | } |
179 | |
180 | /** |
181 | * Get the relationship type options. |
182 | * |
183 | * @return array |
184 | * An array of relationship type options. |
185 | */ |
186 | private function getRelationshipTypeOptions() { |
187 | |
188 | $query = $this->storage->getQuery(); |
189 | $group = $query->orConditionGroup() |
190 | ->condition('contact_type_a', $this->entity->id()) |
191 | ->condition('contact_type_b', $this->entity->id()); |
192 | $query->condition($group); |
193 | $relationship_type_ids = $query->accessCheck(FALSE)->execute(); |
194 | $relationship_types = $this->storage->loadMultiple($relationship_type_ids); |
195 | $options = []; |
196 | foreach ($relationship_types as $relationship_type) { |
197 | $is_a = $relationship_type->get('contact_type_a') == $this->entity->id(); |
198 | if ($is_a) { |
199 | $options[$relationship_type->id()] = $relationship_type->label(); |
200 | } |
201 | else { |
202 | $options[$relationship_type->id()] = $relationship_type->get('label_b'); |
203 | } |
204 | |
205 | } |
206 | return $options; |
207 | } |
208 | |
209 | } |