php - Creating custom input form for a node using Ajax -
attached example screenshot of form i'm using php , ajax in drupal 7.
by default, form display 10 fields. form uses ajax enable users add potentially infinite number of input fields initial 10 in groups of 5. need upon retrieval, repopulate fields saved, same functionality available, should user wish edit.
i have on previous attempt used $form_state in entries_form_add_five add number of input boxes , returned using ajax call couldn't work when loading data edit. how can rebuild form new $node-->entries_form['term'] array has been increased 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 $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 runs when adding 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 * {entries_form_node_form_alter} nid in(:nids)', array(':nids' => array_keys($nodes)))->fetchallassoc('nid'); foreach ($nodes &$node) { $node->entries_form['term'] = json_decode($result[$node->nid]->term); } } }
any appreciated.
thanks!
i agree there no examples of loading saved data unlimited values field. (i think should have been in examples module.
i wrote code below manage list of people. list stored in drupal variable table, not nodes, methodology should similar, hope should guide in right direction.
function people_list_form($form, &$form_state) { $form['#tree'] = true; // load list of names - here use node load instead $names = variable_get('people_list', array()); if (empty($form_state['num_names'])) { // store number of names 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 each name add form elements ($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 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'] $k => $v) { $names[] = $v['name']; } variable_set('people_list', $names); drupal_set_message('names updated'); }
Comments
Post a Comment