0

I am using Drupal 7, and I have a content type called Case Record that has the following fields:

  • Title (title, Node module element)
  • Case Number (field_cr_case_number, Text)
  • Case Title (field_cr_case_title, Text)

When the user creates a new node of this type and enters in a Case Number, an AJAX call is supposed to fetch the Case Title and populate it on the Case Title textbox. Here is my module code:

<?php
/**
 * @file
 * Module for Case Records.
 *
 * @category Examples
 *
 * @package Example Modules
 */

/**
 * Implements hook_form_alter().
 */
function case_records_database_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'case_record_node_form':

      $form['field_cr_case_number'] = array(
        '#type' => 'textfield',
        '#title' => 'Case Number',
        '#prefix' => '<div id="div-case-number-element-wrapper">',
        '#suffix' => '</div>',
        '#ajax' => array(
          'callback' => 'case_records_database_ajax_callback',
          'event' => 'change',
          'method' => 'replace',
          'wrapper' => 'div-case-name-element-wrapper',
        ),
      );

      $cs_short_title = "";
      if (isset($form_state['values']['field_cr_case_number'])) {

        // Code omitted. Use db_select to query the database and
        // get the real case title, but for now we'll just call it
        // "Some Case Title".
        $cs_short_title = "Some Case Title";
      }

      $form['field_cr_case_title'] = array(
        '#type' => 'textfield',
        '#title' => t('Case Title'),
        '#value' => $cs_short_title,
        '#prefix' => '<div id="div-case-name-element-wrapper">',
        '#suffix' => '</div>',
      );

      break;
  }
}

/**
 * AJAX callback.
 */
function case_records_database_ajax_callback($form, &$form_state) {
  return $form['field_cr_case_title'];
}

The AJAX call does indeed populate the Case Title text field with the dummy "Some Case Title" text. However, when I save the new node neither the Case Number field nor the Case Title field are saved with any values. They are both blank. What is wrong with my code?

1

That code is overriding the fields structure. That fields have a specific structure and that code is changing it completely, so in submit the values are not recognized.

I recommend you use devel module and use dpm function to inspect the structure of that fields:

  dpm($form['field_cr_case_number']);
  dpm($form['field_cr_case_title']);

To make your form changes you must only add the #ajax property to your field:

$form['field_cr_case_number'][LANGUAGE_NONE][0]['value']['#ajax'] = array(
  'callback' => 'case_records_database_ajax_callback',
  'event' => 'change',
  'method' => 'replace',
  'wrapper' => 'div-case-name-element-wrapper',
);

And to add the wrapper and #value to your second field do something like this:

$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#prefix'] = '<div id="div-case-name-element-wrapper">';
$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#suffix'] = '</div>';
$form['field_cr_case_title'][LANGUAGE_NONE][0]['value']['#value'] = $cs_short_title;

In this question is also explained how to make the ajax callback.

  • The second line of the second field says "#prefix" when it should say "#suffix" but other than that, thank you very much that worked perfectly you're a genius. – MrSnrub Sep 29 '17 at 18:55
  • thx, I replace it in case other people want reference – mistermoper Sep 29 '17 at 19:30

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.