You are here

根据字段获取Nid并保存到entity reference字段中

g089h515r806 的头像
Submitted by g089h515r806 on 星期六, 2015-12-26 05:10

在实际的项目中,我们遇到这样的问题,讲座与讲座视频,我们处理成为了两个内容类型,讲座代码是唯一的,为了导入的方便,我们先导入了讲座,再导入了讲座视频,然后写了一段代码,根据讲座代码,来同步两个内容类型的关联。

这是我编写的PHP代码,用在VBO中:

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node');
  $query->entityCondition('bundle', 'lecture');
  $col = 'value'; //hardcoded at here.
  $query->fieldCondition('field_booking_code', $col, $entity->field_precode['und'][0]['value']);
  $result = $query->execute();
  $count = $query
    ->count()
    ->execute();
  if ($count) {
    $nids = array_keys($result['node']);
 $nid = reset($nids);
 $entity->field_lecture['und'][0]['target_id'] = $nid;
 node_save($entity);
  }

先用单个节点测试了一下,可以,然后批处理所有的节点。

论坛:

Drupal版本: