/[drupal]/contributions/modules/nodeasblock/nodeasblock.module
ViewVC logotype

Contents of /contributions/modules/nodeasblock/nodeasblock.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (show annotations) (download) (as text)
Thu Aug 20 04:26:10 2009 UTC (3 months ago) by herc
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +55 -11 lines
File MIME type: text/x-php
- Implement of hook_content_build_modes().
- Add a template for theming instead of a theme function.
1 <?php
2 // $Id: nodeasblock.module,v 1.14 2009/08/13 22:52:33 herc Exp $
3
4 /**
5 * @file
6 * Allows those with the appropriate permissions the right to create blocks
7 * for each node using nodeapi.
8 */
9
10 /**
11 * @name Node as block visibility settings
12 * @{
13 */
14
15 /**
16 * Indicates that node as block feature is disabled for the specific content type.
17 */
18 define('NODEASBLOCK_DISABLED', 0);
19
20 /**
21 * Indicates that the node author decide whether to create a block
22 * for the node.
23 */
24 define('NODEASBLOCK_PER_NODE', 1);
25
26 /**
27 * Indicates that a node block is created always for the specific content type.
28 */
29 define('NODEASBLOCK_ALWAYS', 2);
30
31 /**
32 * @} End of "Node as block visibility settings".
33 */
34
35 /**
36 * @name Node as block build modes.
37 * @{
38 */
39
40 define('NODEASBLOCK_BUILD_TEASER', 'nodeasblock teaser');
41 define('NODEASBLOCK_BUILD_FULL', 'nodeasblock full');
42
43 /**
44 * @} End of "Node as block build modes".
45 */
46
47 /**
48 * Implementation of hook_perm().
49 */
50 function nodeasblock_perm() {
51 return array('create blocks for nodes', 'administer blocks for nodes');
52 }
53
54 /**
55 * Implementation of hook_theme().
56 */
57 function nodeasblock_theme() {
58 return array(
59 'nodeasblock' => array(
60 'arguments' => array('node' => NULL),
61 ),
62 );
63 }
64
65 /**
66 * Implementation of hook_form_alter_node_type_form().
67 */
68 function nodeasblock_form_node_type_form_alter($form, $form_state) {
69 if (isset($form['identity']['type'])) {
70
71 drupal_theme_initialize();
72 $theme = variable_get('theme_default', 'garland');
73
74 $node_type = $form['#node_type']->type;
75
76 $form['nodeasblockset'] = array(
77 '#type' => 'fieldset',
78 '#title' => t('Node as block settings'),
79 '#collapsible' => TRUE,
80 '#collapsed' => TRUE,
81 );
82 $form['nodeasblockset']['nodeasblock'] = array(
83 '#type' => 'radios',
84 '#title' => t('Create blocks from this content type'),
85 '#default_value' => variable_get('nodeasblock_'. $node_type, 1),
86 '#options' => array(
87 NODEASBLOCK_DISABLED => t('Never (Disabled)'),
88 NODEASBLOCK_PER_NODE => t('On a per node basis'),
89 NODEASBLOCK_ALWAYS => t('Always')
90 ),
91 '#return_value' => 1,
92 '#description' => t('Allow users to make these nodes visible as sidebar blocks.'),
93 );
94
95 // Regions to select from default theme.
96 $regions = system_region_list($theme);
97 $form['nodeasblockset']['nodeasblock_region'] = array(
98 '#type' => 'select',
99 '#multiple' => TRUE,
100 '#title' => t('Select allowed regions for these blocks'),
101 '#options' => $regions,
102 '#default_value' => variable_get('nodeasblock_region_'. $node_type, array()),
103 '#description' => t('These are only the regions defined for the default theme'),
104 );
105 $form['nodeasblockset']['nodeasblock_default_region'] = array(
106 '#type' => 'select',
107 '#title' => t('Default region'),
108 '#options' => $regions,
109 '#default_value' => variable_get('nodeasblock_default_region_'. $node_type, system_default_region($theme)),
110 );
111 $form['nodeasblockset']['nodeasblock_default_weight'] = array(
112 '#type' => 'weight',
113 '#title' => t('Default weight'),
114 '#options' => $regions,
115 '#default_value' => variable_get('nodeasblock_default_weight_'. $node_type, 0),
116 );
117 }
118 }
119
120 /**
121 * Implementation of hook_form_alter().
122 */
123 function nodeasblock_form_alter($form, $form_state, $form_id) {
124 if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id &&
125 variable_get('nodeasblock_'. $form['type']['#value'], TRUE) &&
126 user_access('create blocks for nodes')) {
127 _nodeasblock_node_form($form);
128 }
129 }
130
131 /**
132 * Helper function to provide the node form additions.
133 */
134 function _nodeasblock_node_form(&$form) {
135 drupal_theme_initialize();
136 $theme = variable_get('theme_default', 'garland');
137
138 $node = $form['#node'];
139
140 if (isset($node->nid)) {
141 $block = db_select('block', 'b')
142 ->fields('b')
143 ->condition('module', 'nodeasblock')
144 ->condition('theme', $theme)
145 ->condition('delta', $node->nid)
146 ->execute()
147 ->fetchAssoc();
148 }
149 else {
150 $block = array(
151 'status' => 1,
152 'roles' => array(),
153 'region' => variable_get('nodeasblock_default_region_'. $node->type, system_default_region($theme)),
154 'weight' => variable_get('nodeasblock_default_weight_'. $node->type, 0),
155 'custom' => 0, 'visibility' => 0,
156 'pages' => ''
157 );
158 $node->nid = 0;
159 }
160
161 if (!isset($node->nodeasblock)) {
162 $node->nodeasblock = 0;
163 }
164
165 $form['nodeasblockset'] = array(
166 '#type' => 'fieldset',
167 '#title' => t('Provide a block'),
168 '#collapsible' => TRUE,
169 '#collapsed' => TRUE,
170 '#tree' => TRUE
171 );
172 $form['nodeasblockset']['status'] = array('#type' => 'value', '#value' => $block['status']);
173
174 // If variable not set we'll assume 1 = on a per node basis.
175 $form['nodeasblockset']['nodeasblock'] = array(
176 '#type' => 'checkbox',
177 '#title' => t('Create a block for this node?'),
178 '#default_value' => $node->nodeasblock,
179 '#description' => t('Check this box to create a block for this node. The block will contain the teaser.'),
180 );
181
182 // Slight change to the UI if the block is required.
183 if (variable_get("nodeasblock_$node->type", NODEASBLOCK_PER_NODE) == NODEASBLOCK_ALWAYS) {
184 $form['nodeasblockset']['nodeasblock']['#default_value'] = TRUE;
185 $form['nodeasblockset']['nodeasblock']['#disabled'] = 'disabled';
186 $form['nodeasblockset']['#collapsed'] = FALSE;
187 }
188
189 // Advanced settings.
190 if (user_access('administer blocks') || user_access('administer blocks for nodes')) {
191 // Fetch available list of regions.
192 $theme_regions = system_region_list($theme);
193 $allowed_regions = array(BLOCK_REGION_NONE => '<'. t('none') .'>');
194 foreach (variable_get("nodeasblock_region_". $node->type, array()) as $region) {
195 if (array_key_exists($region, $theme_regions)) {
196 $allowed_regions[$region] = $theme_regions[$region];
197 }
198 }
199 $form['nodeasblockset']['region'] = array(
200 '#type' => 'select',
201 '#options' => $allowed_regions,
202 '#title' => t('Region'),
203 '#default_value' => $block['region'],
204 '#description' => t('The region of the page this block should appear in.'),
205 );
206 $form['nodeasblockset']['weight'] = array(
207 '#type' => 'weight',
208 '#title' => t('Weight'),
209 '#default_value' => $block['weight'],
210 '#description' => t('The weight of this block. Larger numbers move a block farther down in a region.')
211 );
212
213 // Save title and reset after.
214 module_load_include('inc', 'block', 'block.admin');
215 $title = drupal_get_title();
216 $form_state = array('storage' => NULL, 'submitted' => FALSE);
217 $block_form = block_admin_configure($form_state, 'nodeasblock', $node->nid);
218 drupal_set_title($title);
219
220 foreach (array('block_settings', 'page_vis_settings') as $category) {
221 $form['nodeasblockset'][$category] = $block_form[$category];
222 $form['nodeasblockset'][$category]['#collapsed'] = TRUE;
223 $form['nodeasblockset'][$category]['#parents'] = array('nodeasblockset');
224 }
225
226 // Really advanced settings, only for real block administrators.
227 if (user_access('administer blocks')) {
228 foreach (array('user_vis_settings', 'role_vis_settings') as $category) {
229 $form['nodeasblockset'][$category] = $block_form[$category];
230 $form['nodeasblockset'][$category]['#collapsed'] = TRUE;
231 $form['nodeasblockset'][$category]['#parents'] = array('nodeasblockset');
232 }
233 }
234 else {
235 $form['nodeasblock']['custom'] = array('#type' => 'value', '#value' => $block['custom']);
236 }
237 }
238 else {
239 // Just pass values.
240 foreach (array('region', 'weight', 'title', 'custom', 'roles', 'visibility', 'pages') as $field) {
241 $form['nodeasblockset'][$field] = array('#type' => 'value', '#value' => $block[$field]);
242 }
243 }
244 }
245
246 /**
247 * Implement hook_node_load().
248 */
249 function nodeasblock_node_load($nodes) {
250 $result = db_select('nodeasblock', 'nab')
251 ->fields('nab', array('nid'))
252 ->condition('nid', array_keys($nodes), 'IN')
253 ->execute();
254 foreach ($result as $record) {
255 $nodes[$record->nid]->nodeasblock = TRUE;
256 }
257 }
258
259 /**
260 * Implement hook_node_insert().
261 */
262 function nodeasblock_node_insert($node) {
263 _nodeasblock_node_save($node);
264 }
265
266 /**
267 * Implement hook_node_update().
268 */
269 function nodeasblock_node_update($node) {
270 _nodeasblock_node_save($node);
271 }
272
273 /**
274 * Helper function to respond to node updates and inserts.
275 *
276 * @param $node
277 * The node object.
278 */
279 function _nodeasblock_node_save($node) {
280 // Only modify/create block if the current user has access.
281 if (user_access('create blocks for nodes')) {
282 // First delete the entry from the table if it exists.
283 db_delete('nodeasblock')
284 ->condition('nid', $node->nid)
285 ->execute();
286 if ($node->nodeasblockset['nodeasblock'] || variable_get("nodeasblock_$node->type", NODEASBLOCK_DISABLED) == NODEASBLOCK_ALWAYS) {
287 db_insert('nodeasblock')
288 ->fields(array('nid' => $node->nid))
289 ->execute();
290 drupal_set_message(t('The block configuration has been saved.'));
291 nodeasblock_save_block($node);
292 }
293 }
294 }
295
296 /**
297 * Implement hook_node_delete().
298 */
299 function nodeasblock_node_delete($node) {
300 db_delete('nodeasblock')
301 ->condition('nid', $node->nid)
302 ->execute();
303 // Calling _block_rehash() will delete the block entry from
304 // the {blocks} table and other related tables.
305 _block_rehash();
306 }
307
308 /**
309 * Save the block data associated to a node.
310 *
311 * @param $node
312 * The node object.
313 */
314
315 function nodeasblock_save_block($node) {
316 // Fetch block data.
317 $block = $node->nodeasblockset;
318 // This will actually creates the block.
319 _block_rehash();
320 // Update block data.
321 db_update('block')
322 ->fields(array(
323 'status' => $block['status'],
324 'region' => $block['region'],
325 'weight' => $block['weight'],
326 'visibility' => $block['visibility'],
327 'custom' => $block['custom'],
328 'title' => $block['title']
329 ))
330 ->condition('module', 'nodeasblock')
331 ->condition('delta', $node->nid)
332 ->execute();
333
334 if (is_array($block['roles'])) {
335 db_delete('block_role')
336 ->condition('module', 'nodeasblock')
337 ->condition('delta', $node->nid);
338 $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
339 foreach (array_filter($block['roles']) as $rid) {
340 $query->values(array($rid, 'nodeasblock', $node->nid));
341 }
342 $query->execute();
343 }
344 cache_clear_all();
345 }
346
347 /**
348 * Implementation of hook_block().
349 */
350 function nodeasblock_block($op = 'list', $delta = 0) {
351 if ($op == 'list') {
352 // Get the list of nodeblock nodes from database.
353 $nids = db_query('SELECT nid FROM {nodeasblock}')->fetchCol();
354 $nodes = node_load_multiple($nids);
355 $blocks = array();
356 foreach ($nodes as $$node) {
357 $blocks[$node->nid]['info'] = check_plain($node->title);
358 }
359 return $blocks;
360 }
361 elseif ($op == 'view') {
362 // Don't show on own node page
363 if (arg(0) == 'node' && arg(1) == $delta) {
364 return;
365 }
366 // Load the node if it's published.
367 if ($node = node_load(array('nid' => $delta, 'status' => 1))) {
368 // Don't show nodes in other languages than current. Integration with i18n node languages.
369 global $language;
370 if (empty($node->language) || $node->language == $language->language) {
371 return array(
372 'subject' => l($node->title, 'node/'. $node->nid),
373 'content' => nodeasblock_block_content($node),
374 );
375 }
376 }
377 }
378 }
379
380 /**
381 * Implement hook_page_alter().
382 *
383 * Render blocks into their regions.
384 */
385 function nodeasblock_page_alter($page) {
386 global $theme;
387
388 // The theme system might not yet be initialized. We need $theme.
389 drupal_theme_initialize();
390
391 // Get the list of nodeblock nodes from database.
392 $nids = db_query('SELECT nid FROM {nodeasblock}')->fetchCol();
393 $nodes = node_load_multiple($nids);
394 $build = node_build_multiple($nodes);
395
396 $page['sidebar_first']['nodeasblock'] = $build['nodes'];
397
398 }
399 /**
400 * Theme the node as a block.
401 *
402 * @ingroup themeable
403 */
404 function nodeasblock_block_content($node) {
405 $node->build_mode = NODEASBLOCK_BUILD_TEASER;
406 $node = node_build_content($node, TRUE, FALSE);
407 $node->body = drupal_render($node->content);
408
409 return theme('nodeasblock', $node);
410 }
411
412 /**
413 * Implementation of hook_content_build_modes().
414 */
415 function nodeasblock_content_build_modes() {
416 return array(
417 'block' => array(
418 'title' => t('Block'),
419 'build modes' => array(
420 NODEASBLOCK_BUILD_TEASER => array(
421 'title' => t('Teaser'),
422 ),
423 NODEASBLOCK_BUILD_FULL => array(
424 'title' => t('Full node'),
425 ),
426 ),
427 ),
428 );
429 }
430
431 /**
432 * Process variables for nodeasblock.tpl.php.
433 *
434 * The $variables array contains the following arguments:
435 * - $node
436 *
437 * @see nodeasblock.tpl.php
438 */
439 function template_preprocess_nodeasblock(&$variables) {
440 $node = $variables['node'];
441 // If the user has permissions to edit the node, then add a link.
442 if (node_access('update', $node)) {
443 $variables['edit_link'] = l('['. t('edit') .']', 'node/'. $node->nid .'/edit', array("title" => t("Edit")));
444 }
445 $variables['content'] = $variables['node']->body;
446 }

  ViewVC Help
Powered by ViewVC 1.1.2