Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DetailTypeForm
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 form
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
12
 save
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Drupal\crm\Form;
4
5use Drupal\Core\Entity\EntityForm;
6use Drupal\Core\Form\FormStateInterface;
7
8/**
9 * Detail Type form.
10 *
11 * @property \Drupal\crm\CrmDetailTypeInterface $entity
12 */
13class DetailTypeForm extends EntityForm {
14
15  /**
16   * {@inheritdoc}
17   */
18  public function form(array $form, FormStateInterface $form_state) {
19
20    $form = parent::form($form, $form_state);
21
22    $form['label'] = [
23      '#type' => 'textfield',
24      '#title' => $this->t('Label'),
25      '#maxlength' => 255,
26      '#default_value' => $this->entity->label(),
27      '#description' => $this->t('Label for the detail type.'),
28      '#required' => TRUE,
29    ];
30
31    $form['id'] = [
32      '#type' => 'machine_name',
33      '#default_value' => $this->entity->id(),
34      '#machine_name' => [
35        'exists' => '\Drupal\crm\Entity\DetailType::load',
36      ],
37      '#disabled' => !$this->entity->isNew(),
38    ];
39
40    $form['status'] = [
41      '#type' => 'checkbox',
42      '#title' => $this->t('Enabled'),
43      '#default_value' => $this->entity->status(),
44    ];
45
46    $form['description'] = [
47      '#type' => 'textarea',
48      '#title' => $this->t('Description'),
49      '#default_value' => $this->entity->get('description'),
50      '#description' => $this->t('Description of the detail type.'),
51    ];
52
53    $bundles = $this->entityTypeManager->getStorage('crm_contact_detail_type')->loadMultiple();
54
55    $form['bundles'] = [
56      '#type' => 'checkboxes',
57      '#title' => $this->t('Bundles'),
58      '#options' => array_map(function ($type) {
59        return $type->label();
60      }, $bundles),
61      '#default_value' => $this->entity->get('bundles') ?: [],
62      '#description' => $this->t('Select the bundles that this detail type applies to.'),
63      '#multiple' => TRUE,
64    ];
65
66    $form['negate'] = [
67      '#type' => 'checkbox',
68      '#title' => $this->t('Negate'),
69      '#default_value' => $this->entity->get('negate') ?: FALSE,
70      '#description' => $this->t('If checked, this detail type will be applied to all bundles except those selected.'),
71    ];
72
73    return $form;
74  }
75
76  /**
77   * {@inheritdoc}
78   */
79  public function save(array $form, FormStateInterface $form_state) {
80
81    $bundles = $form_state->getValue('bundles');
82    $this->entity->set('bundles', array_filter(array_values($bundles)));
83
84    $result = parent::save($form, $form_state);
85    $message_args = ['%label' => $this->entity->label()];
86    $message = $result == SAVED_NEW
87      ? $this->t('Created new detail type %label.', $message_args)
88      : $this->t('Updated detail type %label.', $message_args);
89    $this->messenger()->addStatus($message);
90    $form_state->setRedirectUrl($this->entity->toUrl('collection'));
91    return $result;
92  }
93
94}