Drupal - 2个单独的feed更新同一组节点(feed模块)

时间:2012-12-10 12:11:47

标签: drupal drupal-7 drupal-feeds

我正在使用feeds模块将一组发布引入Drupal内容类型。它们设置为使用cron定期运行。我有两个独立的Feed,应该按如下方式工作:

  • Feed 1(pure_feed) - 拉入大部分字段
  • Feed 2(harvard_format) - 访问单独的网址源并更新内容类型上的一个字段。

我遇到的问题是,Feed 2总是会创建一组新节点,而不是更新现有节点(使用Feed 1创建)。我在/ import中使用了调试选项,可以看到feed 2的GUID与feed 1的GUID匹配,但它仍然创建了2组节点,而不是更新1组节点。

以下是来自feeds_items数据库表的摘录:

Feed 1

Feed 2

正如您所看到的,它们都具有相同的GUID,但它们被映射到单独的节点。有没有办法让第二个Feed映射到与第一个Feed相同的节点?

1 个答案:

答案 0 :(得分:2)

我把一些东西拼在一起,允许我的第二个Feed从我的第一个Feed中更新节点。不确定这是否是正确的做事方式,但它有效。以下是我在未来帮助其他人时所做的事情:

  • 创建了一个扩展FeedsNodeProcessor
  • 的自定义处理器
  • 将所有FeedsNodeProcessor的函数复制到新类。
  • 覆盖现有的EntityId函数,如下所示(harvard_format是我的辅助Feed​​,pure_feed是我的主要Feed):

受保护的函数existingEntityId(FeedsSource $ source,FeedsParserResult $ result){

if($source->id == 'harvard_format') {

  $query = db_select('feeds_item')
    ->fields('feeds_item', array('entity_id'))
    ->condition('feed_nid', $source->feed_nid)
    ->condition('entity_type', $this->entityType())
    ->condition('id', 'pure_feed');

  // Iterate through all unique targets and test whether they do already
  // exist in the database.
  foreach ($this->uniqueTargets($source, $result) as $target => $value) {
    switch ($target) {
      case 'url':
        $entity_id = $query->condition('url', $value)->execute()->fetchField();
        break;
      case 'guid':
        $entity_id = $query->condition('guid', $value)->execute()->fetchField();
        break;
    }
    if (isset($entity_id)) {
      // Return with the content id found.
      return $entity_id;
    }
  }
  return 0;
}
elseif ($nid = parent::existingEntityId($source, $result)) {
  return $nid;
} else {

  // Iterate through all unique targets and test whether they do already
  // exist in the database.
  foreach ($this->uniqueTargets($source, $result) as $target => $value) {
    switch ($target) {
      case 'nid':
        $nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(':nid' => $value))->fetchField();
        break;
      case 'title':
        $nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(':title' => $value, ':type' => $this->config['content_type']))->fetchField();
        break;
      case 'feeds_source':
        if ($id = feeds_get_importer_id($this->config['content_type'])) {
          $nid = db_query("SELECT fs.feed_nid FROM {node} n JOIN {feeds_source} fs ON n.nid = fs.feed_nid WHERE fs.id = :id AND fs.source = :source", array(':id' => $id, ':source' => $value))->fetchField();
        }
        break;
    }
    if ($nid) {
      // Return with the first nid found.
      return $nid;
    }
  }
  return 0;
}

}

  • 从FeedsProcessor复制Process和newItemInfo函数。
  • 覆盖newItemInfo,如下所示:

受保护的函数newItemInfo($ entity,$ feed_nid,$ hash =''){

$entity->feeds_item = new stdClass();
$entity->feeds_item->entity_id = 0;
$entity->feeds_item->entity_type = $this->entityType();
// Specify the feed id, otherwise pure_feed's entries in the feeds_item table will be changed to harvard_format
$entity->feeds_item->id = ($this->id == "harvard_format") ? "pure_feed" : $this->id; 
$entity->feeds_item->feed_nid = $feed_nid;
$entity->feeds_item->imported = REQUEST_TIME;
$entity->feeds_item->hash = $hash;
$entity->feeds_item->url = '';
$entity->feeds_item->guid = '';

}