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.
Hi I've got a problem with the field collection modules and an autofill module that I've made based on what I've found on other post.
The code is working in a single entry, but when I put unlimited entries I have problems
I think the problem is about the ['und'][0]
that for the second term become ['und'][1]
and so on...
I don't know how to fix it. Some ideas?
My code:
<?php
// $Id$
/**
* Implements hook_field_widget_form_alter().
*/
function MODULE_NAME_field_widget_form_alter(&$element, &$form_state, $context) {
if(isset($element['#field_name']) && $element['#field_name'] == 'field_descrizione') {
$element['#ajax'] = array(
'callback' => 'MODULE_NAME_auto_fill_callback',
);
}
}
function MODULE_NAME_auto_fill_callback(&$form, &$form_state) {
$commands = array();
if(isset($form_state['values']['field_descrizione'])) {
$nid = $form_state['values']['field_man_ordinaria']['und'][0]['field_descrizione']['und'][0]['nid'];
$node = node_load($nid);
if($node) {
if(isset($node->field_to_field_art['und'])) {
$commands[] = ajax_command_invoke('#edit-field-man-ordinaria-und-0-field-art-und-0-value', 'val', array($node->field_to_field_art['und'][0]['value']));
}
}
}
return array('#type' => 'ajax', '#commands' => $commands);
}
Comments
Comment #1
casaran CreditAttribution: casaran commentedI don't know exactly which [0] is supposed to change, but what about trying something like this:
well you get the idea, if the problem is as you believe and it's not the correct [0], just change to $form_state['values']['field_man_ordinaria']['und'][0]['field_descrizione']['und'][$i]['nid']
Hope it will help
Comment #2
lucadeluchis CreditAttribution: lucadeluchis commentedThank you but it's not working.
The part that change I think are these:
#edit-field-man-ordinaria-und-0-field-art-und-0-value
become#edit-field-man-ordinaria-und-1-field-art-und-0-value
#edit-field-man-ordinaria-und-2-field-art-und-0-value
#edit-field-man-ordinaria-und-0-field-art-und-0-value
become#edit-field-man-ordinaria-und-1-field-art-und-0-value
#edit-field-man-ordinaria-und-2-field-art-und-0-value
I think the problem is there
Comment #3
casaran CreditAttribution: casaran commentedOk let's try again then. $node->field_to_field_art['und'][0]['value'] corresponds to the first one of the unlimited entries in your field collection. So this has to be the variable part.
Comment #4
lucadeluchis CreditAttribution: lucadeluchis commentedThanks again for your kindness, I've tried and the first time I had an error, then I've tried to add a parenthesis } at the end of the code that you've suggested me and the error was gone but it's still working just for the first term, the other are not filling.
Comment #5
casaran CreditAttribution: casaran commentedCan you post your javascript function and the css part of these fields please? Or perhaps for the css a link to your page.
Comment #6
lucadeluchis CreditAttribution: lucadeluchis commentedMh...is this part?
Comment #7
casaran CreditAttribution: casaran commentedI guess you're trying to link your "descrizione" list field which corresponds to the field collection id to your "art" text entry, so that when something is selected in the list, the corresponding description would appear in the text field. What I don't understand is why my previous function would not work, especially when you are saying it is working for a single field.
Sorry I can't help more than this remotely. You will need someone much more expert about this than me.
Comment #8
lucadeluchis CreditAttribution: lucadeluchis commentedYes you're right!
I modify the code according on what you posted, did I make something wrong?
By the way thank you very much for helping. :)
Comment #9
casaran CreditAttribution: casaran commentedNo, I don't mean that you would have made a mistake in using the code. Of course if it doesn't work, it's because my code is wrong. That is why I said I could not help much more than this remotely. I am sorry I could not be of much help.
Anyone has any idea to help LucaDeLuchis?
*edit: after a bit of thinking, I understand that your first function was the closest to what you want to do and my code was not going in the right direction. However the problem is that to get the current selected value from your select list field, you would use javascript. The problem now is to link the javascript to some value into your database. If you have a limited number of possibilites (as it seems your list is limited), you could hard code everything within the javascript/jquery function. It sure is not a good solution but it would work. You would not need your code from before at all, just some javascript like this:
etc.
If your list is not limited, then look at some ajax function to get the value from the database.
Comment #10
lucadeluchis CreditAttribution: lucadeluchis commentedThank you again casaran. You were very kind.
Comment #11
jmuzz CreditAttribution: jmuzz commented