Question

i've been having a nightmare trying to understand the Drupal way! I have an example here and if someone could show me the correct way to achieve my result I'm hoping it will help me open the door to understanding.

So I'm trying to alter the Node creation Date to appear similar to Stack Overflows format. E.G. This is either "Posted x hours ago" or "Posted on 17th Aug at 12:22" etc.

I have managed to create the desiered effect using the "node.tpl.php" file. I've achieved this using the node variable "$created" and the following code.

    $showCreate = round((time() - $created) / 60);
if ($showCreate < 60) {
    $showCreate = $showCreate . "mins ago." ;
} else {
    $showCreate = round($showCreate / 60);
    if ($showCreate > 24) {     
        $createMonth  = format_date($created, 'custom', 'M');
        $createDate   = format_date($created, 'custom', 'd');   
        $createTime   = format_date($created, 'custom', 'H:i');
        switch ($createDate) {
            case 1:
            case 21:
            case 31:
                $createDate = $createDate . "st";
                break;
            case 2:
            case 22:
                $createDate = $createDate . "nd";
                break;
            case 3:
            case 23:
                $createDate = $createDate . "rd";
                break;
            default:
                $createDate = $createDate . "th";
                break;
        }
        $showCreate = $createMonth . " " . $createDate . " at " . $createTime;
    } else {
        $showCreate = $showCreate . "hrs ago." ;    
    }
}

Is this a 'correct' way to achieve this? or would you use some of the hooks and preprocess functions? I would prefer to wrap this up into a MOdule so I can apply it directly to my other drupal sites.

Thanks a lot in advance.

Was it helpful?

Solution

There are a fair few different ways to do this in Drupal 7, I guess it depends on what you're doing.

If you're writing a module then you want to use hook_node_view to alter the rendered content.

This is some example code from the page above:

function hook_node_view($node, $view_mode, $langcode) {
  $node->content['my_additional_field'] = array(
    '#markup' => $additional_field, 
    '#weight' => 10, 
    '#theme' => 'mymodule_my_additional_field',
  );
}

If you're writing a theme then you want to use hook_preprocess_node in your theme's template.php file, something like this:

function mytheme_preprocess_node(&$vars) {
  $node = $vars['node'];

  $vars['my_created_date'] = my_date_extraction_function($node->created);
}

Then in your node.tpl.php you'll have the variable $my_created_date available, which you can use in place of the original created date.

Obviously you can use your imagination here, anything you add to the $vars array will be available in node.tpl.php and you have full access to the node object in mytheme_preprocess_node().

If you haven't used hook_node_view or the render API before you might find it easier to use the preprocess_node method as there's less of a learning curve.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top