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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelationshipForm
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
42
 save
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Drupal\crm\Form;
4
5use Drupal\Component\Datetime\TimeInterface;
6use Drupal\Core\Datetime\DateFormatterInterface;
7use Drupal\Core\Entity\ContentEntityForm;
8use Drupal\Core\Entity\EntityRepositoryInterface;
9use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10use Drupal\Core\Form\FormStateInterface;
11use Drupal\Core\Session\AccountInterface;
12use Symfony\Component\DependencyInjection\ContainerInterface;
13
14/**
15 * Form controller for the crm relationship entity edit forms.
16 */
17class RelationshipForm extends ContentEntityForm {
18
19  /**
20   * The date formatter service.
21   *
22   * @var \Drupal\Core\Datetime\DateFormatterInterface
23   */
24  protected $dateFormatter;
25
26  /**
27   * The current user.
28   *
29   * @var \Drupal\Core\Session\AccountInterface
30   */
31  protected $currentUser;
32
33  /**
34   * Constructs a RelationshipForm object.
35   *
36   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
37   *   The entity repository.
38   * @param \Drupal\Component\Datetime\TimeInterface $time
39   *   The time service.
40   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
41   *   The date formatter service.
42   * @param \Drupal\Core\Session\AccountInterface $current_user
43   *   The current user.
44   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface|null $entity_type_bundle_info
45   *   The entity type bundle service.
46   */
47  public function __construct(
48    EntityRepositoryInterface $entity_repository,
49    TimeInterface $time,
50    DateFormatterInterface $date_formatter,
51    AccountInterface $current_user,
52    ?EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL,
53  ) {
54
55    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
56    $this->dateFormatter = $date_formatter;
57    $this->currentUser = $current_user;
58  }
59
60  /**
61   * {@inheritdoc}
62   */
63  final public static function create(ContainerInterface $container) {
64    return new self(
65      $container->get('entity.repository'),
66      $container->get('datetime.time'),
67      $container->get('date.formatter'),
68      $container->get('current_user'),
69      $container->get('entity_type.bundle.info'),
70    );
71  }
72
73  /**
74   * {@inheritdoc}
75   */
76  public function form(array $form, FormStateInterface $form_state) {
77    $form = parent::form($form, $form_state);
78    $relationship = $this->getEntity();
79    $bundle = $relationship->bundle->entity;
80
81    $form['contact_a']['widget'][0]['target_id']['#selection_settings']['target_bundles'] = [
82      $bundle->get('contact_type_a') => $bundle->get('contact_type_a'),
83    ];
84    $form['contact_a']['widget'][0]['target_id']['#title'] = $bundle->get('label_a');
85
86    $form['contact_b']['widget'][0]['target_id']['#selection_settings']['target_bundles'] = [
87      $bundle->get('contact_type_b') => $bundle->get('contact_type_b'),
88    ];
89    $form['contact_b']['widget'][0]['target_id']['#title'] = $bundle->get('label_b');
90
91    if ($this->getOperation() == 'edit') {
92      $form['contact_a']['widget']['#disabled'] = TRUE;
93      $form['contact_b']['widget']['#disabled'] = TRUE;
94    }
95
96    $form['advanced']['#attributes']['class'][] = 'entity-meta';
97
98    $form['meta'] = [
99      '#type' => 'details',
100      '#group' => 'advanced',
101      '#weight' => -10,
102      '#title' => $this->t('Status'),
103      '#attributes' => ['class' => ['entity-meta__header']],
104      '#tree' => TRUE,
105      '#access' => $this->currentUser->hasPermission('administer crm'),
106    ];
107    $form['meta']['published'] = [
108      '#type' => 'item',
109      '#markup' => $relationship->isPublished() ? $this->t('Active') : $this->t('Inactive'),
110      '#access' => !$relationship->isNew(),
111      '#wrapper_attributes' => ['class' => ['entity-meta__title']],
112    ];
113    $form['meta']['changed'] = [
114      '#type' => 'item',
115      '#title' => $this->t('Last saved'),
116      '#markup' => !$relationship->isNew() ? $this->dateFormatter->format($relationship->getChangedTime(), 'short') : $this->t('Not saved yet'),
117      '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
118    ];
119
120    $form['meta']['status'] = &$form['status'];
121    $form['meta']['status']['#weight'] = 100;
122    unset($form['status']);
123
124    if (isset($form['uid'])) {
125      unset($form['uid']);
126    }
127
128    if (isset($form['created'])) {
129      $form['created']['#weight'] = 200;
130      $form['meta']['created'] = &$form['created'];
131      unset($form['created']);
132    }
133
134    return $form;
135  }
136
137  /**
138   * {@inheritdoc}
139   */
140  public function save(array $form, FormStateInterface $form_state) {
141    $result = parent::save($form, $form_state);
142
143    $relationship = $this->getEntity();
144
145    $label = $relationship->label() ?? 'No label';
146    $message_arguments = ['%label' => $label];
147    $logger_arguments = [
148      '%label' => $label,
149      'link' => $relationship->toLink($this->t('View'))->toString(),
150    ];
151
152    switch ($result) {
153      case SAVED_NEW:
154        $this->messenger()->addStatus($this->t('New crm relationship %label has been created.', $message_arguments));
155        $this->logger('crm')->notice('Created new crm relationship %label', $logger_arguments);
156        break;
157
158      case SAVED_UPDATED:
159        $this->messenger()->addStatus($this->t('The crm relationship %label has been updated.', $message_arguments));
160        $this->logger('crm')->notice('Updated crm relationship %label.', $logger_arguments);
161        break;
162    }
163
164    $form_state->setRedirect('entity.crm_relationship.canonical', ['crm_relationship' => $relationship->id()]);
165
166    return $result;
167  }
168
169}