Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactTypeForm
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 6
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
12
 actions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 save
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 getRelationshipTypeOptions
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Drupal\crm\Form;
4
5use Drupal\Core\Entity\BundleEntityFormBase;
6use Drupal\Core\Entity\EntityStorageInterface;
7use Drupal\Core\Entity\EntityTypeInterface;
8use Drupal\Core\Form\FormStateInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
11/**
12 * Form handler for contact type forms.
13 */
14class 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      $form['primary'] = [
81        '#type' => 'select',
82        '#title' => $this->t('Primary Relationship'),
83        '#options' => $this->getRelationshipTypeOptions(),
84        '#description' => $this->t('The primary relationship type for this contact type.'),
85        '#default_value' => $entity_type->get('primary'),
86      ];
87    }
88
89    return $this->protectBundleIdElement($form);
90  }
91
92  /**
93   * {@inheritdoc}
94   */
95  protected function actions(array $form, FormStateInterface $form_state) {
96    $actions = parent::actions($form, $form_state);
97    $actions['submit']['#value'] = $this->t('Save contact type');
98    $actions['delete']['#value'] = $this->t('Delete contact type');
99
100    return $actions;
101  }
102
103  /**
104   * {@inheritdoc}
105   */
106  public function save(array $form, FormStateInterface $form_state) {
107    /** @var \Drupal\crm\Entity\ContactType $entity_type */
108    $entity_type = $this->entity;
109
110    $entity_type->set('id', trim($entity_type->id()));
111    $entity_type->set('label', trim($entity_type->label()));
112    if ($this->operation == 'edit') {
113      $entity_type->set('primary', trim($entity_type->get('primary')));
114    }
115
116    $status = parent::save($form, $form_state);
117    $t_args = ['%name' => $entity_type->label()];
118    if ($status == SAVED_UPDATED) {
119      $message = $this->t('The contact type %name has been updated.', $t_args);
120      $this->messenger()->addStatus($message);
121    }
122    elseif ($status == SAVED_NEW) {
123      $message = $this->t('The contact type %name has been added.', $t_args);
124      $this->messenger()->addStatus($message);
125    }
126
127    $form_state->setRedirectUrl($entity_type->toUrl('collection'));
128
129    return $status;
130  }
131
132  /**
133   * Get the relationship type options.
134   *
135   * @return array
136   *   An array of relationship type options.
137   */
138  private function getRelationshipTypeOptions() {
139
140    $query = $this->storage->getQuery();
141    $group = $query->orConditionGroup()
142      ->condition('contact_type_a', $this->entity->id())
143      ->condition('contact_type_b', $this->entity->id());
144    $query->condition($group);
145    $relationship_type_ids = $query->accessCheck(FALSE)->execute();
146    $relationship_types = $this->storage->loadMultiple($relationship_type_ids);
147    $options = [];
148    foreach ($relationship_types as $relationship_type) {
149      $is_a = $relationship_type->get('contact_type_a') == $this->entity->id();
150      if ($is_a) {
151        $options[$relationship_type->id()] = $relationship_type->label();
152      }
153      else {
154        $options[$relationship_type->id()] = $relationship_type->get('label_b');
155      }
156
157    }
158    return $options;
159  }
160
161}