You are here

class AddExpressionForm in Rules 8.3

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

Hierarchy

Expanded class hierarchy of AddExpressionForm

File

src/Form/AddExpressionForm.php, line 16

Namespace

Drupal\rules\Form
View source
class AddExpressionForm extends EditExpressionForm {

  /**
   * The Rules expression manager to get expression plugins.
   *
   * @var \Drupal\rules\Engine\ExpressionManagerInterface
   */
  protected $expressionManager;

  /**
   * The expression ID that is added, example: 'rules_action'.
   *
   * @var string
   */
  protected $expressionId;

  /**
   * Creates a new object of this class.
   */
  public function __construct(ExpressionManagerInterface $expression_manager) {
    $this->expressionManager = $expression_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.rules_expression'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, RulesUiHandlerInterface $rules_ui_handler = NULL, $uuid = NULL, $expression_id = NULL) {
    $this->expressionId = $expression_id;
    $this->uuid = $uuid;

    // When initially adding the expression, we have to initialize the object
    // and add the expression.
    if (!$this->uuid) {

      // Before we add our edited expression to the component's expression,
      // we clone it such that we do not change the source component until
      // the form has been successfully submitted.
      $component = clone $rules_ui_handler
        ->getComponent();
      $this->uuid = $this
        ->getEditedExpression($component)
        ->getUuid();
      $form_state
        ->set('component', $component);
      $form_state
        ->set('uuid', $this->uuid);
    }
    return parent::buildForm($form, $form_state, $rules_ui_handler, $this->uuid);
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditedExpression(RulesComponent $component) {
    $component_expression = $component
      ->getExpression();
    if (!$component_expression instanceof ExpressionContainerInterface) {
      throw new LogicException('Cannot add expression to expression of type ' . $component_expression
        ->getPluginId());
    }
    if ($this->uuid && ($expression = $component_expression
      ->getExpression($this->uuid))) {
      return $expression;
    }
    else {
      $expression = $this->expressionManager
        ->createInstance($this->expressionId);
      $rule_expression = $component
        ->getExpression();
      $rule_expression
        ->addExpressionObject($expression);
      return $expression;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $form_state
      ->setRedirectUrl($this->rulesUiHandler
      ->getBaseRouteUrl());
  }

  /**
   * Provides the page title on the form.
   */
  public function getTitle(RulesUiHandlerInterface $rules_ui_handler, $expression_id) {
    $this->expressionId = $expression_id;
    $expression = $this->expressionManager
      ->createInstance($this->expressionId);
    return $this
      ->t('Add @expression', [
      '@expression' => $expression
        ->getLabel(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddExpressionForm::$expressionId protected property The expression ID that is added, example: 'rules_action'.
AddExpressionForm::$expressionManager protected property The Rules expression manager to get expression plugins.
AddExpressionForm::buildForm public function Form constructor. Overrides EditExpressionForm::buildForm
AddExpressionForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
AddExpressionForm::getEditedExpression protected function Gets the currently edited expression from the given component. Overrides EditExpressionForm::getEditedExpression
AddExpressionForm::getTitle public function Provides the page title on the form. Overrides EditExpressionForm::getTitle
AddExpressionForm::submitForm public function Form submission handler. Overrides EditExpressionForm::submitForm
AddExpressionForm::__construct public function Creates a new object of this class.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
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::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EditExpressionForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 1
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. 1
FormBase::container private function Returns the service container.
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.