Откройте модальный диалог формы в AJAX callback в drupal 7 с помощью ctools

По сути, я пытаюсь открыть модальный диалог (содержащий другую форму) во время обратного вызова формы AJAX. Пока я знаю, что могу вызвать модальные диалоги, используя

  • ссылка (класс: "ctools-use-modal")
  • форма, которая содержит кнопку и скрытый элемент URL

В моем случае мне нужно иметь возможность открывать форму (в модальном диалоге), зависящую от пользовательского ввода, например, результата элемента select-list-element.

Я вижу в основном два способа сделать это:

  1. Поместите AJAX-обратный вызов в список выбора, который изменяет скрытое поле URL -> откройте модальное с помощью кнопки. Это сработало, НО, когда я закрываю модальное диалоговое окно, обратный вызов ajax списка выбора больше не будет срабатывать (поэтому он работает только в первый раз).

  2. Оставьте кнопку нажатой и откройте модал непосредственно в AJAX-обратном вызове списков выбора. Это также работает, НО обратный вызов отправки модальных форм игнорируется.

У кого-нибудь есть идея решить эту проблему? Вот мой пример кода (версия 2):

/**
 * Implementation of hook_menu()
 */
function ajax_madness_menu() {
    $items = array();   
    $items['ajax-madness/dependant-modal'] = array(
        'title' => t("dependant modal"),
        'page callback' => 'ajax_madness_dependant_modal_page',     
        'access callback' => true,      
        'type' => MENU_CALLBACK,        
    );          
    $items['ajax-madness/dependant-modal/form/%'] = array(
        'title' => t("dependant modal"),
        'page callback' => 'drupal_get_form',
        'page arguments' => array("ajax_madness_dependant_modal_form",3),       
        'access callback' => true,      
        'type' => MENU_CALLBACK,        
    );  
    return $items;      
}

/**
 * Page with modal form selector
 */
function ajax_madness_dependant_modal_page() {
    ctools_include('ajax');
    ctools_include('modal');
    ctools_modal_add_js();  
    $build = array();
    $build["dependant_select_form"]["#markup"] = render(drupal_get_form("ajax_madnesss_select_modal_form"));    
    return $build;  
}

/**
 * Select the modal form
 */ 
function ajax_madnesss_select_modal_form($form,&$form_state) {
    $form["select"] = array(
        "#type" => "select",
        "#title" => t("Select modal form"),
        "#options" => array(
            1 => t("Modal form @number",array("@number" => 1)),
            2 => t("Modal form @number",array("@number" => 2)),
            3 => t("Modal form @number",array("@number" => 3)),
        ),  
        "#ajax" => array(
            "callback" => "ajax_madnesss_select_modal_form_ajax",
        ),
    );      
    return $form;       
}

/**
 * Select the modal form ajax callback
 */ 
function ajax_madnesss_select_modal_form_ajax($form,&$form_state) {
    ctools_include('ajax');
    ctools_include('modal');                

    $new_form_state = form_state_defaults();
    $new_form_state["ajax"] = true;
    $new_form_state["title"] = drupal_get_title();  
    $new_form_state['build_info']['args'] = array($form_state["values"]["select"]); 

    $output = ctools_modal_form_wrapper("ajax_madness_dependant_modal_form", $new_form_state);  
    if(!empty($new_form_state['ajax_commands'])) {
        $output = $new_form_state['ajax_commands'];
    }           
    print ajax_render($output);
    drupal_exit();
}

/**
 * The form that should be displayed in the modal
 */ 
function ajax_madness_dependant_modal_form($form,&$form_state,$option) {        
    $form["text"] = array(
        "#type" => "textfield",
        "#default_value" => t("Congratulations! You selected modal with option nr. @option",array("@option" => $option)),
    );  

    $form["#submit"][] = "ajax_madness_dependant_modal_form_submit";

    $form["submit"] = array(
        "#type" => "submit",
        "#value" => t("Close the modal & submit"),  
    );  
    return $form;
}

/**
 * Form submit function (never gets called)
 */
function ajax_madness_dependant_modal_form_submit($form,&$form_state) {
    dpm("Got called");
    ctools_include('ajax');
    ctools_include('modal');    
    $form_state['ajax_commands'][] = ctools_modal_command_dismiss();
    $form_state['ajax_commands'][] = ctools_ajax_command_reload();
    drupal_set_message(t("Congratulations! You submitted the MADNESS!"));
}

0 ответов

Другие вопросы по тегам