-
Notifications
You must be signed in to change notification settings - Fork 6
/
single_blog.module
163 lines (144 loc) · 4.33 KB
/
single_blog.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
// $Id$
/**
* @file
* Enables a single blog for an individual or multiple users.
*/
// After you learn Form API in Chapter 5, you'll be able to
// make these settings configurable.
define('SINGLE_BLOG_NODE_TYPE', 'article');
define('SINGLE_BLOG_LIST_COUNT', 5);
define('SINGLE_BLOG_DATE_FORMAT', 'F d, Y');
/**
* Returns a list of blog entries.
*
* @param $number
* The number of blog entries to return.
* @return
* A result set object containing the list of blog entries.
*/
function single_blog_list($number) {
// Use the Database API to retrieve our data.
// @see http://drupal.org/node/310069
$query = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created', 'uid'))
->condition('type', SINGLE_BLOG_NODE_TYPE)
->condition('status', 1)
->orderBy('created', 'DESC')
->range(0, $number)
->addTag('node_access')
->execute();
return $query;
}
/**
* Implements hook_block_info().
*/
function single_blog_block_info() {
$blocks = array();
// The array key defines the $delta parameter used in all
// other block hooks.
$blocks['recent'] = array(
// The name of the block on the blocks administration page.
'info' => t('Recent blog posts'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*
* Third draft!
*
* @pararm $delta
* The name of the requested block.
*/
function single_blog_block_view($delta = '') {
// Create an empty block.
$block = array(
'subject' => '',
'content' => '',
);
// Check which block is being requested.
if ($delta == 'recent') {
// Set the block title.
$block['subject'] = t('Recent blog posts');
// Check if the user can access content.
if (user_access('access content')) {
// Retrieve the most recent nodes.
$result = single_blog_list(SINGLE_BLOG_LIST_COUNT);
// Create links for each blog entry.
$items = array();
foreach ($result as $node) {
$items[] = array(
'data' => array(
'#theme' => 'single_blog_block_item',
'#node' => $node,
),
'class' => array('node-' . $node->nid),
);
}
if (!empty($items)) {
// Theme the list of blog entries.
$block['content']['list'] = array(
'#theme' => 'item_list__single_blog',
'#items' => $items,
'#pre_render' => array('single_blog_item_list_child_render'),
);
// Add a link to the full list of blog entries.
$block['content']['more'] = array(
'#theme' => 'more_link',
'#url' => 'blog',
'#title' => t('Read the latest blog entries.'),
);
// Add a CSS file to style the block.
$block['content']['#attached']['css'][] = drupal_get_path('module', 'single_blog') . '/single-blog.css';
}
}
}
return $block;
}
/**
* Render the child elements of theme_item_list() before its data is themed.
*/
function single_blog_item_list_child_render($elements) {
foreach (array_keys($elements['#items']) AS $key) {
// Take the renderable array that we set in single_blog_block_view() and
// render it into the string that theme_item_list() expects.
if (is_array($elements['#items'][$key]['data'])) {
$elements['#items'][$key]['data'] = drupal_render($elements['#items'][$key]['data']);
}
}
return $elements;
}
/**
* Implements hook_theme().
*/
function single_blog_theme($existing, $type, $theme, $path) {
return array(
'single_blog_block_item' => array(
'variables' => array(
'node' => NULL,
),
'template' => 'single-blog-block-item',
),
);
}
/**
* Preprocesses single blog block item variables.
*/
function template_preprocess_single_blog_block_item(&$variables) {
$node = $variables['node'];
// Create a renderable array for the title.
$variables['title'] = array(
'#type' => 'link',
'#title' => $node->title,
'#href' => 'node/' . $node->nid,
);
// Format the creation date of the node.
$variables['created'] = $node->created;
$variables['date'] = format_date($node->created, 'custom', SINGLE_BLOG_DATE_FORMAT);
// Load the account object with the node's creator and store in a variable for
// themer convenience.
$variables['user'] = user_load($node->uid);
// Theme the username.
$variables['name'] = theme('username', array('account' => $variables['user']));
}