Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ContactTypeForm | |
0.00% |
0 / 64 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
form | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
12 | |||
actions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
save | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getRelationshipTypeOptions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 |
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 | /** |
17 | * The entity storage. |
18 | * |
19 | * @var \Drupal\Core\Entity\EntityStorageInterface |
20 | */ |
21 | protected $storage; |
22 | |
23 | /** |
24 | * Constructs a ContactTypeForm object. |
25 | * |
26 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
27 | * The entity storage. |
28 | */ |
29 | public function __construct(EntityStorageInterface $storage) { |
30 | $this->storage = $storage; |
31 | } |
32 | |
33 | /** |
34 | * {@inheritdoc} |
35 | */ |
36 | final public static function create(ContainerInterface $container) { |
37 | return new static( |
38 | $container->get('entity_type.manager')->getStorage('crm_relationship_type'), |
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 | |
50 | $form['label'] = [ |
51 | '#title' => $this->t('Label'), |
52 | '#type' => 'textfield', |
53 | '#default_value' => $entity_type->label(), |
54 | '#description' => $this->t('The human-readable name of this contact type.'), |
55 | '#required' => TRUE, |
56 | '#size' => 30, |
57 | ]; |
58 | |
59 | $form['id'] = [ |
60 | '#type' => 'machine_name', |
61 | '#default_value' => $entity_type->id(), |
62 | '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, |
63 | '#machine_name' => [ |
64 | 'exists' => ['Drupal\crm\Entity\ContactType', 'load'], |
65 | 'source' => ['label'], |
66 | ], |
67 | '#description' => $this->t('A unique machine-readable name for this contact type. It must only contain lowercase letters, numbers, and underscores.'), |
68 | ]; |
69 | |
70 | $form['description'] = [ |
71 | '#type' => 'textarea', |
72 | '#title' => $this->t('Description'), |
73 | '#description' => $this->t('A short description of the contact.'), |
74 | '#default_value' => $entity_type->get('description') ?: '', |
75 | ]; |
76 | |
77 | if ($this->operation == 'edit') { |
78 | $form['#title'] = $this->t('Edit %label contact type', ['%label' => $entity_type->label()]); |
79 | } |
80 | |
81 | return $this->protectBundleIdElement($form); |
82 | } |
83 | |
84 | /** |
85 | * {@inheritdoc} |
86 | */ |
87 | protected function actions(array $form, FormStateInterface $form_state) { |
88 | $actions = parent::actions($form, $form_state); |
89 | $actions['submit']['#value'] = $this->t('Save contact type'); |
90 | $actions['delete']['#value'] = $this->t('Delete contact type'); |
91 | |
92 | return $actions; |
93 | } |
94 | |
95 | /** |
96 | * {@inheritdoc} |
97 | */ |
98 | public function save(array $form, FormStateInterface $form_state) { |
99 | /** @var \Drupal\crm\Entity\ContactType $entity_type */ |
100 | $entity_type = $this->entity; |
101 | |
102 | $entity_type->set('id', trim($entity_type->id())); |
103 | $entity_type->set('label', trim($entity_type->label())); |
104 | |
105 | $status = parent::save($form, $form_state); |
106 | $t_args = ['%name' => $entity_type->label()]; |
107 | if ($status == SAVED_UPDATED) { |
108 | $message = $this->t('The contact type %name has been updated.', $t_args); |
109 | $this->messenger()->addStatus($message); |
110 | } |
111 | elseif ($status == SAVED_NEW) { |
112 | $message = $this->t('The contact type %name has been added.', $t_args); |
113 | $this->messenger()->addStatus($message); |
114 | } |
115 | |
116 | $form_state->setRedirectUrl($entity_type->toUrl('collection')); |
117 | |
118 | return $status; |
119 | } |
120 | |
121 | /** |
122 | * Get the relationship type options. |
123 | * |
124 | * @return array |
125 | * An array of relationship type options. |
126 | */ |
127 | private function getRelationshipTypeOptions() { |
128 | |
129 | $query = $this->storage->getQuery(); |
130 | $group = $query->orConditionGroup() |
131 | ->condition('contact_type_a', $this->entity->id()) |
132 | ->condition('contact_type_b', $this->entity->id()); |
133 | $query->condition($group); |
134 | $relationship_type_ids = $query->accessCheck(FALSE)->execute(); |
135 | $relationship_types = $this->storage->loadMultiple($relationship_type_ids); |
136 | $options = []; |
137 | foreach ($relationship_types as $relationship_type) { |
138 | $is_a = $relationship_type->get('contact_type_a') == $this->entity->id(); |
139 | if ($is_a) { |
140 | $options[$relationship_type->id()] = $relationship_type->label(); |
141 | } |
142 | else { |
143 | $options[$relationship_type->id()] = $relationship_type->get('label_b'); |
144 | } |
145 | |
146 | } |
147 | return $options; |
148 | } |
149 | |
150 | } |