-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathReactionRuleEditForm.php
More file actions
143 lines (123 loc) · 3.95 KB
/
ReactionRuleEditForm.php
File metadata and controls
143 lines (123 loc) · 3.95 KB
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
<?php
/**
* @file
* Contains \Drupal\rules\Form\ReactionRuleEditForm.
*/
namespace Drupal\rules\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Core\RulesEventManager;
use Drupal\rules\Core\RulesUiConfigHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form to edit a reaction rule.
*/
class ReactionRuleEditForm extends RulesComponentFormBase {
/**
* The event plugin manager.
*
* @var \Drupal\rules\Core\RulesEventManager
*/
protected $eventManager;
/**
* The RulesUI handler of the currently active UI.
*
* @var \Drupal\rules\Core\RulesUiConfigHandler
*/
protected $rulesUiHandler;
/**
* Constructs a new object of this class.
*
* @param \Drupal\rules\Core\RulesEventManager $event_manager
* The event plugin manager.
*/
public function __construct(RulesEventManager $event_manager) {
$this->eventManager = $event_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('plugin.manager.rules_event'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, RulesUiConfigHandler $rules_ui_handler = NULL) {
// Overridden such we can receive further route parameters.
$this->rulesUiHandler = $rules_ui_handler;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function prepareEntity() {
parent::prepareEntity();
// Replace the config entity with the latest entity from temp store, so any
// interim changes are picked up.
$this->entity = $this->rulesUiHandler->getConfig();
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
foreach ($this->entity->getEventNames() as $key => $event_name) {
$event_definition = $this->eventManager->getDefinition($event_name);
$form['event'][$key] = [
'#type' => 'item',
'#title' => $this->t('Events:'),
'#markup' => $this->t('@label (@name)', [
'@label' => $event_definition['label'],
'@name' => $event_name,
]),
];
}
$form = $this->rulesUiHandler->getForm()->buildForm($form, $form_state);
return parent::form($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this->rulesUiHandler->getForm()->validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save');
$actions['cancel'] = [
'#type' => 'submit',
'#limit_validation_errors' => [['locked']],
'#value' => $this->t('Cancel'),
'#submit' => ['::cancel'],
];
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->rulesUiHandler->getForm()->submitForm($form, $form_state);
// Persist changes by saving the entity.
parent::save($form, $form_state);
// Also remove the temporarily stored component, it has been persisted now.
$this->rulesUiHandler->clearTemporaryStorage();
drupal_set_message($this->t('Reaction rule %label has been updated.', ['%label' => $this->entity->label()]));
}
/**
* Form submission handler for the 'cancel' action.
*/
public function cancel(array $form, FormStateInterface $form_state) {
$this->rulesUiHandler->clearTemporaryStorage();
drupal_set_message($this->t('Canceled.'));
$form_state->setRedirect('entity.rules_reaction_rule.collection');
}
/**
* Title callback: also display the rule label.
*/
public function getTitle($rules_reaction_rule) {
return $this->t('Edit reaction rule "@label"', ['@label' => $rules_reaction_rule->label()]);
}
}