-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsvplist.module
56 lines (51 loc) · 1.88 KB
/
rsvplist.module
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
<?php
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_alter()
* Alter the form Add/Edit form to include admin setting for displaying
* RSVPBlock with content
*/
function rsvplist_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $formState, $form_id) {
$node = $formState->getFormObject()->getEntity();
$current_node_type = $node->getType();
$config = \Drupal::config('rsvplist.settings');
$types = $config->get('allowed_types', []);
// RSVP options for administrators
if (in_array($current_node_type, $types)) {
$form['rsvplist'] = [
'#type' => 'details',
'#title' => t('RSVP Collections'),
'#access' => \Drupal::currentUser()->hasPermission('administer rsvplist'),
'#group' => 'advanced',
'#weight' => 100,
];
/** @var \Drupal\rsvplist\EnablerService $enabler */
$enabler = Drupal::service('rsvplist.enabler');
$form['rsvplist']['rsvplist_enabled'] = [
'#type' => 'checkbox',
'#title' => t('Collect RSVP e-mail addresses for this node.'),
'#default_value' => $enabler->isEnabled($node),
];
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'rsvplist_form_node_form_submit';
}
}
}
}
/**
* Form submission handler for RSVP item field on the node form.
*
* @see \rsvplist_form_node_form_alter()
*/
function rsvplist_form_node_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $formState) {
/** @var \Drupal\rsvplist\EnablerService $enabler */
$enabler = Drupal::service('rsvplist.enabler');
$node = $formState->getFormObject()->getEntity();
if ($enabled = $formState->getValue('rsvplist_enabled')) {
$enabler->setEnabled($node);
}
else {
$enabler->delEnabled($node);
}
}