Ajax在drupal 7.x中的自定义主题中不起作用

时间:2012-05-29 22:08:00

标签: jquery drupal-7

<?php
/** hook_menu **/
function example_contact_form_menu() {
    return array(
    'contact_form_test' => array(
      'page callback' => 'contact_form_test',
      'page arguments' => array(),
      'access arguments' => TRUE,
      'type' => MENU_CALLBACK,
    ),
    'contact_form_test_1' => array(
      'page callback' => 'drupal_get_form',
      'page arguments' => examplexample_contact_form_form'),
      'access arguments' => TRUE,
      'type' => MENU_CALLBACK,
    )
    );
}
function contact_form_test()
{
    drupal_add_css(drupal_get_path('module', 'example_contact_form') . '/example_contact_form.css');
    dpm(drupal_get_form('example_contact_form_form'));
    return render(drupal_get_form('example_contact_form_form'));
}
/**
 * Implementation of hook_form().
 */
function example_contact_form_form($form, &$form_state){
    $form=drupal_build_form('example_form_form',$form_state);
    $form['first_name']['#size']=25;
    $form['last_name']['#size']=25;
    unset($form['submit_form']);
     //adding new fields
     $form['box'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="box">',
    '#suffix' => '</div>',
    '#markup' => '<h1>Initial markup for box</h1>',
  );
      $form['comment'] = array(
    '#title'=> t('Comments'),
    '#type' => 'textarea',
     '#value'=>'Type Here',
    '#description' => t(''),
    '#default_value' => '',
    '#attributes'=>array(
             'onfocus'=> "if(this.value=='Type Here')this.value='';"
            )
    );
    $form['questions'] = array(
    '#type' => 'select', 
    '#title' => t('Whats in your mind'), 
    '#default_value' => array(), 
    '#options' => array(
      'What\'s in your mind' => 'What\'s in your mind', 
      'Question 1' => 'Question 1', 
    ), 
  );
     $form['sub_form'] = array(
    '#type' => 'button',
    '#ajax' => array(
      'callback' => 'example_contact_form_js',
      'wrapper' => 'box',
      'name' => 'submit1',
    ),
    '#value' => t('Submit'),
  );
     unset($form['#theme']);
     $form['#theme'] = array('example_form_form', 'contact_theme_form');
     $form['#validate'] = array('example_contact_form_form_validate');
     $form['#submit'] = array('example_contact_form_form_submit');
     return $form;
}

function example_contact_form_form_validate($form, &$form_state){
    dpm($form_state,t('Form State Validate'));
}


function example_contact_form_js($form, &$form_state){
      $element = $form['box'];
      $element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " . date('c');
      return $element;
}
function advanced_form_callback($form, &$form_state) {
  $city_name = form_result_helper($form_state);
  $element = $form['status'];
  $element['#markup'] = t('You submitted @city', array('@city' => $city_name));
  print 'BHASKAR';
}

function example_contact_form_form_submit($form, &$form_state){
    dpm($form_state,t('Form State Submit'));
}

// Menu callback
function example_contact_form_theme(){
    return array
    (
        'contact_theme_form' => array
        (
                'render element' => 'form',
                'template' => 'contact-signup',
                'path' => drupal_get_path('module', 'example_contact_form').'/templates',
                'arguments' => array('form' => null),
        ),
    );
}

function template_preprocess_contact_theme_form(&$variables) {
  $form=$variables['form'];
  foreach (element_children($form) as $key) {
              unset($form[$key]['#title']);
              $variables[$key]=render(($form[$key]));
              unset($form[$key]);    
 }
$variables['contact']=drupal_render_children($form);
}
?>

问题是当我试图渲染template_preprocess_contact_theme_form中的每个元素时,我删除了这个函数,一切似乎都在起作用。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您的<form>标签出现了吗? 在表单主题中,您可能需要呈现表单子项,然后使用theme_form()

$buildform = '&lt;div&gt;'.drupal_render($form['elem1']).'&lt;/div&gt;';
$buildform .= drupal_render($form['elem2']);
$buildform .= drupal_render($form['form_build_id']);
$buildform .= drupal_render($form['form_id']);
$form['element']['#children'] = $buildform;
$output = theme_form($form);

答案 1 :(得分:0)

解决它。我的template_preprocess_contact_theme_form我删除了form_build_id,form_id这是错误的,而不是隐藏,我正在删除值。