Drupal聚合器和定制输出

时间:2011-02-08 12:01:01

标签: php drupal drupal-6

我的页面上有两个Drupal聚合器块,一个用于Twitter提要,一个用于博客提要。这些都以略微不同的方式提供信息。

我希望博客Feed能够显示标题,然后将帖子的前80个字符显示为预告片。

我希望Twitter Feed能够显示描述,而不是其他内容。原因是标题是状态的完整链接,而描述将其放在标准文本中并链接其中包含的任何URL。

到目前为止,我有这个:

function mythemename_aggregator_block_item($item, $feed = 0) {

  // Target p, div and images and preg_replace them with nothing
  $tagstoreplace[0] = '/<p>/';
  $tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
  $tagstoreplace[2] = '/<img[^>]*>/';

  // Generate output
  $output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
  $output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
  return $output;
}

哪个适合我的博客Feed,非常棒。但是,它显然会将我的推特信息减少到80个字符。我已经尝试了140个字符的折衷,但是它是html和所有,<a href="..."></a>包含在strlen()中所以它不是真的可行,特别是如果我在那里有一些链接,因为它会使我的博客描述预告片太长。

所以,最后,我的问题是:我可以根据我想要提供给它的聚合器源来定制输出吗?

我已经考虑过在页面上获取容器div的ID,但看起来这是不行的。理想情况下,我最终会得到这样的代码:

function mythemename_aggregator_block_item($item, $feed = 0) {

// Target p, div and images and preg_replace them with nothing
$tagstoreplace[0] = '/<p>/';
$tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
$tagstoreplace[2] = '/<img[^>]*>/';

// Get feed type
$blog = //something;
$twitter = //something;

// Generate output
if($blog) {
  $output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
  $output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
}
if($twitter) { $output .= '<p class="feed-item-description">' . $item->description . '</p>'; }

return $output;

对于这个问题的长度感到抱歉,感谢您的坚持!

1 个答案:

答案 0 :(得分:0)

我已经想到了这一点。每个Feed都有$ item-&gt; fid的fid。我的代码现在写着:

    function mythemename_aggregator_block_item($item, $feed = 0) {

  // Target p, div and images and preg_replace them with nothing
  $tagstoreplace[0] = '/<p>/';
  $tagstoreplace[1] = '/<div(.*?)>(.*?)<\/div>/im';
  $tagstoreplace[2] = '/<img[^>]*>/';

  // Display the external link to the item.
  if($item->fid == '3') {
    $output = '<p class="feed-item-title"><a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a></p>\n";
    $output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
  }
  if($item->fid == '1') {
    $output .= '<p class="feed-item-description">' . preg_replace($tagstoreplace, '', $item->description) . '</p>';
  }
  return $output;
}