0

I'm building custom form rule which is having $form_state as rule input parameter, however I would like to use drupal_form_submit() which requires $form_id.

Is it possible to fetch/compute automatically $form_id based on its $form_state to make my rule generic?

Or maybe there is some documentation that explains what $form_state should consist exactly?

My rule is like:

/**
 * Implements hook_rules_action_info().
 */
function rules_forms_rules_action_info() {
  $actions['rules_forms_submit'] = array(
    'label' => t('Submit the form'),
    'group' => 'Rules Forms',
    'parameter' => array(
      'form_state' => array('type' => 'form_state', 'label' => t('Form state')),
    ),
    'base' => 'rules_forms_action_submit',
    'access callback' => 'rules_forms_integration_access',
  );
  return $actions;
}

/**     
 * Action: Submit the form.
 */
function rules_forms_action_submit($form_state) {
  // $form_id = ?
  drupal_form_submit($form_id, $form_state);
}

I've checked $form_state and it's a huge (~0,5MB size-like) ArrayObject with the following main keys/properties:

build_info, temporary, programmed, programmed_bypass_access_check, cache, entityform, entityform_form_mode, field, storage, rebuild, rebuild_info, redirect, submitted, executed, method, groups, buttons, input, values, complete form, process_input, invalid_token, triggering_element, submit_handlers, clicked_button

2 Answers 2

2

I think the easiest way was to pass $form ArrayObject which has $form_id present. So:

'form' => array('type' => 'form', 'label' => t('Form')),

needed to be added next to $form_state parameter in hook_rules_action_info and the final function looks like:

/**
 * Action: Submit the form.
 *
 * @param ArrayObject $form
 *   A reference to the form array of the form for which the event
 *   was triggered.
 * @param ArrayObject $form_state
 *   A reference to the form state array of the form for which the
 *   event was triggered.
 */
function rules_forms_action_submit($form, $form_state) {
  $form_id = $form['form_id']['#value'];
  $form_state = (array)$form_state;
  drupal_form_submit($form_id, $form_state);
}

For submitting entity form, check: How to programatically submit an entityform?

0

Given $form_state of an array or ArrayObject, the Form ID can be found in:

$form_state['build_info']['form_id']

or:

$form_state['build_info']['base_form_id']

as this location is used in Drupal core functions (e.g. drupal_prepare_form()) so it should be pretty reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.