fieke
10/29/2013 - 7:47 AM

Change post date to time ago + date link to node

Change post date to time ago + date link to node

function theme_preprocess_node(&$variables, $hook) {
  // create default theme suggestion for node so
  // node--{node_type}--{view_mode}.tpl.php is available
  $variables['theme_hook_suggestions'][] = "node__" . $variables['type'] . "__" . $variables['view_mode'];


  switch ($variables['type']) {   
    case 'news':
      // ADD LINK TO NEWS DATA - HOMEPAGE
      if($variables['view_mode'] == 'blog_teaser' && isset($variables['content']['title']['#items']['0']['value'])) {

        // GET NODE TITLE, DATE AND NODE URL
        $orgdate = $variables['created'];
        $orgtitle = strip_tags($variables['content']['title']['#items']['0']['value']);
        $linktonode = $variables['node_url'];

        //CHANGE DATE TO TIME AGO 
        $interval = time() - $orgdate;
        $hour = 60*60;
        $day = 24*$hour;
        $time_ago = t("");
        if ($interval < $hour) {
          $time_ago .= t("a few moments ago");
        }
        elseif ($interval < $day) {
          $time_ago .= t("today");
        }
        elseif ($interval < 2*$day) {
          $time_ago .= t("yesterday");
        }
        else {
          $time_ago .= format_interval($interval, 1) . t(' ago');
        }


        // HIDE DATE
        hide($variables['content']['post_date']);

        // NEW TITLE + DATE VARIABLE
        $newtitle = '<div class="teasertitle">' . $orgtitle . '</div> <div class="teaserdate">'. $time_ago.'</div>';

        // ADD NODE URL TO DATE
        $variables['content']['title']['0']['#markup'] = '<a href="'. $linktonode .'" class="title-date">'. $newtitle .'</a>';
      }  
    break;
  }
}