You are here

class ConditionContainerForm in Rules 8.3

Form view structure for Rules condition containers.

Hierarchy

Expanded class hierarchy of ConditionContainerForm

File

src/Form/Expression/ConditionContainerForm.php, line 11

Namespace

Drupal\rules\Form\Expression
View source
class ConditionContainerForm extends ExpressionContainerFormBase {

  /**
   * The rule expression object this form is for.
   *
   * @var \Drupal\rules\Engine\ConditionExpressionContainerInterface
   */
  protected $conditionContainer;

  /**
   * Creates a new object of this class.
   */
  public function __construct(ConditionExpressionContainerInterface $condition_container) {
    $this->conditionContainer = $condition_container;
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form['conditions-table'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'edit-conditions-table',
        ],
      ],
    ];
    $form['conditions-table']['conditions'] = [
      '#type' => 'table',
      '#header' => [
        'element' => $this
          ->t('Conditions'),
        'operations' => $this
          ->t('Operations'),
        'weight' => [
          'data' => $this
            ->t('List position'),
          'class' => [
            'tabledrag-hide',
          ],
        ],
      ],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'table-sort-weight',
        ],
      ],
      '#empty' => $this
        ->t('None'),
    ];

    /** @var \Drupal\rules\Engine\ExpressionInterface $condition */
    foreach ($this->conditionContainer as $condition) {
      $uuid = $condition
        ->getUuid();
      $configuration = $condition
        ->getConfiguration();
      $description = $this
        ->getParameterDescription($configuration);
      $form['conditions-table']['conditions'][$uuid] = [
        'element' => [
          'data' => [
            '#type' => 'item',
            '#plain_text' => $condition
              ->getLabel(),
            '#suffix' => '<div class="description">' . $description . '</div>',
          ],
          // So that the full parameter description will show on hover.
          '#wrapper_attributes' => [
            'title' => [
              $description,
            ],
          ],
        ],
        'operations' => [
          'data' => [
            '#type' => 'operations',
            '#links' => [
              'edit' => [
                'title' => $this
                  ->t('Edit'),
                'url' => $this
                  ->getRulesUiHandler()
                  ->getUrlFromRoute('expression.edit', [
                  'uuid' => $uuid,
                ]),
              ],
              'delete' => [
                'title' => $this
                  ->t('Delete'),
                'url' => $this
                  ->getRulesUiHandler()
                  ->getUrlFromRoute('expression.delete', [
                  'uuid' => $uuid,
                ]),
              ],
            ],
          ],
        ],
        'weight' => [
          '#type' => 'weight',
          '#delta' => 50,
          '#attributes' => [
            'class' => [
              'table-sort-weight',
            ],
          ],
          '#default_value' => $condition
            ->getWeight(),
        ],
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        '#weight' => $condition
          ->getWeight(),
      ];
    }

    // Put action buttons in the table footer.
    $links['add-condition'] = [
      '#theme' => 'menu_local_action',
      '#link' => [
        'title' => $this
          ->t('Add condition'),
        'url' => $this
          ->getRulesUiHandler()
          ->getUrlFromRoute('expression.add', [
          'expression_id' => 'rules_condition',
        ]),
      ],
    ];
    $form['conditions-table']['conditions']['#footer'][] = [
      [
        'data' => [
          '#prefix' => '<ul class="action-links">',
          'local-action-links' => $links,
          '#suffix' => '</ul>',
        ],
        'colspan' => 3,
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValue('conditions', []);
    if (empty($values)) {

      // Core FormState::getValue() doesn't return the default parameter []
      // when there are no values?
      return;
    }
    $component = $this
      ->getRulesUiHandler()
      ->getComponent();

    /** @var \Drupal\rules\Plugin\RulesExpression\RuleExpression $rule_expression */
    $rule_expression = $component
      ->getExpression();
    foreach ($values as $uuid => $expression) {
      $condition = $rule_expression
        ->getExpression($uuid);
      $condition
        ->setWeight($expression['weight']);
      $condition
        ->setConfiguration($condition
        ->getConfiguration());
    }
    $this
      ->getRulesUiHandler()
      ->updateComponent($component);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionContainerForm::$conditionContainer protected property The rule expression object this form is for.
ConditionContainerForm::form public function Adds elements specific to the expression to the form. Overrides ExpressionFormInterface::form
ConditionContainerForm::submitForm public function Implements ExpressionFormInterface::submitForm(). Overrides ExpressionFormTrait::submitForm
ConditionContainerForm::__construct public function Creates a new object of this class.
ExpressionContainerFormBase::getParameterDescription protected function Helper function to extract context parameter names/values from the config.
ExpressionFormTrait::validateForm public function Implements ExpressionFormInterface::validateForm().
RulesUiHandlerTrait::$rulesUiHandler protected property The rules UI handler.
RulesUiHandlerTrait::getRulesUiHandler public function Gets the rules UI handler of the current route.
RulesUiHandlerTrait::setRulesUiHandler public function Sets the Rules UI handler.
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.