function ActionForm::form

Overrides ExpressionFormInterface::form

File

src/Form/Expression/ActionForm.php, line 45

Class

ActionForm
UI form for adding/editing a Rules action.

Namespace

Drupal\rules\Form\Expression

Code

public function form(array $form, FormStateInterface $form_state) {
    $action_id = $form_state->get('action_id');
    $configuration = $this->actionExpression
        ->getConfiguration();
    if (empty($action_id) && !empty($configuration['action_id'])) {
        $action_id = $configuration['action_id'];
        $form_state->set('action_id', $action_id);
    }
    // Step 1 of the multistep form.
    if (!$action_id) {
        $action_definitions = $this->actionManager
            ->getGroupedDefinitions();
        $options = [];
        foreach ($action_definitions as $group => $definitions) {
            foreach ($definitions as $id => $definition) {
                $options[$group][$id] = $definition['label'];
            }
        }
        $form['action_id'] = [
            '#type' => 'select',
            '#title' => $this->t('Action'),
            '#options' => $options,
            '#required' => TRUE,
        ];
        $form['continue'] = [
            '#type' => 'submit',
            '#value' => $this->t('Continue'),
            '#name' => 'continue',
            // Only validate the selected action in the first step.
'#limit_validation_errors' => [
                [
                    'action_id',
                ],
            ],
            '#submit' => [
                static::class . '::submitFirstStep',
            ],
        ];
        return $form;
    }
    // Step 2 of the form.
    $action = $this->actionManager
        ->createInstance($action_id);
    $form['summary'] = [
        '#type' => 'details',
        '#title' => $this->t('Summary'),
    ];
    $form['summary']['description'] = [
        '#type' => 'container',
        '#plain_text' => $this->t('Action: @summary', [
            '@summary' => $action->summary(),
        ]),
        '#attributes' => [
            'class' => [
                'form-item',
            ],
        ],
    ];
    $context_definitions = $action->getContextDefinitions();
    if (!empty($context_definitions)) {
        $form['context_definitions'] = [
            '#type' => 'details',
            '#title' => $this->t('Context variables'),
            '#open' => TRUE,
            '#tree' => TRUE,
        ];
        foreach ($context_definitions as $context_name => $context_definition) {
            $form = $this->buildContextForm($form, $form_state, $context_name, $context_definition, $configuration);
        }
    }
    $provides_definitions = $action->getProvidedContextDefinitions();
    if (!empty($provides_definitions)) {
        $form['provides'] = [
            '#type' => 'details',
            '#title' => $this->t('Provided variables'),
            '#description' => $this->t('You may change the name of any provided variables, but note that renaming already-utilized variables invalidates the existing uses.'),
            '#tree' => TRUE,
        ];
        foreach ($provides_definitions as $provides_name => $provides_definition) {
            $form = $this->buildProvidedContextForm($form, $form_state, $provides_name, $provides_definition, $configuration);
        }
    }
    $form['save'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save'),
        '#name' => 'save',
    ];
    return $form;
}