dsft8327 2011-02-08 12:01
浏览 41
已采纳

Drupal聚合器和定制输出

I have two Drupal aggregator blocks on my page, one for a Twitter feed, one for a blog feed. These both supply information in slightly different ways.

I would like the blog feed to display title, then the first 80 characters of the post as a teaser.

I would like the Twitter feed to display the description and nothing else. The reason for this is that the title is a whole link to the status whereas the description puts it in standard text and links any URLs included within it.

So far, I have this:

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>
";
  $output .= '<p class="feed-item-description">' . substr(preg_replace($tagstoreplace, '', $item->description),0,80) . '...</p>';
  return $output;
}

Which is great for my blog feed, perfect. However, it will obviously cut my twitter feed down to 80 chars. I've tried the compromise of 140 chars but with it being html and all, the <a href="..."></a> is included in the strlen() so it's not really workable, especially if I have a few links in there, as it would make my blog description teaser too long.

So, finally, my question is this: Can I tailor the output depending on which aggregator feed I want to supply it to?

I've thought about doing this by getting the ID of the container div on the page, but it looks like that's a no-go. Ideally, I'll end up with code like this:

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>
";
  $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;

Sorry about the length of this question and thanks for persevering!

  • 写回答

1条回答 默认 最新

  • doulu7174 2011-02-08 12:09
    关注

    I've figured this out. Each feed has a fid at $item->fid. My code now reads:

        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>
    ";
        $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错