This has already been documented here:
http://drupal.org/node/1189240

If you set an Ajax array on the title field through hook_form_alter, it does not run. I, personally, had this in my template.php:

<?php
$form['title']['#ajax']['callback'] = 'my_theme_form_content_node_callback';
$form['title']['#ajax']['wrapper'] = 'ajax-load';
?>

I had the html set in the suffix of the title (it did render the empty div):

<?php
$form['title']['#suffix'] = '<div id="ajax-load"></div>';
?>

I had a callback function (simple example, I tried other commands too):

<?php
function my_theme_form_content_node_callback($form, $form_state) {
$commands = array();
$commands[] = ajax_command_alert(t('Alert'));
return array('#type' => 'ajax', '#commands' => $commands);
}
?>

I also tried removing the wrapper from the ajax element on the title and instead specifying it in the callback, and that didn't work either. Same code worked find for other textfields. Is this a bug?

Comments

cdmo’s picture

Issue summary: View changes

left a line out

marcingy’s picture

Status: Active » Postponed (maintainer needs more info)

Please provide steps on how to recreate.

stenjo’s picture

Status: Postponed (maintainer needs more info) » Active
Issue tags: +ajax error

I'll try and add some more information to this issue as I am experiencing the same thing myself.

I'm trying to modify the node add form by adding some AJAX functionality to the title field and will then have something like this implemented:

function mymodule_form_node_form_alter(&$form, &$form_state) {

  $form['title']['#ajax'] = array(
     'callback' => '_callback_get_info',
     'event' => 'change',
     'wrapper' => 'custom-div-id',
   );

  $form['test_div'] = array(
     '#title' => t("Area for update"),
     '#prefix' => '<div id="custom-div-id">',
     '#suffix' => '</div>',
     '#type' => 'fieldset',
     '#description' => t('This is where we get automatically some text when title field is changed'),

  );

}

function _callback_get_info($form, $form_state) {
  return 'Some dummy values for test';
}

The callback function above seems never to be fired, even though, checking the node form array the #ajax part seems to be added properly.

See also #ajax['callback'] not called on title field.

cdmo’s picture

I'm sorry, I actually wanted to delete this post but couldn't figure out how. If you look in the revisions of my post there's code details. Basically I discovered that you can't seem to set an ajax property on a title field in hook_form_alter. I had to create a second title field, display that to the user, hide the original title field, set ajax on the second title field, do the ajax I need to do (a little lookup to compare a node's title to existing node titles) and then populate the title['value'] (the actual title, that's hidden to users) with the second title field's value. Which seems crazy.

I wanted to delete it because I wasn't sure it was a bug.

cdmo’s picture

Issue summary: View changes

i'd like to delete this post

cdmo’s picture

Issue summary: View changes

reverting back to previous state

stenjo’s picture

In my opinion this is a bug. As far as I can understand there should not be a reason why the AJAX stuff should not work for the title field too.
Would be good if you could post the workaround code and share that with us too.

cdmo’s picture

Issue summary: View changes

fixed minor thing

cdmo’s picture

Issue summary: View changes

adding php tags for text coloring-goodness

cdmo’s picture

Here's the code I used for a workaround, hope this helps:

<?php
function my_theme_form_my_form_id_form_alter(&$form, &$form_state, $form_id) {
// The secondary title field.
$form['title_twin'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#size' => 60,
  '#maxlength' => 128,
  '#required' => TRUE,
  '#ajax' => array(
      'callback' => 'my_theme_form_my_form_id_callback',
      'wrapper' => 'possible-names',
      'effect' => 'fade',
    ),
  );

// A container for the list of possible nodes you might be erroneously duplicating
  $form['container'] = array(
    '#prefix' => '<div id="possible-names">',
    '#suffix' => '</div>',
  );

// After the form_state changes and the Ajax has been called
  if (!empty($form_state['values']['title_twin'])){
    $form['title']['#value'] = $form_state['values']['title_twin'];
  }
}
// Callback function...
function my_theme_form_my_form_id_callback($form, $form_state) {

  $title = $form_state['values']['title_twin'];

// query the db to find similar node titles...
  $result = db_query('SELECT nid, title FROM {node} WHERE title LIKE :title', array(':title' => '%'.$title.'%',));
  $list = array();
  foreach ($result as $record) {
    $list[] = t('<a href=\"node/@nodeid\"">@nodetitle</a>', array(
      '@nodeid' => $record->nid,
      '@nodetitle' => $record->title,
      ));
  }
  $query_output_string = implode(", ", $list);

// creating the form element to return...
  $form['container'] = array(
    '#prefix' => '<div id="possible-names">',
    '#suffix' => '</div>',
    '#markup' => 'Similar race(s): '. $query_output_string,
  );

// returning the list back to the form
return $form['container'];
}
?>
cdmo’s picture

Issue summary: View changes

oops

Version: 7.17 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.