function ReactionRuleEditForm::form

Overrides RulesComponentFormBase::form

File

src/Form/ReactionRuleEditForm.php, line 75

Class

ReactionRuleEditForm
Provides a form to edit a reaction rule.

Namespace

Drupal\rules\Form

Code

public function form(array $form, FormStateInterface $form_state) {
    $form['events'] = [
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'edit-events',
            ],
        ],
    ];
    $form['events']['table'] = [
        '#theme' => 'table',
        '#header' => [
            $this->t('Events'),
            $this->t('Operations'),
        ],
        '#empty' => $this->t('None'),
    ];
    foreach ($this->entity
        ->getEventNames() as $key => $event_name) {
        $event_definition = $this->eventManager
            ->getDefinition($event_name);
        $form['events']['table']['#rows'][$key]['element'] = [
            'data' => [
                '#type' => 'item',
                '#plain_text' => $event_definition['label'],
                '#suffix' => '<div class="description">' . $this->t('Machine name: @name', [
                    '@name' => $event_name,
                ]) . '</div>',
            ],
        ];
        // Provide a delete button ONLY IF there is more than one event.
        if (count($this->entity
            ->getEventNames()) > 1) {
            $form['events']['table']['#rows'][$key]['operations'] = [
                'data' => [
                    '#type' => 'operations',
                    '#links' => [
                        'delete' => [
                            'title' => $this->t('Delete'),
                            'url' => $this->rulesUiHandler
                                ->getUrlFromRoute('event.delete', [
                                'id' => $event_name,
                            ]),
                        ],
                    ],
                ],
            ];
        }
        else {
            $form['events']['table']['#rows'][$key]['element']['colspan'] = 2;
        }
    }
    // Put action buttons in the table footer.
    $links['add-event'] = [
        '#theme' => 'menu_local_action',
        '#link' => [
            'title' => $this->t('Add event'),
            'url' => $this->rulesUiHandler
                ->getUrlFromRoute('event.add', []),
        ],
    ];
    $form['events']['table']['#footer'][] = [
        [
            'data' => [
                '#prefix' => '<ul class="action-links">',
                'local-action-links' => $links,
                '#suffix' => '</ul>',
            ],
            'colspan' => 2,
        ],
    ];
    // CSS to make form easier to use. Load this at end so we can override
    // styles added by #theme table.
    $form['#attached']['library'][] = 'rules/rules_ui.styles';
    $form = $this->rulesUiHandler
        ->getForm()
        ->buildForm($form, $form_state);
    return parent::form($form, $form_state);
}