18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4<br />

Our single_blog_block_info() function is a simple hook_block_info()<br />

implementation as described in Chapter 2. We return an array of information that<br />

describes the blocks that our module provides. Each key in the array is the "delta"<br />

for that block. The delta, when combined with the name of the module, creates<br />

the unique block ID for a block which is stored in the block.tpl.php file's<br />

$block_html_id. For example, we've defined our "recent blog posts" block as<br />

having a delta of recent, so its full block ID is single-blog-recent. The block ID<br />

is used by <strong>Drupal</strong> to assign blocks to regions. The "info" returned by our<br />

single_blog_block_info() function defines the block "name" that you see<br />

on the block administration page.<br />

The single_blog_block_view() function implements hook_block_view().<br />

When <strong>Drupal</strong> wants to render a particular block, it calls the hook_block_view()<br />

implementation of the module responsible for the block, passing in the block's<br />

$delta as a parameter. Our function first checks the $delta given to it and then<br />

returns the requested block's data as an array containing the block title in the<br />

subject key and its contents in the content key. First, we're going to set the block<br />

title using the t() function. From Chapter 2, you should recall that t() translates<br />

English language strings into other languages. Then our function checks that the<br />

user viewing the page can access the website's content via user_access('access<br />

content'); this call checks if the current user has the access content permission. If<br />

the current user doesn't have the proper permission, you'll notice that our function<br />

returns an empty content key; this signals to <strong>Drupal</strong> that the requested block should<br />

not be rendered.<br />

The last thing we're going to do before we finally start theming is to call our API<br />

function, single_blog_list(), in order to get the raw data from the database.<br />

Actually, single_blog_list() returns a result set object, but we haven't learned<br />

any special Database API functions to retrieve each row from the result set object. If<br />

we just iterate over this object using foreach, we'll get a series of objects that contain<br />

each row's data.<br />

foreach ($result as $node) { }<br />

Specifically, each time through the foreach loop, $node will be an object with<br />

properties for each database field we requested, nid, title, created, and uid.<br />

When the $result object has processed all the rows, the foreach loop will<br />

automatically end.<br />

[ 97 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!