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

casaran’s picture

I don't know exactly which [0] is supposed to change, but what about trying something like this:


if(isset($form_state['values']['field_descrizione']['und'])) {
    $i = 0;
    foreach($form_state['values']['field_descrizione']['und'] as $element) {
         if(isset($form_state['values']['field_man_ordinaria']['und'][$i]['field_descrizione']['und'][0]['nid'])) {
              $nid = $form_state['values']['field_man_ordinaria']['und'][$i]['field_descrizione']['und'][0]['nid'];
         }
         $i++;
    }

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

lucadeluchis’s picture

Thank 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

casaran’s picture

Ok 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.


if($node) {

      if(isset($node->field_to_field_art['und'])) {
          $i = 0;
          foreach($node->field_to_field_art['und'] as $element) {
              if(isset($node->field_to_field_art['und'][$i]['value'])) {
                  $selector = '#edit-field-man-ordinaria-und-'.$i.'-field-art-und-0-value';
                  $commands[] = ajax_command_invoke($selector, 'val', array($node->field_to_field_art['und'][$i]['value']));
              }
          $i++;
          } 
       }    
} 

lucadeluchis’s picture

Thanks 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.

casaran’s picture

Can you post your javascript function and the css part of these fields please? Or perhaps for the css a link to your page.

lucadeluchis’s picture

Mh...is this part?

<input type="hidden" name="form_id" value="example_node_form" />
<div class="field-type-field-collection field-name-field-man-ordinaria field-widget-field-collection-embed form-wrapper" id="edit-field-man-ordinaria"><div id="field-man-ordinaria-add-more-wrapper"><div class="form-item"><table id="field-man-ordinaria-values" class="field-multiple-table sticky-enabled">
<thead><tr><th colspan="2" class="field-label"><label>man_ordinaria </label></th><th>Ordinamento</th> </tr></thead>
<tbody>
<tr class="draggable odd"><td class="field-multiple-drag"></td><td><div class="field-type-text field-name-field-art- field-widget-text-textfield form-wrapper" id="edit-field-man-ordinaria-und-0-field-art-"><div id="field-man-ordinaria-und-0-field-art-add-more-wrapper"><div class="form-item form-type-textfield form-item-field-man-ordinaria-und-0-field-art--und-0-value">
<label for="edit-field-man-ordinaria-und-0-field-art-und-0-value">art. </label>
<input class="text-full form-text" type="text" id="edit-field-man-ordinaria-und-0-field-art-und-0-value" name="field_man_ordinaria[und][0][field_art_][und][0][value]" value="" size="60" maxlength="255" />
</div>
</div></div><div class="field-type-node-reference field-name-field-descrizione field-widget-options-select form-wrapper" id="edit-field-man-ordinaria-und-0-field-descrizione"><div class = 'entityconnect-edit select single-value'><input type="submit" id="edit-field-man-ordinaria-und-0-field-descrizione-edit-entityconnect-field-descrizione-all-field-man-ordinaria-und-0" name="edit_entityconnect__field_descrizione_all_field_man_ordinaria-und-0" value="Edit content" class="form-submit" /></div><div class = 'entityconnect-add select single-value'><input type="submit" id="edit-field-man-ordinaria-und-0-field-descrizione-add-entityconnect-field-descrizione-all-field-man-ordinaria-und-0" name="add_entityconnect__field_descrizione_all_field_man_ordinaria-und-0" value="New content" class="form-submit" /></div><div class="form-item form-type-select form-item-field-man-ordinaria-und-0-field-descrizione-und">
<label for="edit-field-man-ordinaria-und-0-field-descrizione-und">Descrizione </label>
<select id="edit-field-man-ordinaria-und-0-field-descrizione-und" name="field_man_ordinaria[und][0][field_descrizione][und]" class="form-select"><option value="_none">- Nessuno -</option><option value="81"></option><option value="82"></option><option value="83"></option><option value="84"></option><option value="85"></option><option value="86"></option><option value="87"></option><option value="88"></option><option value="89"></option><option value="90"></option><option value="91"></option><option value="92"></option><option value="93"></option><option value="94"></option><option value="95"></option><option value="96"></option><option value="97"></option><option value="98"></option><option value="99"></option><option value="100"></option><option value="101"></option><option value="102"></option><option value="103"></option><option value="104"></option><option value="105"></option><option value="106"></option><option value="107"></option><option value="108"></option><option value="109"></option><option value="110"></option><option value="111"></option><option value="112"></option><option value="113"></option><option value="114"></option><option value="115"></option><option value="116"></option><option value="117"></option><option value="118"></option><option value="119"></option><option value="120"></option><option value="121"></option><option value="122"></option><option value="73">1</option><option value="129">1111</option><option value="127">a1</option><option value="130">alloraa</option><option value="125">asdasda</option><option value="126">azeta</option><option value="78">prova imput</option><option value="79">prova imput 2</option><option value="72">sadasd</option><option value="128">sito1</option><option value="80">z</option></select>
</div>
</div><input type="submit" id="edit-field-man-ordinaria-und-0-remove-button" name="field_man_ordinaria_und_0_remove_button" value="Elimina" class="form-submit" /></td><td class="delta-order"><div class="form-item form-type-select form-item-field-man-ordinaria-und-0--weight">
<label class="element-invisible" for="edit-field-man-ordinaria-und-0-weight">Peso per la riga 1 </label>
<select class="field_man_ordinaria-delta-order form-select" id="edit-field-man-ordinaria-und-0-weight" name="field_man_ordinaria[und][0][_weight]"><option value="0" selected="selected">0</option></select>
</div>
</td> </tr>
</tbody>
</table>
<div class="clearfix"><input class="field-add-more-submit form-submit" type="submit" id="edit-field-man-ordinaria-und-add-more" name="field_man_ordinaria_add_more" value="Aggiungi un altro elemento" /></div></div></div></div><h2 class="element-invisible">Schede Verticali</h2><div class="vertical-tabs-panes"><fieldset class="menu-link-form collapsible collapsed form-wrapper" id="edit-menu"><legend><span class="fieldset-legend">Impostazioni del menù</span></legend><div class="fieldset-wrapper"><div class="form-item form-type-checkbox form-item-menu-enabled">
<input type="checkbox" id="edit-menu-enabled" name="menu[enabled]" value="1" class="form-checkbox" /> <label class="option" for="edit-menu-enabled">Fornisce una voce del menu </label>

</div>

casaran’s picture

I 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.

lucadeluchis’s picture

Yes 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. :)


<?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_man_ordinaria']['und'][0]['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'])) {
          $i = 0;
          foreach($node->field_to_field_art['und'] as $element) {
              if(isset($node->field_to_field_art['und'][$i]['value'])) {
                  $selector = '#edit-field-man-ordinaria-und-'.$i.'-field-art-und-0-value';
                  $commands[] = ajax_command_invoke($selector, 'val', array($node->field_to_field_art['und'][$i]['value']));
              }
          $i++;
          }
       }   
} 
}

  return array('#type' => 'ajax', '#commands' => $commands);
} 

casaran’s picture

No, 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:


$("#edit-field-man-ordinaria-und-0-field-descrizione-und").change(function(){
  if ($(this).val() == '81') {
     $("input[name='field_man_ordinaria[und][0][field_art_][und][0][value]']").val("Your text corresponding to 81 here");
  }
  if ($(this).val() == '82') {
     $("input[name='field_man_ordinaria[und][0][field_art_][und][0][value]']").val("Your text corresponding to 82 here");
  }
});

etc.

If your list is not limited, then look at some ajax function to get the value from the database.

lucadeluchis’s picture

Thank you again casaran. You were very kind.

jmuzz’s picture

Priority: Major » Normal