2013-07-18 100 views
0

我想修改已存在的节点中的字段集合,以便可以更改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
    1. 专家形象< ---想只改变这些数据,并保持低于
      • field_expert_image
      • 图片其余
    2. 专家名称
      • field_expert_name
      • 文本
    3. 专家标题
      • 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作为一个整体(最终用户或开发者)

回答

5

好了,所以我想我已经想通了这一点,我写了一个函数,将设置字段集合值:

// $node: (obj) node object returned from node_load() 
// $collection: (string) can be found in drupal admin interface: 
//    structure > field collections > field name 
// $fields: (array) see usage below 
// $index: (int) the index to the element you wish to edit   

function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) { 
    if ($node && $collection && !empty($fields)) { 
     // Get the field collection ID 
     $eid = $node->{$collection}[LANGUAGE_NONE][$index]['value']; 

     // Load the field collection with the ID from above 
     $entity = entity_load_single('field_collection_item', array($eid)); 

     // Wrap the loaded field collection which makes setting/getting much easier 
     $node_wrapper = entity_metadata_wrapper('field_collection_item', $entity); 

     // Loop through our fields and set the values 
     foreach ($fields as $field => $data) { 
      $node_wrapper->{$field}->set($data); 
     } 

     // Once we have added all the values we wish to change then we need to 
     // save. This will modify the node and does not require node_save() so 
     // at this point be sure it is all correct as this will save directly 
     // to a published node 
     $node_wrapper->save(true); 
    } 
} 

用法:

// id of the node you wish to modify 
$node = node_load(123); 

// Call our function with the node to modify, the field collection machine name 
// and an array setup as collection_field_name => value_you_want_to_set 
// collection_field_name can be found in the admin interface: 
// structure > field collections > manage fields 
updateFieldCollection(
    $node, 
    'field_home_experts', 
    array (
     'field_expert_image' => (array)file_load(582), // Loads up an existing image 
     'field_expert_name' => 'Some Guy', 
     'field_expert_title' => 'Some Title', 
    ) 
); 

希望这可以帮助其他人,因为我花了一整天试图让这个去工作(希望在Drupal7中我永远不会成为一个小菜鸟)。可能会有格式化文本正确设置()的问题,但我不确定此时是什么,所以请记住(如果您有一个字段的格式为filtered_html,例如,不确定正确设置而不做别的事情)。

祝你好运! Jake

+0

它帮了我很多! –

+0

我很感谢你的出色工作 –

+0

也帮助了我,谢谢杰克! –

0

在使用上述函数后,我仍然遇到了问题中提到的错误。 这是对我工作:

function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) { 
    $eid = $node->{$collection}[LANGUAGE_NONE][$index]['value']; 
    $fc_item = entity_load('field_collection_item', array($eid)); 
    foreach ($fields as $field => $data) { 
    $fc_item[$eid]->{$field}[LANGUAGE_NONE][0]['value'] = $data; 
    } 
    $fc_item[$eid]->save(TRUE); 
} 

我希望这可以帮助别人,因为我花了相当长的一段时间来得到这个工作。

+0

致命错误:调用未定义的方法stdClass :: save() – AlxVallejo