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.
I tried adding the #ajax property to an autocomplete entity reference field on a Content Type creation form but I don't seem to be getting the callback when the field value is changed. I tried adding #ajax to some other simple fields on the same page and the callback worked, so it's specific to the entity reference field. (The reason I'm doing this is to catch and handle the situations where there are zero or > 1 matching references and put up a little form to allow the user to add the missing entity or choose between the multiple matching ones).
Comments
Comment #1
lasimon CreditAttribution: lasimon commentedComment #2
lasimon CreditAttribution: lasimon commentedSorry, I was right the first time: -rc1. I haven't tried -dev.
Comment #3
amitaibu> I haven't tried -dev
You should try -dev then
Comment #4
bjalford CreditAttribution: bjalford commentedI'm seeing the same thing using dev. Here's the code to add the ajax which works fine when pointing to another field but not the entity ref one.
Comment #5
Damien Tournoud CreditAttribution: Damien Tournoud commentedComment #6
Damien Tournoud CreditAttribution: Damien Tournoud commentedComment #7
danielfdsilva CreditAttribution: danielfdsilva commentedI'm having the same problem described by lasimon.
I tried on a clean install and the problem persists.
I created an entity reference field in a basic page and set the entity selection to another content type.
Then in a module i added:
The ajax doesn't work for the entity reference but it works for the another_select field.
Is this some kind of default behavior of entity reference?
Comment #8
danielfdsilva CreditAttribution: danielfdsilva commentedJust noticed that the language identifier was missing.
Stupid error but it took some time to realize.
The warning remains. Pay attentions to the language.
Comment #9
bjalford CreditAttribution: bjalford commentedCan you post the code with the fix?
Comment #10
bjalford CreditAttribution: bjalford commentedI tried adding $form['field_company']['und'][0]['value']['#ajax'] but no luck
I get the ajax spinner but the callback isnt fired
Comment #11
haydeniv CreditAttribution: haydeniv commentedbjalford try $form['field_company']['und']['#ajax']
or $form['field_company']['und'][0]['#ajax']
Depends on the name of the field. You can see it using the DPM module or firebug to inspect the text field.
Comment #12
bjalford CreditAttribution: bjalford commentedcouldnt get it to work. Could someone upload complete code for a field created through the UI?
Comment #13
vistree CreditAttribution: vistree commentedHi, I am using the current dev-version. And I am not able to make ajax-callbacks run ...
My entity reference uses a views-list to select an user.
After selecting the user, I want to fill a text field in the current content type with the phonenumber of the selected user. I use a custom modul to add the ajax callback, but it doesn't work.
My code:
Is this code correct? Is there still a problem with ajax in entityreference?
Is there a way to debug ajax??
Comment #14
haydeniv CreditAttribution: haydeniv commented@vistree
If you are using the Devel module you can put
at the top of your hook_form_alter() to see where to put the ['#ajax'] value.
Comment #15
vistree CreditAttribution: vistree commented@haydeniv
Thank you for your hint. But as I am no programmer, maybe you could help me a bit more ...
I enabled devel modul and inserted your code. Now I get lot of debug information. For each field in my content type, there is an debug entry now. How do I find the correct place for the ['#ajax'] now?
I didn't include the global entity-entry of this debug record.
Comment #16
haydeniv CreditAttribution: haydeniv commentedWhy are you using hook_field_widget_form_alter() instead of just hook_form_alter(). I don't think you would have much better luck implementing hook_form_alter() instead of the field_widget_form.
Comment #17
vistree CreditAttribution: vistree commentedHm, maybe I am wrong, but I want to fire the ajax directly after the value of the selectfield has been changed - inside the edit mode - without saving. I thought, using hook_form_alter() I need to save the form or to put an button in the form??
Comment #18
haydeniv CreditAttribution: haydeniv commentedYou are not implementing hook_field_widget_form_alter() correctly anyways
http://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_widget_form/7
It should be something more like this:
Where are you getting your example code from?
Comment #19
vistree CreditAttribution: vistree commentedHi haydeniv,
I got the example from this post:
http://drupal.stackexchange.com/questions/70452/auto-fill-from-on-conten...
Hoped, that it works, as there was no negativ feedback ....
Comment #20
haydeniv CreditAttribution: haydeniv commentedYou may want to post this question at http://drupal.stackexchange.com because your question is not really related to an entity reference field other than it happens to contain one. Every time we post here all of the people in this thread get notified.
What you are trying to accomplish is more like this question: http://drupal.stackexchange.com/questions/83909/autofilling-fields-based...
I can't think of any modules that will do what you want to do out of the box so a custom module is going to be your best bet. Getting into AJAX is pretty complicated stuff for a new programmer though. Putting this back to works as designed as the initial problem was taken care of.