0

I'm creating nodes pragmatically but I don't know how to fill field_collection fields. Most of the examples i've seen have field_collections being updated from existing nodes and i'm not entirely certain they can also be used when creating new nodes.

function createNode($Obj){
  global $user;
  $node = entity_create('node', array(
    'type' => 'event',
    'uid' => 1,
    'status' => 1,
    'comment' => 0,
    'promote' => 0,
  ));
  $body = "<p>" . (string)$Obj->BuildingCode;
  $body .= " " . (string)$Obj->RoomNumber . "</p>";
  $body .= "<p>" . (string)$Obj->Description . "</p>";

  $entity = entity_metadata_wrapper('node', $node);
  $entity->language(LANGUAGE_NONE);
  $entity->title = (string)$Obj->ActivityName;
  $entity->body->set(array(
    'value' => $body,
    'summary' => (string)$Obj->Description,
    'format' => 'full_html',
  ));
  $entity->field_activityid = (string)$Obj->ActivityId;
  $entity->field_location_room = (string)$Obj->RoomNumber;
  $entity->field_location_building = (string)$Obj->BuildingCode;
  $entity->field_location_address->set(array(
    'country' => '',
    'thoroughfare' => '',
    'premise' => '',
    'locality' => '',
    'administrative_area' => '',
    'postal_code' => '',
  ));
  // $enitity->field_event_organizer->set(array(
  //   'field_event_presenter_name' => (string)$Obj->ContactFirstName,
  //   'field_event_presenter_title' => "TEST",
  //   'field_event_presenter_organizati' => "ORG",
  // ));

  $values = array(
    'field_name' => 'field_event_organizer',
    'field_event_presenter_name' => array(
      LANGUAGE_NONE => array(array('value' => (string)$Obj->ContactFirstName)),
    ),
    'field_event_presenter_title' => array(
      LANGUAGE_NONE => array(array('value' => "PTITLE")),
    ),
    'field_event_presenter_organizati' => array(
      LANGUAGE_NONE => array(array('value' => "PORGANI")),
    ),
  );

  $entity->save();
}

EDIT: main collection field: picture of main field_collection sub fields: picture of field_collection fields to be created

I tried to add a modified version the code below in hopes it would work for me:

$field_collection_item = entity_create('field_collection_item', array(
    'field_name' => 'field_event_organizer'
  ));
  $field_collection_item->setHostEntity('node', $node);
  $field_collection_item->field_event_organizer_name = "PROG NAME";
  $field_collection_item->save();

  $entity->field_event_organizer = $field_collection_item->item_id;

However i got an error EntityMetadataWrapperException: Unknown data property propertyInfo. in EntityStructureWrapper->getPropertyInfo() (line 354 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

EDIT: Re-tweaked the answer given to me by SN_26 and it manages to create the node with the field_collection filled in but I get the following error message:

The website encountered an unexpected error. Please try again later.
Status message Registration settings have been saved.
Error messageEntityMetadataWrapperException: There can be only numerical keyed items in a list. in EntityListWrapper->get() (line 1038 of /Applications/MAMP/htdocs/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

full updated code:

function createNode($Obj){
  global $user;
  var_dump($Obj);
  $node = entity_create('node', array(
    'type' => 'event',
    'uid' => 1,
    'status' => 1,
    'comment' => 0,
    'promote' => 0,
  ));
  $body = "<p>" . (string)$Obj->BuildingCode;
  $body .= " " . (string)$Obj->RoomNumber . "</p>";
  $body .= "<p>" . (string)$Obj->Description . "</p>";

  $entity = entity_metadata_wrapper('node', $node);
  $entity->language(LANGUAGE_NONE);
  $entity->title = (string)$Obj->ActivityName;
  $entity->body->set(array(
    'value' => $body,
    'summary' => (string)$Obj->Description,
    'format' => 'full_html',
  ));
  $entity->field_activityid = (string)$Obj->ActivityId;
  $entity->field_location_room = (string)$Obj->RoomNumber;
  $entity->field_location_building = (string)$Obj->BuildingCode;
  $entity->field_location_address->set(array(
    'country' => '',
    'thoroughfare' => '',
    'premise' => '',
    'locality' => '',
    'administrative_area' => '',
    'postal_code' => '',
  ));
  $entity->field_event_date[0]->set(array(
    'value' => _unix_date_time($Obj->StartDate, $Obj->StartMinute),//Y-m-d H:i:s
    'value2' => _unix_date_time($Obj->EndDate, $Obj->EndMinute),
  ));

  $field_collection_item = entity_create('field_collection_item', array(
    'field_name' => 'field_event_organizer'
  ));
  $field_collection_item->setHostEntity('node', $node);
  $field_collection_item->field_event_organizer_name[LANGUAGE_NONE][0]['value'] = "PROG NAME";
  $field_collection_item->save();

  $entity->field_event_organizer[LANGUAGE_NONE][0]['value'] = $field_collection_item->item_id;

  $entity->save();
}
0

This should do the trick to insert a new field collection item:

$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_some_collection'));
$field_collection_item->setHostEntity('node', $entity);
$field_collection_item->field_my_field_in_the_fc[LANGUAGE_NONE][0]['value'] = 'hello';
$field_collection_item->save();

$entity->field_some_collection[LANGUAGE_NONE][]['value'] = $field_collection_item->item_id;
  • I'm not exactly loading a node. I'm creating a node and filling the fields for a node type that was made using the Drupal UI. – blu Aug 18 '17 at 18:18
  • See above for the edited version. – SN_26 Aug 21 '17 at 13:19
  • Sorry for the delay. This didn't work. The last thing i posted semi worked, it can create a node with the field collection but it also crashes. The code you gave me gives me the following error: EntityMetadataWrapperException: There can be only numerical keyed items in a list. in EntityListWrapper->get() – blu Aug 21 '17 at 20:45
0

After much trial and error I found that this works by NOT assigning the $field_collection_item to the entity was the correct way to do this.

$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_event_organizer'));
$field_collection_item->setHostEntity('node', $node);
$field_collection_item->field_event_organizer_name[LANGUAGE_NONE][] = array('value' => $fullName);
$field_collection_item->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.