design for simplicity

clients
resources

Gold for Drupal themers: get_defined_vars()

200718Sep

I remember when I first started working with Drupal, one of the biggest challenges when theming was discovering what variables were available to me.

Perhaps I didn't like the formatting of the date as supplied by the default $submitted variable and wanted to see if there was a simple timestamp I could format...?

Luckily I came to learn of the get_defined_vars() PHP function which returns an array of all defined variables within the current scope.

Drop this into the relevant theme file (in the case of the example problem above, this would be node.tpl.php) and wrap it within a print_r to print the array, and nestle that within a pre tag, and you're set:

<?php
print '<pre>';
print_r(get_defined_vars());
print
'</pre>';
?>

This will return something like:

Array
(
    [file] => sites/all/themes/everybody/node.tpl.php
    [variables] => Array
        (
            [nid] => 5
            [vid] => 5
            [type] => page

[-snipped away a bunch of stuff for brevity-]

        )

    [nid] => 5
    [vid] => 5
    [type] => page
    [status] => 1
    [created] => 1184215138
    [changed] => 1184215138
    [comment] => 0
    [promote] => 0
    [sticky] => 0
    [revision_timestamp] => 1184215138
    [title] => Publicity Materials
    [body] =>

Body contents would be here...

    [log] =>
    [format] => 1
    [uid] => 10
    [name] => mistlethwaite
    [picture] =>
    [data] => a:0:{}
    [last_comment_timestamp] => 1184215138
    [last_comment_name] =>
    [comment_count] => 0
    [taxonomy] => Array
        (
        )

    [files] => Array
        (
        )

    [readmore] =>
    [content] =>

Body contents would be here...

    [links] =>

    * 5 reads


    [date] => Wed, 07/11/2007 - 10:38pm
    [node] => stdClass Object
        (
            [nid] => 5
            [vid] => 5
            [type] => page
            [status] => 1
            [created] => 1184215138
            [changed] => 1184215138
            [comment] => 0
            [promote] => 0
            [sticky] => 0
            [revision_timestamp] => 1184215138
            [title] => Publicity Materials
            [body] =>

Body contents would be here...

            [log] =>
            [format] => 1
            [uid] => 10
            [name] => mistlethwaite
            [picture] =>
            [data] => a:0:{}
            [last_comment_timestamp] => 1184215138
            [last_comment_name] =>
            [comment_count] => 0
            [taxonomy] => Array
                (
                )

            [files] => Array
                (
                )

            [readmore] =>
            [content] => Array
                (
                    [body] => Array
                        (
                            [#weight] => 0
                            [#value] =>

Body contents would be here...

                            [#printed] => 1
                        )

                    [#children] =>

Body contents would be here...

                    [#printed] => 1
                )

            [links] => Array
                (
                    [statistics_counter] => Array
                        (
                            [title] => 5 reads
                        )

                )

        )

    [node_url] => /node/5
    [page] => 1
    [teaser] =>
    [terms] =>
    [submitted] =>
    [zebra] => odd
    [id] => 1
    [directory] => sites/all/themes/everybody
    [is_front] =>
)

Whoa, that's a lot of stuff! I even cut some of it out. (By the way, I'm not certain if all of it has been run through Drupal's XSS filters, so be wary about what you print out to the page for your users. Anyone with the lowdown on this, I'd love to have your comments below.)

Using the above, we can determine that $node->created is the object variable for which we're looking. The $date variable is a formatted date, but the $node->created (look for the [node] => stdClass Object at the top level indent, and then the [created] attribute indented under that, and write it as $node->created (alternately, if this had been an array ala [node] => Array, then $node['created'] would be the format)) is a timestamp which we can use to display the date however we want using PHP's date() function.

If you wanted a faux wall calendar for your submitted by date, like on this site, do the following:

Add wherever you want it, into node.tpl.php:

<div class="wallcalendar"><span class="yr"><?php print date('Y', $node->created) ?></span><span class="day"><?php print date('j', $node->created) ?></span><span class="mo"><?php print date('M', $node->created) ?></span></div>

Add to your theme's stylesheet:

div.wallcalendar{width:55px;text-align:center;border:1px solid #AC8589;float:right;margin:0em 1em}
div.wallcalendar span.day{display:block;width:55px;background:#EFD4D5;font-size:1.6em}
div.wallcalendar span.mo{display:block;width:55px;background:#EFD4D5;font-size:1em}
div.wallcalendar span.yr{display:block;width:55px;background:#6C2229;color:#D8BDDA}

And of course you could get really fancy with it if you wanted, and set background images in the CSS to create a more detailed calendar look - maybe the corner of page coming up...

tags

Thanks!

I was hoping to find a quick and easy way to create this, and your code works beautifully. Many thanks!

Great!

Glad to have helped out!

Great code!

Great snippet! The only question is: why we can't set the value of variables like "$submitted" easily from the admin interface? Peace.

thx

hi mlsamuelson,
thx for code sniped and tutorial.

PS: your dropdown menu with java ist great.

thanks

Thank you very much very nice article
Great information! Very useful for me. Thanks a lot.
The idea is awesome. Congrats.

var_export()

In place of print_r() in the above you can use var_export() and it will output valid PHP.

This tip thanks to chx: http://www.drupal4hu.com/node/134

Returns Wrong Date

Can't figure this out:

I keep getting this date:
196931Dec

Any ideas?

Matt, sounds like you have a

Matt, sounds like you have a null/zero value. It'll get interpreted as that date.

mlsamuelson

© 2008