View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Core\RulesEventManager;
use Drupal\rules\Engine\ExpressionManagerInterface;
use Drupal\rules\Ui\RulesUiConfigHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ReactionRuleEditForm extends RulesComponentFormBase {
protected $eventManager;
protected $rulesUiHandler;
public function __construct(ExpressionManagerInterface $expression_manager, RulesEventManager $event_manager) {
parent::__construct($expression_manager);
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.rules_expression'), $container
->get('plugin.manager.rules_event'));
}
public function buildForm(array $form, FormStateInterface $form_state, RulesUiConfigHandler $rules_ui_handler = NULL) {
$this->rulesUiHandler = $rules_ui_handler;
return parent::buildForm($form, $form_state);
}
protected function prepareEntity() {
parent::prepareEntity();
$this->entity = $this->rulesUiHandler
->getConfig();
}
public function form(array $form, FormStateInterface $form_state) {
$form['events'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'edit-events',
],
],
];
$form['events']['table'] = [
'#theme' => 'table',
'#header' => [
$this
->t('Events'),
$this
->t('Operations'),
],
'#empty' => $this
->t('None'),
];
foreach ($this->entity
->getEventNames() as $key => $event_name) {
$event_definition = $this->eventManager
->getDefinition($event_name);
$form['events']['table']['#rows'][$key]['element'] = [
'data' => [
'#type' => 'item',
'#plain_text' => $event_definition['label'],
'#suffix' => '<div class="description">' . $this
->t('Machine name: @name', [
'@name' => $event_name,
]) . '</div>',
],
];
$form['events']['table']['#rows'][$key]['element']['colspan'] = 2;
}
$links['add-event'] = [];
$form['events']['table']['#footer'][] = [
[
'data' => [
'#prefix' => '<ul class="action-links">',
'local-action-links' => $links,
'#suffix' => '</ul>',
],
'colspan' => 2,
],
];
$form['#attached']['library'][] = 'rules/rules_ui.styles';
$form = $this->rulesUiHandler
->getForm()
->buildForm($form, $form_state);
return parent::form($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$this->rulesUiHandler
->getForm()
->validateForm($form, $form_state);
}
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;
}
public function save(array $form, FormStateInterface $form_state) {
$this->rulesUiHandler
->getForm()
->submitForm($form, $form_state);
$component = $this->rulesUiHandler
->getComponent();
$this->entity
->updateFromComponent($component);
parent::save($form, $form_state);
$this->rulesUiHandler
->clearTemporaryStorage();
$this
->messenger()
->addMessage($this
->t('Reaction rule %label has been updated.', [
'%label' => $this->entity
->label(),
]));
}
public function cancel(array $form, FormStateInterface $form_state) {
$this->rulesUiHandler
->clearTemporaryStorage();
$this
->messenger()
->addMessage($this
->t('Canceled.'));
$form_state
->setRedirect('entity.rules_reaction_rule.collection');
}
public function getTitle($rules_reaction_rule) {
return $this
->t('Edit reaction rule "@label"', [
'@label' => $rules_reaction_rule
->label(),
]);
}
}