class EditExpressionForm

UI form to edit an expression like a condition or action in a rule.

Hierarchy

Expanded class hierarchy of EditExpressionForm

File

src/Form/EditExpressionForm.php, line 14

Namespace

Drupal\rules\Form
View 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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditExpressionForm::$component protected property The edited component.
EditExpressionForm::$rulesUiHandler protected property The RulesUI handler of the currently active UI.
EditExpressionForm::$uuid protected property The UUID of the edited expression in the rule.
EditExpressionForm::buildComponent protected function Builds an updated component object based upon the submitted form values.
EditExpressionForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
EditExpressionForm::getEditedExpression protected function Gets the currently edited expression from the given component. 1
EditExpressionForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EditExpressionForm::getTitle public function Provides the page title on the form. 1
EditExpressionForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm 1
EditExpressionForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.