3

On content type called Order have 2 fields - "Category name" (term reference) and "Date" (Date (Unix timestamp)). On node edit form I want to change date to other value (for test purpose it is "hard coded", see code below) on changing "Category name". Ajax fire on "Category name" and "Date" fieldset "fade" but date value still no change.

Code:

function my_module_form_alter(&$form, &$form_state, $form_id) {  
  if ($form_id == 'order_node_form') {
    // Date (Unix timestamp)
    $form['field_date']['#prefix'] = '<div id="field-date-replace">';
    $form['field_date']['#suffix'] = '</div>';

    // Taxonomy reference select list
    $form['field_category_name']['und']['#ajax'] = array(
      'callback' => 'category_name_callback',
      'wrapper' => 'field-date-replace',
      'method' => 'replace',
      'effect' => 'fade',
    );

    if (!empty($form_state['field_category_name']['und'][0]['tid'])) {
      $form['field_date']['und'][0]['#default_value']['value'] = strtotime('12/12/2013 00:00:00');
    }

    // Test - work if uncommented
    //$form['field_date']['und'][0]['#default_value']['value'] = strtotime('10/10/2013 00:00:00');
  }
}

function category_name_callback(&$form, &$form_state) {
  // don't work too
  //return $form['field_date'];

  return array(
    '#type' => 'ajax',
    '#commands' => array(
      ajax_command_replace("#field-date-replace", render($form['field_date'])),
    ),
  );
}
1

I found a solution but not why, if someone knows and says I will mark as the correct answer. Phone type behaves similarly to the decision to date and it is the same.

First of all this was hard to debug. For this I add in ajax callback the line

ajax_command_prepend("#field-date-replace", theme_status_messages(dpm($form_state)), 'status'),

And now all code

function my_module_form_alter(&$form, &$form_state, $form_id) {  
  if ($form_id == 'order_node_form') {
    // Date (Unix timestamp)
    $form['field_date']['#prefix'] = '<div id="field-date-replace">';
    $form['field_date']['#suffix'] = '</div>';

    // simple text line input
    $form['field_some_text']['#prefix'] = '<div id="field-some-text-replace">';
    $form['field_some_text']['#suffix'] = '</div>';

    // Phone number module field
    $form['field_phone']['#prefix'] = '<div id="field-phone-replace">';
    $form['field_phone']['#suffix'] = '</div>';

    // Taxonomy reference select list
    $form['field_category_name']['und']['#ajax'] = array(
      'callback' => 'category_name_callback_func',
      'wrapper' => 'field-some-text-replace',
      'method' => 'replace',
      'effect' => 'fade',
    );

    if (!empty($form_state['values']['field_category_name']['und'][0]['tid'])) {
      $form['field_some_text']['und'][0]['value']['#value'] = 'after change';
    }
    else {
      $form['field_some_text']['und'][0]['value']['#value'] = 'before change';
    }

  }
}

/**
 * AJAX callback for 'field_category_name' form 
 */
function category_name_callback_func($form, $form_state) {

  if (!empty($form_state['values']['field_category_name']['und'][0]['tid'])) {
    $form['field_phone']['und'][0]['number']['#value'] = '999999';
    $form['field_phone']['und'][0]['country_codes']['#value'] = 'gb';

    $form['field_date']['und'][0]['value']['date']['#value'] = '01/01/2014';
  }
  else {
    $form['field_phone']['und'][0]['number']['#value'] = '';
    $form['field_phone']['und'][0]['country_codes']['#value'] = '';
  }

  return array(
    '#type' => 'ajax',
    '#commands' => array(
      ajax_command_replace("#field-date-replace", render($form['field_date'])),
      ajax_command_replace("#field-some-text-replace", render($form['field_some_text'])),
      ajax_command_replace("#field-phone-replace", render($form['field_phone'])),

      // remove comment (//) on next line for debuging
      //ajax_command_prepend("#field-date-replace", theme_status_messages(dpm($form_state)), 'status'),
    ),
  );
}

The difference between all the examples I've seen, and this decision is that the values ​​change is made in "ajax callback" but not in hook_form_alter.

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.