You are here

function rules_webform_form_rules_expression_edit_alter in RULES WEBFORM 3.x

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

Implements hook_form_FORM_ID_alter().

Add the 'webform_fields' properties definitions to the context variable. The information are then using in the 'WebformFieldsDataDefinition' class and in the 'WebformFieldsUnchangedDataDefinition' class. And it's needed for autocomplete of properties of 'webform_fields' context variable and the 'webform_fields_unchanged' context variable. Also, removing default 'Webform submission' action from the Action selection list. And also removing condition 'webform name from the condition list, because it set programmatically by default. The module doesn't work with Rules components (actions and variables are not available). Therefore if this hook was called when a user edit a Rule component then hide related actions and condition.

File

./rules_webform.module, line 239
Contains rules_webform.module.

Code

function rules_webform_form_rules_expression_edit_alter(&$form, FormStateInterface $form_state, $form_id) {
  $reaction_rule = \Drupal::request()
    ->get('rules_reaction_rule');

  // If this hook was called when a user edit a Rule component then hide
  // related actions and condition.
  // The module doesn't work with Rules components (actions and variables
  // are not available).
  if (is_null($reaction_rule)) {

    // Removing our actions 'Delete webform submission' and 'Set webform
    // field value' from the action list.
    unset($form['action_id']['#options']['A Webform']);

    // Removing our condition 'webform_name' from the condition list.
    unset($form['condition_id']['#options']['A Webform']);
    return;
  }
  $event_name = $reaction_rule
    ->getEventNames()[0];

  // Events supported by the module.
  $webform_events = [
    'webform_submit',
    'updating_submission',
    'deleting_submission',
    'viewing_submission',
  ];
  if (!in_array($event_name, $webform_events)) {

    // Removing our condition 'webform_name' from the condition list.
    unset($form['condition_id']['#options']['A Webform']);

    // Removing our actions because they are unuseful for other events.
    unset($form['action_id']['#options']['A Webform']);
    return;
  }

  // Removing default 'Webform submission' actions from the Action selection
  // list.
  unset($form['action_id']['#options']['Webform submission']);

  // Removing our condition 'webform name from the condition list, because it
  // set programmatically by default.
  unset($form['condition_id']['#options']['A Webform']);

  // Get the information about a webform fields.
  $rule_id = $reaction_rule->id;
  $webform_id = \Drupal::state()
    ->get('rules_webform.webform_id.' . $rule_id);
  $webform = Webform::load($webform_id);
  $elements = $webform
    ->getElementsInitializedAndFlattened();
  $fields_definitions = [];
  rules_webform_extract_composite_elements($elements, $fields_definitions);

  // Store webform fields information for using in the
  // 'WebformFieldsDataDefinition' class and in
  // the'WebformFieldsUnchangedDataDefinition' class.
  \Drupal::state()
    ->set('rules_webform.fields_definitions', $fields_definitions);

  // If this is the form of 'set_webform_field_value' action
  // then store event name.
  if (isset($form_state
    ->getStorage()['action_id'])) {
    if ($form_state
      ->getStorage()['action_id'] == 'set_webform_field_value') {

      // Store 'event_name' for using in "set_webform_field_value" action.
      $form['context_definitions']['event_name']['setting']['#default_value'] = $event_name;
      $form['context_definitions']['event_name']['#access'] = FALSE;
    }
  }

  // If this is add 'Action' form then alter it.
  if (isset($form['action_id'])) {

    // If event is 'Deleting a submission' then remove 'Deleting a submission'
    // and 'Set webform field value' actions.
    if ($event_name == 'deleting_submission') {
      unset($form['action_id']['#options']['A Webform']);
    }

    // Add validate handler to add and setup 'delete_webform_submission'
    // action if user will select it.
    $form['continue']['#validate'] = [
      'rules_webform_form_rules_expression_edit_validate',
    ];
  }
}