You are here

function rules_webform_form_rules_reaction_rule_add_form_alter in RULES WEBFORM 3.x

Same name and namespace in other branches
  1. 8 rules_webform.module \rules_webform_form_rules_reaction_rule_add_form_alter()

Add the element for a 'Webform id' selection to the rule adding form.

Implements hook_form_FORM_ID_alter().

File

./rules_webform.module, line 23
Contains rules_webform.module.

Code

function rules_webform_form_rules_reaction_rule_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Create the element for a webform selection and hide it and its label.
  // It will made visible with JS if a user selects any web form event.
  $form['selection']['webform_id'] = [
    '#type' => 'select',
    '#title' => t('Webform'),
    '#empty_option' => t('- Select -'),
    '#attributes' => [
      'style' => 'display: none;',
    ],
    '#label_attributes' => [
      'style' => 'display: none;',
    ],
  ];
  $form['#attached']['library'][] = 'rules_webform/rules_webform.select_webform_id';
  $query = \Drupal::entityTypeManager()
    ->getStorage('webform')
    ->getQuery();
  $webform_ids = $query
    ->execute();
  $webforms = Webform::loadMultiple($webform_ids);
  foreach ($webforms as $webform) {
    $form['selection']['webform_id']['#options'][$webform
      ->get('id')] = $webform
      ->get('title');
  }

  // Remove default Webform submission events from the list.
  unset($form['selection']['events'][0]['event_name']['#options']['Webform submission']);
  $form['actions']['submit']['#submit'][] = 'rules_webform_check_if_webform_submit_event_selected';
}