2013-07-18 44 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