0

In a preprocess_node hook, I try to insert a Drupal node_form form in my page. I do it with this function:

function my_module_get_edit_form($selection) {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'drupal.form');
  $form = drupal_get_form('selection_node_form', $selection);

  //here some code to disable (#access = FALSE) unwanted fields and options

  $form['actions']['submit'] = array(
    '#type' => 'button',
    '#value' => 'Update',
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'smll_selection_ajax_callback',
      'wrapper' => 'selection_form'
    ),
  );

  return drupal_render($form);
}

The form is rendered properly but when I click my submit button, it always submits the form and forwards me to the node's page, no ajax call.

I tried adding #ajax array onto a select field in the same form ($form['field_application_type']['und']['#ajax']) but nothing is triggered when I change the select's value. No HTTP request, no Javascript error, nothing.

I tried using drupal_build_form with a manufactured $form_state.

I tried creating my form in an another function and calling it with drupal_build_form.

I tried setting #ajax_processed to TRUE.

The HTML id selection_form exists but, anyway, I never come to the moment the callback function is called.

  • What's in your smll_selection_ajax_callback? – GwenM Jan 19 '16 at 13:49
  • return "Yipee"; But it is never called. – Kodiak Jan 19 '16 at 14:31
  • it seems it's not taking your actions at all, try to add a class attribute to see if it works. dsm the form before and after your changes and see if it actually works. – GwenM Jan 19 '16 at 14:53
0

You don't use the correct function for that;

function MYMODULE_form_alter(&$form, &form_state, $form_id) {
  if($form_id == 'selection_node_form' //and same condition you call your function){
    //do your changes here
  }
}

Or use drupal_retrieve_form then you can override the form.
The drupal_get_form call the function drupal_build_form which build the form, you can't override it that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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