rng-3.x-dev/src/Form/RegistrationForm.php

src/Form/RegistrationForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
 
namespace Drupal\rng\Form;
 
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\rng\EventManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Form controller for registrations.
 */
class RegistrationForm extends ContentEntityForm {
 
  /**
   * The registration entity.
   *
   * @var \Drupal\rng\Entity\RegistrationInterface
   */
  protected $entity;
 
  /**
   * The RNG event manager.
   *
   * @var \Drupal\rng\EventManagerInterface
   */
  protected $eventManager;
 
  /**
   * Injects extra services used by this form.
   *
   * This is done this way to avoid overriding the parent constructor.
   *
   * @param \Drupal\rng\EventManagerInterface $event_manager
   *   The RNG event manager.
   */
  public function setUp(EventManagerInterface $event_manager) {
    $this->eventManager = $event_manager;
  }
 
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $self = parent::create($container);
    $self->setup($container->get('rng.event_manager'));
    return $self;
  }
 
  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    /** @var \Drupal\rng\Entity\RegistrationInterface $registration */
    $registration = $this->getEntity();
    $current_user = $this->currentUser();
 
    $event = $registration->getEvent();
    $event_meta = $this->eventManager->getMeta($event);
 
    $form = parent::form($form, $form_state);
 
    if (!$registration->isNew()) {
      $form['#title'] = $this->t('Edit Registration', [
        '%event_label' => $event->label(),
        '%event_id' => $event->id(),
        '%registration_id' => $registration->id(),
      ]);
    }
 
    // Registrants are initially either a collection of stub registrant
    // entities, or a single blank one. After any data changes, registrants
    // include all associated with the registration.
    $registrants = $registration->getRegistrants();
 
    $form['registrants_before'] = [
      '#type' => 'value',
      '#value' => $registrants,
    ];
 
    $form['people'] = [
      '#type' => 'details',
      '#title' => $this->t('People'),
      '#description' => $this->t('Select people to associate with this registration.'),
      '#open' => TRUE,
      '#tree' => TRUE,
    ];
 
    $event_type = $event_meta->getEventType();
    $form['people']['registrants'] = [
      '#type' => 'registrants',
      '#event' => $event,
      '#default_value' => $registrants,
      '#allow_creation' => $event_meta->getCreatableIdentityTypes(),
      '#allow_reference' => $event_meta->getIdentityTypes(),
      '#registration' => $registration,
      '#form_modes' => $event_type->getIdentityTypeEntityFormModes(),
      '#tree' => TRUE,
    ];
 
    if (!$registration->isNew()) {
      $form['revision_information'] = [
        '#type' => 'details',
        '#title' => $this->t('Revisions'),
        '#optional' => TRUE,
        '#open' => $current_user->hasPermission('administer rng'),
        '#weight' => 20,
      ];
      $form['revision'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Create new revision'),
        '#description' => $this->t('Revisions record changes between saves.'),
        '#default_value' => FALSE,
        '#access' => $current_user->hasPermission('administer rng'),
        '#group' => 'revision_information',
      ];
    }
 
    return $form;
  }
 
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\rng\Entity\RegistrationInterface $registration */
    $registration = parent::validateForm($form, $form_state);
 
    /** @var \Drupal\rng\Entity\RegistrantInterface[] $registrants_before */
    $registrants_before = $form_state->getValue('registrants_before');
    /** @var \Drupal\rng\Entity\RegistrantInterface[] $registrants_after */
    $registrants_after = $form_state->getValue(['people', 'registrants']);
 
    // Registrants.
    $registrants_after_ids = [];
    foreach ($registrants_after as $registrant) {
      if (!$registrant->isNew()) {
        $registrants_after_ids[] = $registrant->id();
      }
    }
 
    // Delete old registrants if they are not needed.
    $registrants_delete = [];
    foreach ($registrants_before as $i => $registrant) {
      if (!$registrant->isNew()) {
        if (!in_array($registrant->id(), $registrants_after_ids)) {
          $registrants_delete[] = $registrant;
        }
      }
    }
 
    $form_state->set('registrants_after', $registrants_after);
    $form_state->set('registrants_delete', $registrants_delete);
 
    return $registration;
  }
 
  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $registration = $this->entity;
 
    $t_args = [
      '@type' => $registration->bundle(),
      '%label' => $registration->label(),
      '%id' => $registration->id(),
    ];
 
    if (!$registration->isNew()) {
      $registration->setNewRevision(!$form_state->isValueEmpty('revision'));
    }
 
    if ($registration->save() == SAVED_NEW) {
      $this->messenger()->addMessage($this->t('Registration has been created.', $t_args));
    }
    else {
      $this->messenger()->addMessage($this->t('Registration was updated.', $t_args));
    }
 
    /** @var \Drupal\rng\Entity\RegistrantInterface[] $registrants */
    $registrants = $form_state->get('registrants_after');
    foreach ($registrants as $registrant) {
      $registrant->setRegistration($registration);
      $registrant->save();
    }
 
    /** @var \Drupal\rng\Entity\RegistrantInterface[] $registrants_delete */
    $registrants_delete = $form_state->get('registrants_delete');
    foreach ($registrants_delete as $registrant) {
      $registrant->delete();
    }
 
    $event = $registration->getEvent();
    if ($registration->access('view')) {
      $form_state->setRedirectUrl($registration->toUrl());
    }
    else {
      $form_state->setRedirectUrl($event->toUrl());
    }
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc