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

Contents of /contributions/modules/nodeblock/nodeblock.module

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


Revision 1.11 - (show annotations) (download) (as text)
Wed Apr 1 03:12:10 2009 UTC (7 months, 3 weeks ago) by rz
Branch: MAIN
Changes since 1.10: +32 -3 lines
File MIME type: text/x-php
task #400010: Better theming experience
1 <?php
2 // $Id: nodeblock.module,v 1.10 2009/02/05 20:00:12 rz Exp $
3
4 /**
5 * @file
6 * Enables use of specified node types as custom blocks.
7 */
8
9 /**
10 * Utility function to tell whether a type is enabled as a node block
11 */
12 function nodeblock_type_enabled($type) {
13 if (is_object($type)) {
14 $type = $type->type;
15 }
16 return variable_get('nodeblock_'. $type, 0) ? TRUE : FALSE;
17 }
18
19 /**
20 * Implementation of hook_form_alter().
21 */
22 function nodeblock_form_alter(&$form, $form_state, $form_id) {
23 // content type settings form
24 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
25 $form['workflow']['nodeblock'] = array(
26 '#type' => 'radios',
27 '#title' => t('Available as block'),
28 '#default_value' => variable_get('nodeblock_'. $form['#node_type']->type, 0),
29 '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
30 '#description' => t('Should these nodes be made available as blocks?'),
31 );
32 }
33 // node add/edit form
34 elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
35 $node = $form['#node'];
36 // Add translation fallback field for nodeblock and translation enabled source nodes only
37 if (nodeblock_type_enabled($node->type) && module_exists('translation') && translation_supported_type($node->type) && empty($node->translation_source)) {
38 $form['nodeblock'] = array(
39 '#type' => 'fieldset',
40 '#title' => t('Block translation options'),
41 '#tree' => true,
42 );
43 $form['nodeblock']['translation_fallback'] = array(
44 '#type' => 'checkbox',
45 '#title' => t('Enable translation fallback?'),
46 '#description' => t('If checked, the source translation node will be used when a translation for the current language does not exist. If unchecked, the block will not be displayed if a matching translation does not exist.'),
47 '#default_value' => $node->nodeblock_translation_fallback,
48 );
49 }
50 }
51 }
52
53 /**
54 * Implementation of hook_nodeapi().
55 */
56 function nodeblock_nodeapi(&$node, $op, $teaser, $page) {
57 // do nothing if not enabled
58 if (!nodeblock_type_enabled($node)) {
59 return;
60 }
61
62 switch ($op) {
63 case 'load':
64 $tnid = $node->tnid ? $node->tnid : $node->nid;
65 return array('nodeblock_translation_fallback' => variable_get('nodeblock_translation_fallback_'. $tnid, 1));
66
67 case 'insert':
68 case 'update':
69 drupal_set_message(t('The block you just created is now available on the <a href="!url">block configuration page</a>.', array('!url' => url('admin/build/block'))));
70
71 // set the translation fallback variable if set.
72 if (isset($node->nodeblock['translation_fallback'])) {
73 $tnid = $node->tnid ? $node->tnid : $node->nid;
74 variable_set('nodeblock_translation_fallback_'. $tnid, $node->nodeblock['translation_fallback']);
75 }
76 _block_rehash();
77 break;
78
79 case 'delete':
80 _block_rehash();
81 break;
82 }
83 }
84
85 /**
86 * Implementation of hook_block().
87 */
88 function nodeblock_block($op = 'list', $delta = 0, $edit = array()) {
89 $types = node_get_types();
90 if ($op == 'list') {
91 foreach ($types as $type) {
92 if (nodeblock_type_enabled($type)) {
93 // Fetch all nodes of this type, excluding translations.
94 $result = db_query('SELECT nid, title FROM {node} WHERE type = "%s" AND status = 1 AND (nid = tnid OR tnid = 0)', $type->type);
95 while ($node = db_fetch_object($result)) {
96 $blocks[$node->nid] = array('info' => $node->title .' (nodeblock)');
97 }
98 }
99 }
100 return $blocks;
101 }
102 elseif ($op == 'view') {
103 $node = node_load($delta);
104
105 // if the node type is translatable, try to load the node with the appropriate
106 // language from the translation set.
107 if (module_exists('translation') && translation_supported_type($node->type)) {
108 global $language;
109 $translations = translation_node_get_translations($node->tnid);
110 if ($translations[$language->language]) {
111 $node = node_load($translations[$language->language]->nid);
112 }
113 elseif (!$node->nodeblock_translation_fallback) {
114 // if no translation was found, and not using the fallback option
115 // return nothing, so the block doesn't display.
116 return;
117 }
118 // otherwise we just use the main node
119 }
120
121 // Set a flag so that themes have more context.
122 $node->nodeblock = TRUE;
123
124 $block['subject'] = $node->title;
125 $block['content'] = node_view($node, FALSE, FALSE, TRUE);
126
127 return $block;
128 }
129 }
130
131 /**
132 * Implementation of hook_link().
133 */
134 function nodeblock_link($type, $node = NULL, $teaser = FALSE) {
135 $links = array();
136
137 if ($type == 'node' && nodeblock_type_enabled($node)) {
138 if (node_access('update', $node)) {
139 $links['nodeblock_edit'] = array(
140 'title' => t('Edit'),
141 'href' => 'node/'. $node->nid .'/edit',
142 'query' => drupal_get_destination(),
143 );
144 }
145 if (module_exists('translation') && _translation_tab_access($node)) {
146 $links['nodeblock_translate'] = array(
147 'title' => t('Translate'),
148 'href' => 'node/'. $node->nid .'/translate',
149 'query' => drupal_get_destination(),
150 );
151 }
152 if (user_access('administer blocks')) {
153 $links['nodeblock_configure'] = array(
154 'title' => t('Configure'),
155 'href' => 'admin/build/block/configure/nodeblock/'. $node->nid,
156 'query' => drupal_get_destination(),
157 );
158 }
159 }
160
161 return $links;
162 }
163
164 /**
165 * Implementation of hook_preprocess_node().
166 *
167 * Add node-nodeblock-default to the suggested theme files for all nodeblock
168 * enabled nodes. Note that the template is "unshifted" onto the template files
169 * array. This gives the template file a lower priority than any node-nodetype
170 * templates, but a higher priority than a generic node.tpl.php.
171 */
172 function nodeblock_preprocess_node(&$variables) {
173 if ($variables['node']->nodeblock) {
174 array_unshift($variables['template_files'], 'node-nodeblock-default');
175 }
176 }
177
178 /**
179 * Implementation of hook_theme_registry_alter().
180 *
181 * Add nodeblock path to the 'theme paths' for the 'node' hook. This allows us
182 * to use node-nodeblock-default.tpl.php from the module directory. Note that
183 * the path is "unshifted" onto the theme paths array. This puts the module path
184 * before the modules/node path, but since neither of these modules implements
185 * the same templates, there is not problem.
186 */
187 function nodeblock_theme_registry_alter(&$registry) {
188 array_unshift($registry['node']['theme paths'], drupal_get_path('module', 'nodeblock'));
189 }

  ViewVC Help
Powered by ViewVC 1.1.2