function AddEventFormTrait::buildEventForm

2 calls to AddEventFormTrait::buildEventForm()
AddEventForm::buildForm in src/Form/AddEventForm.php
Form constructor.
ReactionRuleAddForm::form in src/Form/ReactionRuleAddForm.php
Gets the actual form array to be built.

File

src/Form/AddEventFormTrait.php, line 37

Class

AddEventFormTrait
Trait for adding an event.

Namespace

Drupal\rules\Form

Code

public function buildEventForm(array $form, FormStateInterface $form_state) {
    $event_definitions = $this->eventManager
        ->getGroupedDefinitions();
    $options = [];
    foreach ($event_definitions as $group => $definitions) {
        foreach ($definitions as $id => $definition) {
            $options[$group][$id] = $definition['label'];
        }
    }
    $form['#entity_builders'][] = '::entityBundleBuilder';
    $form['selection'] = [
        '#type' => 'details',
        '#title' => $this->t('Event selection'),
        '#open' => TRUE,
    ];
    $form['selection']['events'] = [
        '#tree' => TRUE,
    ];
    // Selection of an event will trigger an Ajax request to see if this is an
    // entity event; if so, present a select element to choose a bundle type.
    $form['selection']['events'][]['event_name'] = [
        '#type' => 'select',
        '#title' => $this->t('React on event'),
        '#options' => $options,
        '#description' => $this->t('Rule evaluation is triggered whenever the selected event occurs.'),
        '#required' => TRUE,
        '#ajax' => [
            'event' => 'change',
            'wrapper' => 'entity-bundle-restriction',
            'callback' => '::bundleSelectCallback',
        ],
    ];
    // Empty container to hold the bundle selection element, if available
    // for the event chosen above.
    $form['selection']['container'] = [
        '#type' => 'container',
        '#attributes' => [
            'id' => 'entity-bundle-restriction',
        ],
    ];
    $event_name = $form_state->getValue([
        'events',
        0,
        'event_name',
    ]);
    // On form reload via Ajax, the $event_name will be set.
    if (!empty($event_name)) {
        // Add a non-required select element "Restrict by type" to choose from
        // all the bundles defined for the entity type.
        $event_definition = $this->eventManager
            ->getDefinition($event_name);
        $handler_class = $event_definition['class'];
        if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
            // We have bundles ...
            $bundles = $this->entityBundleInfo
                ->getBundleInfo($event_definition['entity_type_id']);
            // Transform the $bundles array into a form suitable for select options.
            array_walk($bundles, function (&$value, $key) {
                $value = $value['label'];
            });
            // Bundle selections for this entity type.
            $form['selection']['container']['bundle'] = [
                '#type' => 'select',
                '#title' => $this->t('Restrict by type'),
                '#empty_option' => '- None -',
                '#empty_value' => 'notselected',
                '#options' => $bundles,
                '#description' => $this->t('If you need to filter for multiple values, either add multiple events or use the "Entity is of bundle" condition. These options are available after saving this form.'),
            ];
        }
    }
    return $form;
}