外部にRSSを提供するとなると、案件によっては要素などが指定フォーマットで決まっていたりする。
そこで指定通りにRSSを出力しようとするのだが、スキル不足で中途半端なカスタマイズな出力をする。
Viewsを使用してカスタマイズなRSSを出力する。
Views RSSでは普通の使い方だと要素名は固定しかない。
ViewsのStyle outputのtemplateを上書きし、適用する必要がある。
Style output元ソース
<?php // $Id: views-bonus-export-xml.tpl.php,v 1.2 2009/06/24 17:27:53 neclimdul Exp $ /** * @file views-view-table.tpl.php * Template to display a view as a table. * * - $title : The title of this group of rows. May be empty. * - $rows: An array of row items. Each row is an array of content * keyed by field ID. * - $header: an array of headers(labels) for fields. * - $themed_rows: a array of rows with themed fields. * @ingroup views_templates */ // Short tags act bad below in the html so we print it here. print '<?xml version="1.0" encoding="UTF-8" ?>'; ?> <xml> <?php foreach ($themed_rows as $count => $row): ?> <node> <?php foreach ($row as $field => $content): $label = $header[$field] ? $header[$field] : $field; ?> <<?php print $label; ?>><?php print $content; ?></<?php print $label; ?>> <?php endforeach; ?> </node> <?php endforeach; ?> </xml>
Style output元ソースから変更例
<?php // $Id: views-bonus-export-xml.tpl.php,v 1.2 2009/06/24 17:27:53 neclimdul Exp $ /** * @file views-view-table.tpl.php * Template to display a view as a table. * * - $title : The title of this group of rows. May be empty. * - $rows: An array of row items. Each row is an array of content * keyed by field ID. * - $header: an array of headers(labels) for fields. * - $themed_rows: a array of rows with themed fields. * @ingroup views_templates */ // Short tags act bad below in the html so we print it here. print '<?xml version="1.0" encoding="UTF-8" ?>'; ?> <rss version="2.0"> <channel> <title>テスト配信用</title> <link>http://a-liner.net/</link> <description>日記みたいなところ</description> <language>ja</language> <pubDate><?php print date('r');?></pubDate> <?php foreach ($themed_rows as $count => $row): ?> <item> <?php foreach ($row as $field => $content): $label = $header[$field] ? $header[$field] : $field; ?> <<?php print $label; ?>><?php print $content; ?></<?php print $label; ?>> <?php endforeach; ?> <?php var_dump($row); ?> //デバッグ用 <media:thumbnail url="<?php print $row[field_hogehoge_fid]; ?>" /> //表示したい箇所を指定 <content:encoded><?php print $row[body]; ?></content:encoded> //表示したい箇所を指定 </item> <?php endforeach; ?> </channel> </rss>
参考例としてスマートニュースのところが良いだろうか。
フォーマット確認ページもあるのでやってみるとよろしいかと・・・。
色々試してみたがViews Bonus Packが任意の要素名が作成出来て一番扱いやすかった。
任意のViewsから「Paged Feed 」を使用する。
これで出力すると、
imageが「画像」
linkが「リンク」
descriptionが「説明」
と要素が和訳で出力されてしまう現象が発生・・・。
ラベルをそれぞれ英語で入力して保存しても和訳されてしまう。
解決策は「インターフェースを翻訳」で登録されている日本語を英語に戻すこと。
admin/build/translate/search
から
内蔵インターフェイス image
内蔵インターフェイス link
内蔵インターフェイス description
を検索。
それぞれのリンクを見ると日本語が画像・リンク・説明となっているので、それぞれ「image・link・description」として保存する。
これで、和訳が消えてそのまま英語の要素名が出力される。
ちなみに任意の要素名はViews Custom FieldのGlobal: 独自のテキストで生成できる。
今のところ<![CDATA[]]>とかで出力してほしいとか言われたら、スキル不足でお手上げ。