Drupal 7 block render array

When creating an old fashion block in Drupal 7 you should try to use the new render() function in Drupal 7. It takes a bit getting used to, especially when it comes to passing variables to a theming function.

This is probably how your current block content would look inside a hook_block_view() function:

$block['content'] = '<p>some HTML</p>';

This will work as expected, but if you want to use the render function it should look like this:

$block['content'] = array(     '#markup' => '<p>some HTML</p>', );

Pretty basic stuff. Now once you start using some of the build in theming functions in Drupal 7 it gets a bit more complicated.

In a project I am currently working on I wanted to use the theme_image_formatter function build into Drupal 7 in order to output an image coming from facebook’s graph API.

This is how the code looks inside the hook_block_view()

$block['content'] = array(     '#theme' => 'image_formatter',     '#item' => array(         'uri' => 'hello.png',         'alt' => 'hello',          'title' => 'some title',     ), );
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: