Hello I need to insert a formated date in a block template: block--delta-blocks--page-title.tpl.php

As I think this can be done using a preprocess function to set the $day, $month, $year variables:

function cybele_preprocess_block(&$variables) {
    $variables['day'] = format_date($node->created, 'custom', 'd' );

But it seems $node->created is not visible here. It works if I place it in:

function mytheme_preprocess_node(&$vars) {
    $node = $vars['node'];
    $vars['year'] = format_date($node->created, 'custom', 'Y' );
    $vars['month'] = format_date($node->created, 'custom', 'm' );
    $vars['day'] = format_date($node->created, 'custom', 'd' );


but not in _preprocess_block function. Can someone help me do that? Probably I need to declare in mytheme_preprocess_page but the block template won't see from there either.

1 Answer 1


The important difference between the two code examples you're giving, is $node = $vars['node'];. You can only use the variables that are available inside the function you're writing. If you declare a function cybele_preprocess_block(&$variables), then the only variable you start with is $variables. In other words: inside that function, $node doesn't exist yet!

Sometimes, the current node is stored in the $variables array, so if it's in there, we can take it out again. That's what the line $node = $vars['node']; does: it creates a new variable $node and populates it with the value of $variables['node']. You do not have to put the node in a separate variable; you can also do this:

function mytheme_preprocess_node(&$variables) {
  $variables['day'] = format_date($variables['node']->created, 'custom', 'd' );

If the node is not available inside $variables (which I think is the case in _preprocess_block) you will have to load the current node. Fortunately that's rather easy in Drupal 7, because you can use menu_get_object. This will only work when viewing a node page.

function mytheme_preprocess_block(&$variables) {
  $node = menu_get_object();
  $variables['day'] = format_date($node->created, 'custom', 'd' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.