class EditExpressionForm in Rules 8.3
UI form to edit an expression like a condition or action in a rule.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\rules\Form\EditExpressionForm
Expanded class hierarchy of EditExpressionForm
File
- src/
Form/ EditExpressionForm.php, line 14
Namespace
Drupal\rules\FormView source
class EditExpressionForm extends FormBase {
/**
* The edited component.
*
* @var \Drupal\rules\Engine\RulesComponent
*/
protected $component;
/**
* The RulesUI handler of the currently active UI.
*
* @var \Drupal\rules\Ui\RulesUiHandlerInterface
*/
protected $rulesUiHandler;
/**
* The UUID of the edited expression in the rule.
*
* @var string
*/
protected $uuid;
/**
* Gets the currently edited expression from the given component.
*
* @param \Drupal\rules\Engine\RulesComponent $component
* The component from which to get the expression.
*
* @return \Drupal\rules\Engine\ExpressionInterface|null
* The expression object.
*/
protected function getEditedExpression(RulesComponent $component) {
$rule_expression = $component
->getExpression();
return $rule_expression
->getExpression($this->uuid);
}
/**
* {@inheritdoc}
*/
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;
// During form rebuilds, keep track of changes using form state.
$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;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rules_expression_edit';
}
/**
* Builds an updated component object based upon the submitted form values.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\rules\Engine\RulesComponent
* The updated component.
*/
protected function buildComponent(array $form, FormStateInterface $form_state) {
$component = clone $this->component;
// In order to update the whole component we need to invoke the submission
// handler of the expression form. That way the expression gets changed
// accordingly.
$expression = $this
->getEditedExpression($component);
$form_handler = $expression
->getFormHandler();
$form_handler
->submitForm($form, $form_state);
return $component;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Ensure the object properties are initialized, see
// https://www.drupal.org/node/2669032.
$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);
// @todo This ignores ExpressionFormInterface::validateForm().
$component = $this
->buildComponent($form, $form_state);
$violations = $component
->checkIntegrity();
// Only show the violations caused by the edited expression.
foreach ($violations
->getFor($this->uuid) as $violation) {
$form_state
->setError($form, $violation
->getMessage());
}
}
/**
* {@inheritdoc}
*/
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());
}
/**
* Provides the page title on the form.
*/
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(),
]);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EditExpressionForm:: |
protected | property | The edited component. | |
EditExpressionForm:: |
protected | property | The RulesUI handler of the currently active UI. | |
EditExpressionForm:: |
protected | property | The UUID of the edited expression in the rule. | |
EditExpressionForm:: |
protected | function | Builds an updated component object based upon the submitted form values. | |
EditExpressionForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
1 |
EditExpressionForm:: |
protected | function | Gets the currently edited expression from the given component. | 1 |
EditExpressionForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EditExpressionForm:: |
public | function | Provides the page title on the form. | 1 |
EditExpressionForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
1 |
EditExpressionForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |