Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.19% |
80 / 105 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
RelationshipTypeForm | |
76.19% |
80 / 105 |
|
33.33% |
2 / 6 |
13.94 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
form | |
98.70% |
76 / 77 |
|
0.00% |
0 / 1 |
3 | |||
getContactTypeOptions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
actions | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
save | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace Drupal\crm\Form; |
4 | |
5 | use Drupal\Core\Entity\BundleEntityFormBase; |
6 | use Drupal\Core\Entity\EntityTypeBundleInfoInterface; |
7 | use Drupal\Core\Entity\EntityTypeInterface; |
8 | use Drupal\Core\Form\FormStateInterface; |
9 | use Symfony\Component\DependencyInjection\ContainerInterface; |
10 | |
11 | /** |
12 | * Form handler for crm relationship type forms. |
13 | */ |
14 | class RelationshipTypeForm extends BundleEntityFormBase { |
15 | |
16 | /** |
17 | * The entity type bundle info service. |
18 | * |
19 | * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface |
20 | */ |
21 | protected $entityTypeBundleInfo; |
22 | |
23 | /** |
24 | * Constructs a RelationshipTypeForm object. |
25 | * |
26 | * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info |
27 | * The entity type bundle info service. |
28 | */ |
29 | public function __construct(EntityTypeBundleInfoInterface $entity_type_bundle_info) { |
30 | $this->entityTypeBundleInfo = $entity_type_bundle_info; |
31 | } |
32 | |
33 | /** |
34 | * {@inheritdoc} |
35 | */ |
36 | final public static function create(ContainerInterface $container) { |
37 | return new self( |
38 | $container->get('entity_type.bundle.info') |
39 | ); |
40 | } |
41 | |
42 | /** |
43 | * {@inheritdoc} |
44 | */ |
45 | public function form(array $form, FormStateInterface $form_state) { |
46 | $form = parent::form($form, $form_state); |
47 | |
48 | $entity_type = $this->entity; |
49 | if ($this->operation == 'edit') { |
50 | $form['#title'] = $this->t('Edit %label crm relationship type', ['%label' => $entity_type->label()]); |
51 | } |
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 crm relationship 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\RelationshipType', 'load'], |
68 | 'source' => ['label'], |
69 | ], |
70 | '#description' => $this->t('A unique machine-readable name for this crm relationship type. It must only contain lowercase letters, numbers, and underscores.'), |
71 | ]; |
72 | |
73 | $form['description'] = [ |
74 | '#title' => $this->t('Description'), |
75 | '#type' => 'textarea', |
76 | '#default_value' => $entity_type->get('description'), |
77 | '#description' => $this->t('A description of this crm relationship type.'), |
78 | ]; |
79 | |
80 | $form['asymmetric'] = [ |
81 | '#title' => $this->t('Asymmetric'), |
82 | '#type' => 'checkbox', |
83 | '#default_value' => $entity_type->isNew() ? 1 : $entity_type->get('asymmetric'), |
84 | '#description' => $this->t('Check this box if the relationship is asymmetric.'), |
85 | ]; |
86 | |
87 | $form['label_a'] = [ |
88 | '#title' => $this->t('Contact A label'), |
89 | '#type' => 'textfield', |
90 | '#default_value' => $entity_type->get('label_a'), |
91 | '#description' => $this->t('The human-readable name of this crm relationship type from contact A to contact B.'), |
92 | '#required' => TRUE, |
93 | '#size' => 30, |
94 | ]; |
95 | |
96 | $form['contact_type_a'] = [ |
97 | '#title' => $this->t('Contact A type'), |
98 | '#type' => 'select', |
99 | '#options' => $this->getContactTypeOptions(), |
100 | '#default_value' => $entity_type->get('contact_type_a'), |
101 | '#description' => $this->t('The contact type for the first contact in the relationship.'), |
102 | '#required' => TRUE, |
103 | ]; |
104 | |
105 | $form['label_b'] = [ |
106 | '#title' => $this->t('Contact B label'), |
107 | '#type' => 'textfield', |
108 | '#default_value' => $entity_type->get('label_b'), |
109 | '#description' => $this->t('The human-readable name of this crm relationship type from contact A to contact B.'), |
110 | '#required' => TRUE, |
111 | '#size' => 30, |
112 | '#states' => [ |
113 | 'visible' => [ |
114 | ':input[name="asymmetric"]' => ['checked' => TRUE], |
115 | ], |
116 | ], |
117 | ]; |
118 | |
119 | $form['contact_type_b'] = [ |
120 | '#title' => $this->t('Contact B type'), |
121 | '#type' => 'select', |
122 | '#options' => $this->getContactTypeOptions(), |
123 | '#default_value' => $entity_type->get('contact_type_b'), |
124 | '#description' => $this->t('The contact type for the second contact in the relationship.'), |
125 | '#required' => TRUE, |
126 | '#states' => [ |
127 | 'visible' => [ |
128 | ':input[name="asymmetric"]' => ['checked' => TRUE], |
129 | ], |
130 | ], |
131 | ]; |
132 | |
133 | return $this->protectBundleIdElement($form); |
134 | } |
135 | |
136 | /** |
137 | * Returns a list of contact types. |
138 | */ |
139 | protected function getContactTypeOptions() { |
140 | $crm_contact_type = $this->entityTypeBundleInfo->getBundleInfo('crm_contact'); |
141 | $options = []; |
142 | foreach ($crm_contact_type as $type => $contact) { |
143 | $options[$type] = $contact['label']; |
144 | } |
145 | |
146 | return $options; |
147 | } |
148 | |
149 | /** |
150 | * {@inheritdoc} |
151 | */ |
152 | protected function actions(array $form, FormStateInterface $form_state) { |
153 | $actions = parent::actions($form, $form_state); |
154 | $actions['submit']['#value'] = $this->t('Save relationship type'); |
155 | |
156 | return $actions; |
157 | } |
158 | |
159 | /** |
160 | * {@inheritdoc} |
161 | */ |
162 | public function save(array $form, FormStateInterface $form_state) { |
163 | $entity_type = $this->entity; |
164 | $entity_type |
165 | ->set('id', trim($entity_type->id())) |
166 | ->set('label', trim($entity_type->label())); |
167 | |
168 | if (!$entity_type->get('asymmetric')) { |
169 | $entity_type->set('label_b', $entity_type->get('label_a')); |
170 | $entity_type->set('contact_type_b', $entity_type->get('contact_type_a')); |
171 | } |
172 | |
173 | $status = $entity_type->save(); |
174 | |
175 | $t_args = ['%name' => $entity_type->label()]; |
176 | if ($status == SAVED_UPDATED) { |
177 | $message = $this->t('The crm relationship type %name has been updated.', $t_args); |
178 | } |
179 | elseif ($status == SAVED_NEW) { |
180 | $message = $this->t('The crm relationship type %name has been added.', $t_args); |
181 | } |
182 | $this->messenger()->addStatus($message); |
183 | |
184 | $form_state->setRedirectUrl($entity_type->toUrl('collection')); |
185 | |
186 | return $status; |
187 | } |
188 | |
189 | } |