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.

Theming a <strong>Module</strong><br />

* Enables a single-user blog.<br />

*/<br />

// After you learn Form API in Chapter 5, you'll be able to<br />

// make these settings configurable.<br />

define('SINGLE_BLOG_NODE_TYPE', 'article');<br />

define('SINGLE_BLOG_LIST_COUNT', 3);<br />

/**<br />

* Returns a list of blog entries.<br />

*<br />

* @param $number<br />

* The number of blog entries to return.<br />

* @return<br />

* A result set object containing the list of blog entries.<br />

*/<br />

function single_blog_list($number) {<br />

// Use the Database API to retrieve our data.<br />

// @see http://drupal.org/node/310069<br />

$query = db_select('node', 'n')<br />

->fields('n', array('nid', 'title', 'created', 'uid'))<br />

->condition('type', SINGLE_BLOG_NODE_TYPE)<br />

->condition('status', 1)<br />

->orderBy('created', 'DESC')<br />

->range(0, $number)<br />

->addTag('node_access')<br />

->execute();<br />

}<br />

return $query;<br />

/**<br />

* Implements hook_block_info().<br />

*/<br />

function single_blog_block_info() {<br />

$blocks = array();<br />

// The array key defines the $delta parameter used in all<br />

// other block hooks.<br />

$blocks['recent'] = array(<br />

// The name of the block on the blocks administration page.<br />

'info' => t('Recent blog posts'),<br />

);<br />

return $blocks;<br />

}<br />

[ 94 ]

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

Saved successfully!

Ooh no, something went wrong!