我想修改已存在的节点中的字段集合,以便可以更改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作为一个整体(最终用户或开发者)
它帮了我很多! –
我很感谢你的出色工作 –
也帮助了我,谢谢杰克! –