我想修改已存在的節點中的字段集合,以便可以更改3數組中第一個元素上的圖像。問題是,hostEntity信息不是以編程方式修改字段集合缺少hostEntity字段
$field_collection_item->save(true); // with or without the true
// OR
$fc_wrapper->save(true); // with or without the true
我收到以下錯誤:
Exception: Unable to save a field collection item without a valid reference to a host entity. in FieldCollectionItemEntity->save()
當我print_r的現場收集實體hostEntity:保護領域確實是空的,當我做一個entity_load或entity_load_single所以當我做了一套。我場採集的設置如下:
- field_home_experts
- 專家形象< ---想只改變這些數據,並保持低於
- field_expert_image
- 圖片其餘
- 專家名稱
- field_expert_name
- 文本
- 專家標題
- field_expert_title
- 文本
- 專家形象< ---想只改變這些數據,並保持低於
這裏是我想用它來修改現有節點場收集的代碼:
$node = getNode(1352); // Get the node I want to modify
// There can be up to 3 experts, and I want to modify the image of the first expert
$updateItem = $node->field_home_experts[LANGUAGE_NONE][0];
if ($updateItem) { // Updating
// Grab the field collection that currently exists in the 0 spot
$fc_item = reset(entity_load('field_collection_item', array($updateItem)));
// Wrap the field collection entity in the field API wrapper
$fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);
// Set the new image in place of the current
$fc_wrapper->field_expert_image->set((array)file_load(4316));
// Save the field collection
$fc_wrapper->save(true);
// Save the node with the new field collection (not sure this is needed)
node_save($node);
}
任何幫助將不勝感激,我還是很新的Drupal作爲一個整體(最終用戶或開發者)
它幫了我很多! –
我很感謝你的出色工作 –
也幫助了我,謝謝傑克! –