You are here

public function SubmitButtonRules::configurationForm in Flexiform 8

The configuration form.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The form with any additions.

Overrides ConfigurableFormEnhancerInterface::configurationForm

File

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

Class

SubmitButtonRules
A flexiform enhancer to trigger rules on submission.

Namespace

Drupal\flexiform_rules\Plugin\FormEnhancer

Code

public function configurationForm(array $form, FormStateInterface $form_state) {
  foreach ($this
    ->locateSubmitButtons() as $path => $label) {
    $original_path = $path;
    $path = str_replace('][', '::', $path);
    $form[$path] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@label Button Submission Rules', [
        '@label' => $label,
      ]),
      '#description' => 'Array Parents: ' . $original_path,
      '#tree' => TRUE,
      '#open' => TRUE,
    ];
    $form[$path]['rules'] = [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Rule'),
        $this
          ->t('Weight'),
        $this
          ->t('Operations'),
      ],
      '#empty' => $this
        ->t('This submit button has no rules configured.'),
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'rule-weight',
        ],
      ],
    ];
    $max_weight = 0;
    if (!empty($this->configuration[$path]['rules'])) {
      foreach ($this->configuration[$path]['rules'] as $rule_name => $info) {
        $rule = $this
          ->rulesStorage()
          ->load($rule_name);
        if (!$rule) {
          continue;
        }
        $form[$path]['rules'][$rule_name] = [
          '#attributes' => [
            'class' => [
              'draggable',
            ],
          ],
          '#weight' => $info['weight'],
          'rule' => $rule
            ->toLink($rule
            ->label(), 'edit-form')
            ->toRenderable(),
          'weight' => [
            '#type' => 'weight',
            '#title' => $this
              ->t('Execution Order for @title', [
              '@title' => $rule
                ->label(),
            ]),
            '#title_display' => 'invisible',
            '#default_value' => $info['weight'],
            '#attributes' => [
              'class' => [
                'rule-weight',
              ],
            ],
          ],
          'operations' => [
            '#type' => 'container',
            'remove' => [
              '#type' => 'submit',
              '#value' => $this
                ->t('Remove @title', [
                '@title' => $rule
                  ->label(),
              ]),
              '#submit' => [
                [
                  $this,
                  'configurationFormSubmitRemoveRule',
                ],
              ],
              '#submit_path' => $path,
              '#rule_name' => $rule
                ->id(),
            ],
          ],
        ];
        $max_weight = $max_weight > $info['weight'] ? $max_weight : $info['weight'];
      }
    }
    $parents = $form['#parents'];
    $form[$path]['rules']['__new_rule'] = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
      '#weight' => $max_weight + 1,
      'rule' => [
        '#type' => 'container',
        'label' => [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Label'),
        ],
        'id' => [
          '#type' => 'machine_name',
          '#required' => FALSE,
          '#description' => $this
            ->t('A unique machine-readable name. Can only contain lowecase letters numbers and underscores.'),
          '#machine_name' => [
            'exists' => [
              static::class,
              'configurationFormRuleExists',
            ],
            'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
            'source' => array_merge($form['#array_parents'], [
              $path,
              'rules',
              '__new_rule',
              'rule',
              'label',
            ]),
          ],
        ],
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Execution Order for New Rule'),
        '#title_display' => 'invisible',
        '#default_value' => $max_weight,
        '#attributes' => [
          'class' => [
            'rule-weight',
          ],
        ],
      ],
      'operations' => [
        '#type' => 'container',
        'add' => [
          '#name' => 'add' . $path . 'rule',
          '#type' => 'submit',
          '#value' => $this
            ->t('Add New Rule'),
          '#submit' => [
            [
              $this,
              'configurationFormSubmitAddRule',
            ],
          ],
          '#submit_path' => $path,
          '#limit_validation_errors' => [
            array_merge($form['#parents'], [
              $path,
              'rules',
              '__new_rule',
            ]),
          ],
        ],
      ],
    ];
  }
  return $form;
}