You are here

public function SubmitButtonRules::configurationFormSubmitAddRule in Flexiform 8

Form submit for adding a rule.

File

contrib/rules/src/Plugin/FormEnhancer/SubmitButtonRules.php, line 230

Class

SubmitButtonRules
A flexiform enhancer to trigger rules on submission.

Namespace

Drupal\flexiform_rules\Plugin\FormEnhancer

Code

public function configurationFormSubmitAddRule($form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $array_parents = $button['#array_parents'];
  array_pop($array_parents);
  array_pop($array_parents);
  $row = NestedArray::getValue($form, $array_parents);
  $values = $form_state
    ->getValue($row['#parents']);
  $rule_info = $values['rule'];
  $component = $this
    ->rulesStorage()
    ->create([
    'label' => $rule_info['label'],
    'id' => $rule_info['id'],
  ]);
  $component
    ->setExpression($this->expressionManager
    ->createRule());
  $context_definitions = $this->formDisplay
    ->getFormEntityManager()
    ->getContextDefinitions();
  $rules_context_definitions = [];
  foreach ($context_definitions as $namespace => $context_definition) {

    // I don't expext rules will cope very well with an empty $namespace.
    if ($namespace == $this->formDisplay
      ->getBaseEntityNamespace() && $namespace == '') {
      $namespace = 'base_entity';
    }
    $rules_context_definitions[$namespace] = new RulesContextDefinition($context_definition
      ->getDataType(), $context_definition
      ->getLabel(), $context_definition
      ->isRequired(), $context_definition
      ->isMultiple(), $context_definition
      ->getDescription(), $context_definition
      ->getDefaultValue());
    $rules_context_definitions[$namespace]
      ->setConstraints($context_definition
      ->getConstraints());
  }
  $component
    ->setContextDefinitions($rules_context_definitions);
  $component
    ->save();
  $this->configuration[$button['#submit_path']]['rules'][$component
    ->id()] = [
    'weight' => $values['weight'],
  ];

  // Unset the input.
  $input =& $form_state
    ->getUserInput();
  NestedArray::setValue($input, $row['#parents'], []);

  // Set the form to rebuild.
  $form_state
    ->setRebuild();
}