Download & Extend

remove button in 'add another' field for unlimited values doesn't work

Project:Field collection
Version:7.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:needs work

Issue Summary

I have a cck 'field collection' with unlimited values. There is an option to remove an entry by clicking a button 'remove' when you edit a node, but it doesn't work. Nothing happens when you click on it.

Comments

#1

Project:Content Construction Kit (CCK)» Field collection
Version:7.x-2.x-dev» 7.x-1.x-dev
Component:CCK in core» Code

Wrong project, moving.

#2

The same I experience. I have own ajax functionality in my form_alter function. Maybe it collides somehow with the field collection ajax call. I for my part get an answer from the ajax call but the changes aren't reflected in the browser. Tomorrow I will examine it further.

The problem seems to occur because of a html-id in my form, which doesn't correspond to the html id of the field collection. The module clears the id's cache in it's callback function for remove:

  // drupal_html_id() very helpfully ensures that all html IDS are unique
  // on a page. Unfortunately what it doesn't realize is that the IDs
  // we are generating are going to replace IDs that already exist, so
  // this actually works against us.
  if (isset($_POST['ajax_html_ids'])) {
    unset($_POST['ajax_html_ids']);
  }

The callback trys to replace the content of a html element with id edit-field-invitee but in the html of my form the element is attributet with the an id of edit-field-invitee--2.

#3

I am having the same issue, but its independent of the 'unlimited values' setting.

Let me know if you guys find the solution.

Thanks,
Prateek

#4

Status:active» needs work

I too have encountered this issue and have fixed it (at least for now). The solution is somewhat of a hack. I pull out the actual id from the POST 'ajax_html_ids' array and do the ajax commands using it. Like I say in my comment, I'm sure this could be done better. I am not that familiar with core D7 ajax stuff yet and I'm in a hurry. I'm using a bastardized version of Field Collection because I require Field Collection Table support, so a patch would be kind of useless. Here's my whole function field_collection_remove_js():

<?php
function field_collection_remove_js() {
 
// drupal_html_id() very helpfully ensures that all html IDS are unique
  // on a page. Unfortunately what it doesn't realize is that the IDs
  // we are generating are going to replace IDs that already exist, so
  // this actually works against us.
 
$ajax_html_ids = array();
  if (isset(
$_POST['ajax_html_ids'])) {
   
$ajax_html_ids = $_POST['ajax_html_ids'];
    unset(
$_POST['ajax_html_ids']);
  }

  list(
$form, $form_state) = ajax_get_form();
 
drupal_process_form($form['#form_id'], $form, $form_state);

 
// Get the information on what we're removing.
 
$button = $form_state['triggering_element'];
 
// Go two levels up in the form, to the whole widget.
 
$element = drupal_array_get_nested_value($form, array_slice($button['#array_parents'], 0, -3));
 
 
// Check 'ajax_html_ids' to make sure we are using the actual id. I'm sure this could be done more cleanly.
 
$actual_id = $element['#id'];
  foreach (
$ajax_html_ids as $key => $id) {
    if (
$id == $element['#id'] || strpos($id, $element['#id'] . '--') === 0) {
     
$actual_id = $id;
      break;
    }
  }
 
 
// Now send back the proper AJAX command to replace it.
 
$return = array(
   
'#type' => 'ajax',
   
'#commands' => array(
     
ajax_command_replace('#' . $actual_id, drupal_render($element))
    ),
  );

 
// Because we're doing this ourselves, messages aren't automatic. We have
  // to add them.
 
$messages = theme('status_messages');
  if (
$messages) {
   
$return['#commands'][] = ajax_command_prepend('#' . $actual_id, $messages);
  }

  return
$return;
}
?>
nobody click here