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