使用Ajax为节点创建自定义输入表单

时间:2014-03-24 11:59:53

标签: php ajax arrays database drupal

附件是我在Drupal 7中使用PHP和AJAX形式的示例屏幕截图。

默认情况下,表单将显示10个字段。该表单使用AJAX使用户能够将潜在无限数量的输入字段添加到5个组中的初始10个。我需要它做的是检索,重新填充保存的字段,具有相同的功能,应该用户希望编辑。

Example form

我之前尝试在entries_form_add_five中使用$ form_state来添加输入框的数量并使用ajax调用返回但是在加载要编辑的数据时我无法使用它。如何使用新的$ node - > entries_form [' term']数组重建表单,该数组已增加5?

<?php 

function entries_form_form_entries_node_form_alter(&$form, &$form_state, $form_id) {
//trimmed 
$node = $form['#node'];
$form["section"]["term"]["#tree"] = TRUE;

$items = $node->entries_form['term'];
foreach($items as $key => $item) {
    $form["section"]["term"][$key] = array(
        '#type' => 'textfield',
        '#size' => 10,
        '#attributes' => array(
        'class' => array('left'),
         ),
         '#value' => $item,
         ); 
 }  
//trimmed
}

function entries_form_commands_add_callback($form, $form_state) {
return $form['section']['term'];    
}

function entries_form_add_five($node, $form, &$form_state){
$node->entries_form['term'] = array_push($node->entries_form['term'],'', '', '', '', '');
$form_state['rebuild'] = TRUE;
}

function entries_form_node_prepare($node) {
if (empty($node->entries_form)) {
// Set default 10 empty values, since this only runs when adding a new node.
$node->entries_form['term'] = array_fill(0, 10, '');
}
}

function entries_form_node_load($nodes, $types) {
if($types[0] == 'entries'){
    $result = db_query('SELECT * FROM {entries_form_node_form_alter} WHERE nid IN(:nids)', array(':nids' => array_keys($nodes)))->fetchAllAssoc('nid');
    foreach ($nodes as &$node) {
        $node->entries_form['term'] = json_decode($result[$node->nid]->term);
    }
}

}

非常感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

我同意没有将先前保存的数据加载到无限值字段的好例子。 (我认为这应该在示例模块中。

我编写了以下代码来管理人员列表。这个列表存储在Drupal变量表中,而不是节点,但是方法应该类似,所以希望能引导你朝着正确的方向前进。

function people_list_form($form, &$form_state) {

    $form['#tree'] = TRUE;

    // load the list of names - here you could use node load instead
    $names = variable_get('people_list', array());

    if (empty($form_state['num_names'])) {
        // store the number of names we have in $form_state
        $form_state['num_names'] = count($names)>0 ? count($names) : 1;
    }

    $form['names_fieldset'] = array(
        '#title' => 'List of People',
        '#type' => 'fieldset',
        '#prefix' => '<div id="names-fieldset-wrapper">',
        '#suffix' => '</div>',
    );

    // loop for each name to add form elements
    for ($i = 1; $i <= $form_state['num_names']; $i++) {
        $form['names_fieldset']['name'][$i]['name'] = array(
            '#type' => 'textfield',
            '#title' => 'Name #'.$i,
            '#default_value' => isset($names[$i-1]) ? $names[$i-1] : '',
        );
    }

    $form['names_fieldset']['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Add another name'),
        '#submit' => array('people_list_form_add_name'),
        '#ajax' => array(
            'callback' => 'people_list_add_more_callback',
            'wrapper' => 'names-fieldset-wrapper',
        ),
    );

    if ($form_state['num_names'] > 1) {
        $form['names_fieldset']['remove_name'] = array(
            '#type' => 'submit',
            '#value' => t('Remove last name'),
            '#submit' => array('people_list_form_remove_name'),
            '#limit_validation_errors' => array(),
            '#ajax' => array(
                'callback' => 'people_list_add_more_callback',
                'wrapper' => 'names-fieldset-wrapper',
            ),
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );

    return $form;

}

function people_list_add_more_callback($form, $form_state) {
  return $form['names_fieldset'];
}

function people_list_form_add_name($form, &$form_state) {
    $form_state['num_names']++;
    $form_state['rebuild'] = TRUE;
}

function people_list_form_remove_name($form, &$form_state) {
    if ($form_state['num_names'] > 1) {
        $form_state['num_names']--;
    }
    $form_state['rebuild'] = TRUE;
}

function people_list_form_submit($form, &$form_state) {
    $names = array();
    foreach($form_state['values']['names_fieldset']['name'] as $k => $v) {
        $names[] = $v['name'];
    }
    variable_set('people_list', $names);
    drupal_set_message('Names updated');
}
相关问题