You are here

function rules_forms_invoke_event in Rules Forms Support 7

Same name and namespace in other branches
  1. 7.2 rules_forms.module \rules_forms_invoke_event()

Invoke rules event of a certain type.

Parameters

string $event_type: The type of Rules Forms event being invoked.

  • form_built
  • form_validate
  • form_submit
  • button_ELEMENT_ID_validate
  • button_ELEMENT_ID_submit.

mixed $form: A reference to the form array for which the event is being invoked.

mixed $form_state: A reference to the form state array for which the event is being invoked.

string|null $form_id: The form ID of the form for which the event is being invoked, or NULL. If NULL the form ID will be retrieved from the $form array.

See also

rules_forms_event_validate()

rules_forms_event_submit()

rules_forms_form_alter()

5 calls to rules_forms_invoke_event()
rules_forms_event_build in ./rules_forms.module
Form build handler. Stores form values and invokes the event.
rules_forms_event_button_submit in ./rules_forms.module
Submit handler for button level submissions.
rules_forms_event_button_validate in ./rules_forms.module
Validation handler for button level submission handling.
rules_forms_event_submit in ./rules_forms.module
Submit handler to invoke "form submitted" events.
rules_forms_event_validate in ./rules_forms.module
Validation handler to invoke "form validate" events.

File

./rules_forms.module, line 212
Rules Forms Support provides events, conditions, and actions for site forms.

Code

function rules_forms_invoke_event($event_type, &$form, &$form_state, $form_id = NULL, $entity = NULL, $entity_type = NULL) {
  global $user;
  if (empty($form_id)) {
    $form_id = $form['form_id']['#value'];
  }

  // Create an array of form element IDs to be passed as arguments.
  $elements = array();
  if ($element_ids = rules_forms_get_element_ids($form_id)) {
    $elements = array_combine($element_ids, $element_ids);
  }

  // Prepare form data to be passed by reference as ArrayObjects.
  $form_data = new ArrayObject((array) $form);
  $form_state_data = new ArrayObject((array) $form_state);

  // Forms are passed by reference via the ArrayObject class.
  // Element IDs for the form are appended and passed as arguments.
  // This allows us to use a select list in rules for selecting
  // form elements rather than entering an element ID.
  $args = array(
    "rules_forms_{$form_id}_{$event_type}",
    $form_data,
    $form_state_data,
    $form_id,
    entity_metadata_wrapper($entity_type, $entity),
    $user,
  ) + $elements;
  call_user_func_array('rules_invoke_event', $args);

  // Repopulate form arrays to update the form.
  $form = (array) $form_data;
  $form_state = (array) $form_state_data;

  // If a submit event has been invoked (form or button level) unset
  // the form values session variable.
  if (strpos($event_type, 'submit', strlen($event_type) - 7) !== FALSE) {
    unset($form_state['rules_forms']);
  }
}