You are here

public function ReactionRuleAddForm::form in Rules 8.3

Gets the actual form array to be built.

Overrides RulesComponentFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ReactionRuleAddForm.php, line 71

Class

ReactionRuleAddForm
Provides a form to add a reaction rule.

Namespace

Drupal\rules\Form

Code

public function form(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 + parent::form($form, $form_state);
}