0

I read a lot of documentation, specially this post https://www.drupal.org/node/2009252 but doesnt work for me.

This is my code, and the main problem is that i dont get the javascript in function hcat_my_account_create_companion_ajax_callback from the page callback 'my-account/ajax/create-companion' then I cant send my form by ajax.

function hcat_my_account_menu() {
    $items = array();

    $items['my-account/ajax/create-companion'] = array(
        'page callback' => 'hcat_my_account_create_companion_ajax_callback',
        'access callback' => TRUE,
     );

    return $items; 
}

function hcat_my_account_create_companion_ajax_callback($form, &$form_state) {  

    $submission = (object) array();
    $form = render(drupal_get_form('hcat_my_account_acomp_form', null, $submission, true, false));

    $settings = FALSE;
    $javascript = drupal_add_js(NULL, NULL);
    if(isset($javascript['settings'], $javascript['settings']['data'])) {
        $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, '.drupal_json_encode(call_user_func_array('array_merge_recursive', $javascript['settings']['data'])).'); console.log("He cargado el Drupal.settings");</script>';
    }

    form_load_include($form_state, 'inc', 'node', 'node.pages');
//I CANT PASS ANY JAVASCRIPT. WILL DISAPPEAR.
    return "<div id='new-form-ajax'>".$form.$settings."</div>";
}

function hcat_my_account_acomp_form($form, &$form_state, $vars) {

    $form['hola'] = array(
        '#type' => 'textfield',
        '#default_value' => "Vamos",
        '#attributes' => array('class' => array('my-account-input-edit')),
    );
    $form['submit_save'] = array(
          '#type' => 'submit',
          '#value' => 'Vamos que te quda poco',
          '#ajax' => array(
                'callback' => "hcat_my_account_create_attend_form_submit_ajax",
                'wrapper' => "hcat-my-account-acomp-form",
                'method' => 'replace',
                'effect' => 'fade',
              ),
    );

    form_load_include($form_state, 'inc', 'node', 'node.pages');

    return $form;
}

function hcat_my_account_create_attend_form_submit_ajax($form, &$form_state) {
    return "Desde la funcion hcat_my_account_create_attend_form_submit_ajax";
}
  • Why does the function need to start with an underscore? – Kevin Jun 19 '18 at 12:13
  • Is not relevant, i can change the name. – valerogarte Jun 19 '18 at 12:38
  • It’s a convention @Kevin - a preceding underscore indicates the function is private to the module, and other modules shouldn’t rely on the implementation staying the same – Clive Jun 19 '18 at 13:28
  • Okey. Thanks you. I changed it, but i have the same problem. Any idea? – valerogarte Jun 19 '18 at 13:48
0

I found this way to resolve it. Creating another form whose submit return the form that i want. The code is:

function my_module_botton_create_form($form, &$form_state) {
    $form['create_form'] = array(
        '#type' => 'submit',
        '#value' => '',
        '#ajax' => array(
            'callback' => "my_module_to_create_form",
            'wrapper' => "new_acomp_form",
            'method' => 'replace',
            'effect' => 'fade',
          ),
    );

    return $form;
}

function my_module_to_create_form($form, &$form_state) {

    $form=drupal_get_form("my_module_acomp_form");

        $output = array(
            '#type' => 'ajax',
            '#commands' => array(
                array('command' => 'ajax_create_form_acomp', 'data' => drupal_render($form)),
            ),
        );

        return $output;
}

function my_module_acomp_form($form, &$form_state) {

    ...

    return $form;
}

The JS is

Drupal.ajax.prototype.commands.ajax_create_form_acomp = function (ajax, response, status) {
    $('#new_acomp_form').replaceWith(response.data+"<div id='new_acomp_form'></div>");
    //Drupal.behaviors.hcat_my_account.check_if_attendant_exits();
};
| improve this answer | |

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.