EditExpressionForm.php in Rules 8.3
File
src/Form/EditExpressionForm.php
View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Ui\RulesUiHandlerInterface;
use Drupal\rules\Engine\RulesComponent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class EditExpressionForm extends FormBase {
protected $component;
protected $rulesUiHandler;
protected $uuid;
protected function getEditedExpression(RulesComponent $component) {
$rule_expression = $component
->getExpression();
return $rule_expression
->getExpression($this->uuid);
}
public function buildForm(array $form, FormStateInterface $form_state, RulesUiHandlerInterface $rules_ui_handler = NULL, $uuid = NULL) {
$this->rulesUiHandler = $rules_ui_handler;
$this->component = is_object($form_state
->get('component')) ? $form_state
->get('component') : $this->rulesUiHandler
->getComponent();
$this->uuid = $form_state
->get('uuid') ?: $uuid;
$form_state
->set('rules_ui_handler', $this->rulesUiHandler);
$form_state
->set('component', $this->component);
$form_state
->set('uuid', $this->uuid);
$expression = $this
->getEditedExpression($this->component);
if (!$expression) {
throw new NotFoundHttpException();
}
$form_handler = $expression
->getFormHandler();
$form = $form_handler
->form($form, $form_state);
return $form;
}
public function getFormId() {
return 'rules_expression_edit';
}
protected function buildComponent(array $form, FormStateInterface $form_state) {
$component = clone $this->component;
$expression = $this
->getEditedExpression($component);
$form_handler = $expression
->getFormHandler();
$form_handler
->submitForm($form, $form_state);
return $component;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->rulesUiHandler = $form_state
->get('rules_ui_handler');
$this->component = is_object($form_state
->get('component')) ? $form_state
->get('component') : $this->rulesUiHandler
->getComponent();
$this->uuid = $form_state
->get('uuid');
$this->rulesUiHandler
->validateLock($form, $form_state);
$component = $this
->buildComponent($form, $form_state);
$violations = $component
->checkIntegrity();
foreach ($violations
->getFor($this->uuid) as $violation) {
$form_state
->setError($form, $violation
->getMessage());
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->component = $this
->buildComponent($form, $form_state);
$this->rulesUiHandler
->updateComponent($this->component);
$form_state
->setRedirectUrl($this->rulesUiHandler
->getBaseRouteUrl());
}
public function getTitle(RulesUiHandlerInterface $rules_ui_handler, $uuid) {
$this->uuid = $uuid;
$expression = $this
->getEditedExpression($rules_ui_handler
->getComponent());
return $this
->t('Edit @expression', [
'@expression' => $expression
->getLabel(),
]);
}
}