View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Ui\RulesUiConfigHandler;
class RulesComponentEditForm extends RulesComponentFormBase {
protected $rulesUiHandler;
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['#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('Rule component %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_component.collection');
}
public function getTitle($rules_component) {
return $this
->t('Edit rules component "@label"', [
'@label' => $rules_component
->label(),
]);
}
}