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 />

After all these modifications, the second draft of our single_blog_block_view()<br />

function is now complete and should look like this:<br />

/**<br />

* Implements hook_block_view().<br />

*<br />

* Second draft!<br />

*<br />

* @pararm $delta<br />

* The name of the requested block.<br />

*/<br />

function single_blog_block_view($delta = '') {<br />

// Create an empty block.<br />

$block = array(<br />

'subject' => '',<br />

'content' => '',<br />

);<br />

// Check which block is being requested.<br />

if ($delta == 'recent') {<br />

// Set the block title.<br />

$block['subject'] = t('Recent blog posts');<br />

// Check if the user can access content.<br />

if (user_access('access content')) {<br />

// Retrieve the most recent nodes.<br />

$result = single_blog_list(SINGLE_BLOG_LIST_COUNT);<br />

// Create links for each blog entry.<br />

$items = array();<br />

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

$items[] = array(<br />

'data' => array(<br />

'#type' => 'link',<br />

'#title' => $node->title,<br />

'#href' => 'node/' . $node->nid,<br />

),<br />

'class' => array('node-' . $node->nid),<br />

);<br />

}<br />

if (!empty($items)) {<br />

// Theme the list of blog entries.<br />

[ 105 ]

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

Saved successfully!

Ooh no, something went wrong!