3

我正在使用提要模块将一组出版物拉入 Drupal 内容类型。它们被设置为使用 cron 定期运行。我有两个单独的提要,它们的工作方式如下:

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

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

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

饲料 1

饲料 2

如您所见,它们都具有相同的 GUID,但它们被映射到不同的节点。有没有办法让第二个提要映射到与第一个提要相同的节点?

4

1 回答 1

2

我拼凑了一些东西,使我的第二个提要能够从我的第一个提要更新节点。不确定这是否是正确的做事方式,但它确实有效。以下是我所做的,以防将来对其他人有所帮助:

  • 创建了一个扩展 FeedsNodeProcessor 的自定义处理器
  • 将 FeedsNodeProcessor 的所有函数复制到新类。
  • 按如下方式覆盖了 existingEntityId 函数(harvard_format 是我的辅助提要,pure_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 = '';

}

于 2012-12-11T16:55:40.173 回答