Drupal 以编程方式修改缺少hostEntity字段的字段集合

Drupal 以编程方式修改缺少hostEntity字段的字段集合,drupal,drupal-7,drupal-field-collection,Drupal,Drupal 7,Drupal Field Collection,我试图修改已存在的节点中的字段集合,以便可以更改3个数组中第一个元素的图像。问题是,当我执行实体加载或实体加载时未设置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

我试图修改已存在的节点中的字段集合,以便可以更改3个数组中第一个元素的图像。问题是,当我执行实体加载或实体加载时未设置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()
打印字段集合实体时,hostEntity:protected字段确实为空。我的字段集合设置如下:

  • 现场专家
  • 专家图像好的,我想我已经解决了这个问题,我编写了一个函数,可以设置字段集合值:

    // $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

    好吧,我想我已经解决了这个问题,我编写了一个函数,可以设置字段集合值:

    // $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

    在使用上述函数后,我仍然得到问题中提到的错误。 这就是我的工作原理:

    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);
    }
    

    我希望这对其他人有所帮助,因为我花了相当长的时间才使其正常工作。

    在使用上述函数后,我仍然收到问题中提到的错误。 这就是我的工作原理:

    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);
    }
    

    我希望这能帮助一些人,因为我花了相当长的时间来完成这项工作。

    致命错误:调用未定义的方法stdClass::save()致命错误:调用未定义的方法stdClass::save()我感谢你出色的工作我感谢你出色的工作