CocoaSpace

Drupalでウェブサイト構築

node.tpl.phpの$submittedに最終更新日時を追加

Oct 07 2009
Drupalバージョン: 
カテゴリ: 

DrupalコアのBookモジュールで作成したページで、Wikiのように一度作成したページを後日書き直したり、追記したりすることがあるかと思います。CocoaSpaceでも、一度作成したBook page(Drupalノート)を少しずつ加筆することが多いです。そんな時、オリジナルの投稿日時の他に最終更新日時を表示できたらいいなと思い、drupal.orgで方法を探していると、すぐに下のページを見つけました。

Adding "last updated" node informations to the $submitted variable in node.tpl.php (and variations)

Submitted Byモジュールでも同じようなことができるらしいのですが、たったこれだけの変更のためにモジュールをインストールするのも気が引けたので、今回はこちらの方法でやってみました。

2009年10月8日 追記: CocoaSpaceは現在、皆さんにユーザ登録していただいて記事を投稿していただくことはないので必要ないのですが、そういうコミュニティサイトを構築する場合に備えて最終更新日時に加えて、更新をしたユーザ名も表示できるようにしてみました。上記のリンクのコードを少々変更しただけです。使用するテーマのtemplate.phpファイルに下のコードを加えます。

/**
 * Adds last update date and editor to $submitted.
 */
function phptemplate_node_submitted($node) {
  $time_unit = 86400; // number of seconds in 1 day => 24 hours * 60 minutes * 60 seconds
  $threshold = 1;

  if ($node->changed && (round(($node->changed - $node->created) / $time_unit) > $threshold) || $node->uid != $node->revision_uid) { // difference between created and changed times > than threshold
    $user = user_load($node->revision_uid);
    return t('Originally submitted by !orig_name on @created. Last updated by !rev_name on @changed.',
      array(
      '!orig_name' => theme('username', $node),
      '@created' => format_date($node->created, 'small'),
      '!rev_name' => theme('username', $user),
      '@changed' => format_date($node->changed, 'small'),
    ));
  }
  else{
    return t('Submitted by !username on @datetime',
      array(
    '!username' => theme('username', $node),
    '@datetime' => format_date($node->created),
    ));
  }
}

コードが間違っていたら教えてください:-)

Originally submitted by !orig_name on @created. Last updated by !rev_name on @changed.の部分は、そのままですともちろん英語ですが、admin/build/translateから簡単に翻訳文を作成できます。

コメントを追加

Powered by Drupal + Linux