Hello all,

I've been searching high and low, backtracing field_collection.module, but I can't figure this one out. Hopefully someone can provide an insight.

I have a custom module which programmatically updates / creates field_collection_item entities. This is all working great, no problems here. The issue is that I recently have started to programmatically update a Boolean field inside the field collection. Try as I may, I can't seem to toggle the Boolean field to it's TRUE / 1 / '1' state.

I am programmatically saving my field_collection / field_collection_item using the approach outlined in #1106182: Creating field collection programatically.

Here's an example of what I'm doing:


// $data contains info returned from a web-service. The details are not very
// important. $data is of the form:
//
// $data = array(
//           0 => array(
//             'text'    => "Some text.",
//             'visible' => 1,
//           )
//           1 => array(
//             'text'    => "Different text.",
//             'visible' => 0,
//           )
//         );
//
// I have tried setting $data[0]['visible'] to TRUE, '1' and 1. All of these
// don't work.

$my_collection = $node->field_my_collection;
foreach ($data as $d_delta => $d_value) {
  if (!isset($my_collection[$node->language][$d_delta])) {
    $item = array(
      'field_name' => 'field_my_collection',
    );
    $entity = entity_create('field_collection_item', $item);
    $entity->setHostEntity('node', $node);
    $entity->save(TRUE);

    $my_collection[$node->language][$d_delta]['value'] = entity_id('field_collection_item', $entity);
  }

  $entity = entity_load_single('field_collection_item', $my_collection[$node->language][$d_delta]['value']);

  // field_text is a textarea field
  // field_visible is a single on/off checkbox
  $entity->field_text[$node->language][0]['value']    = $d_value['text'];
  // FIXME: It seems to be impossible to toggle field_visible to the
  //        'On' state.
  $entity->field_visible[$node->language][0]['value'] = $d_value['visible'];
  $entity->save(TRUE);
}

Am I doing something wrong here, or is this a bug?

Thanks in advance.

Comments

James Andres’s picture

I've discovered that it works to save Boolean fields when using $entity->save(); instead of $entity->save(TRUE);.

This smells like a bug to me but don't know the field_collection codebase well enough to feel confident I'm right. Could someone with more authority on field_collection take a look into this?

Edit: To be clear, when using $entity->save(TRUE); I am able to save all sorts of other field types (select lists, text fields and even custom fields). It's just Booleans that are being problematic.

James Andres’s picture

Category: support » bug

I have got around this now by using $entity->save(TRUE); and improving other areas of my code to gracefully handle the repeated calls to hook_node_update() as each field items are inserted/updated. I'll leave the ticket open as this still looks like a bug to me.