【问题标题】:update a field-collection from a computed field从计算域更新域集合
【发布时间】:2015-12-23 09:26:46
【问题描述】:

我可以使用以下代码检索附加到我的内容类型的字段集合的字段:

foreach ($entity->field_collection[LANGUAGE_NONE] as $line) {..}

或来自实体包装器。

但我绝对无法使用在计算字段中计算的值来更新集合字段,就像我通常对其他 CCK 字段所做的那样,例如:

$entity->field_regular[LANGUAGE_NONE][0]['value'] = $value ;

然后正常保存,就好像我'手动'编辑了 field_regular。

收集这将不起作用(什么都不可见):

$entity->field_collection[LANGUAGE_NONE][$key]['field_coll_field0'][LANGUAGE_NONE][0]['value'] = $value ;
// entity wrapper way
$coll = entity_load('field_collection_item', array($line['entity']->item_id));
$wcoll = entity_metadata_wrapper('field_collection_item', $coll[$key);
$wcoll->field_coll_field0->set($value) ;

任何 save() 方法都会给我一个空白页(无限 cgi 循环):

entity_save('field_collection_item',$coll);
wcoll->save();

以编程方式保存集合字段我应该知道什么? 谢谢,杰罗姆

【问题讨论】:

    标签: drupal-7 computed-field drupal-field-collection


    【解决方案1】:

    请查看Entity metadata wrappers doc 页面,其中包含如何更新字段集合的简单示例,例如:

    <?php
        // Populate the fields.
        $ewrapper = entity_metadata_wrapper('node', $node);
        $ewrapper->field_lead_contact_name->set($contact_name);
        $ewrapper->field_lead_contact_phone->set($contact_phone);
        $ewrapper->field_lead_contact_email->set($contact_email);  
    
        // Create the collection entity and set it's "host".
        $collection = entity_create('field_collection_item', array('field_name' => 'field_facilities_requested'));
        $collection->setHostEntity('node', $node);  
    
        // Now define the collection parameters.
        $cwrapper = entity_metadata_wrapper('field_collection_item', $collection);
        $cwrapper->field_facility->set(intval($offset));
        $cwrapper->save();  
    
        // Save.
        $ewrapper->save();
    ?>
    

    所以可能您需要执行以下操作:

    try {
      // Entity wrapper way.
      $coll = entity_load('field_collection_item', array($line['entity']->item_id));
      $wcoll = entity_metadata_wrapper('field_collection_item', $coll);
      $wcoll->field_coll_field0 = $value;
      $wcoll->save();
    } catch (Exception $e) {
      drupal_set_message(t('Error message: @error.',
            array('@error' => $e->getMessage())), 'error');
      watchdog_exception('MYMODULE', $e);
    }
    

    添加try/catch 应该可以防止出现空白页。但是,如果您仍然有问题,请检查看门狗日志或 PHP 日志文件中的任何错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多