Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #0.0
cdmo CreditAttribution: cdmo commentedleft a line out
Comment #1
marcingy CreditAttribution: marcingy commentedPlease provide steps on how to recreate.
Comment #2
stenjo CreditAttribution: stenjo commentedI'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:
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.
Comment #3
cdmo CreditAttribution: cdmo commentedI'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.
Comment #3.0
cdmo CreditAttribution: cdmo commentedi'd like to delete this post
Comment #3.1
cdmo CreditAttribution: cdmo commentedreverting back to previous state
Comment #4
stenjo CreditAttribution: stenjo commentedIn 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.
Comment #4.0
cdmo CreditAttribution: cdmo commentedfixed minor thing
Comment #4.1
cdmo CreditAttribution: cdmo commentedadding php tags for text coloring-goodness
Comment #5
cdmo CreditAttribution: cdmo commentedHere's the code I used for a workaround, hope this helps:
Comment #5.0
cdmo CreditAttribution: cdmo commentedoops