-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeimageblock.module
More file actions
494 lines (444 loc) · 16.9 KB
/
nodeimageblock.module
File metadata and controls
494 lines (444 loc) · 16.9 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/* $Id$
*
* Copyright (C) 2007-2008 Thomas Barregren.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file
* Node Image Block 2 – a Drupal module that provides a block that displays
* all images attached to a node.
*
* Node Image Block 2 is developed by Thomas Barregren. It is a complete
* rewrite of the original Node Image Block developed by Mike Carter.
*
* Author:
* Thomas Barregren <http://drupal.org/user/16678>
*/
/******************************************************************************
* THEMEABLE FUNCTIONS
******************************************************************************/
/**
* Themeable function for the container of the images to be viewed.
*
* @param images
* An array of image objects. An image object has following attributes:
* - src:
* The path to the image to be shown. If an imagecache preset has been
* chosen in the block settings, this path will reflect that preset.
* Otherwise it is the same as the path to uploaded image.
* - path:
* The path to the originally uploaded image.
* - desc:
* An description of the image as given in the node's file attachement
* fieldset.
*/
function theme_nodeimageblock($images) {
global $theme_engine;
// If the current theme engine is PHPTemplate and the current theme has a
// Node Image Block 2 template file, process the provided template file, and
// return the resulting output. Otherwise, return the default theming.
if ($theme_engine == 'phptemplate' && file_exists(path_to_theme() .'/nodeimageblock.tpl.php')) {
//$out = _phptemplate_callback('nodeimageblock', array('images' => $images));
$out = theme_render_template(path_to_theme() . '/nodeimageblock.tpl.php', array('images' => $images));
}
else {
$out[] = '<div id="nodeimageblock">';
foreach ($images as $img) {
$out[] = ' <div>';
$out[] = ' <img src="'. $img->src .'" alt="'. $img->desc .'" title="'. $img->desc .'" />';
$out[] = ' <p>'. $img->desc .'</p>';
$out[] = ' </div>';
}
$out[] = '</div>';
$out = implode("\n", $out);
}
return $out;
}
/******************************************************************************
* CONSTANTS
******************************************************************************/
/**
* Bitwise flag for viewing images that are checked in the node's file
* attachement fieldset to be listed.
*/
define('NODEIMAGEBLOCK_INCLUDE_LISTED', 0x01);
/**
* Bitwise flag for viewing images that are not checked in the node's file
* attachement fieldset to be listed.
*/
define('NODEIMAGEBLOCK_INCLUDE_NOT_LISTED', 0x02);
/******************************************************************************
* HOOKS
******************************************************************************/
/**
* Implementation of hook_block().
*/
function nodeimageblock_block($op, $delta = 0, $edit = array()) {
$function = 'nodeimageblock_block_'. $op;
if (function_exists($function)) {
return $function($delta, $edit);
}
}
/**
* Implementation of hook_help().
*/
function nodeimageblock_help($path, $arg) {
switch ($path) {
case 'admin/help#nodeimageblock':
return _nodeimageblock_help();
}
}
/**
* Implementation of hook_theme().
*/
function nodeimageblock_theme() {
return array(
'nodeimageblock' => array(
'arguments' => array('images' => NULL)
)
);
}
/******************************************************************************
* IMPLEMENTATION OF THE BLOCK API
******************************************************************************/
/**
* Implements the 'list' operator of te hook_block().
*/
function nodeimageblock_block_list() {
$block[0]['info'] = 'Node Image Block 2';
return $block;
}
/**
* Implements the 'configure' operator of te hook_block().
*/
function nodeimageblock_block_configure($delta) {
// Ask for which images to include.
$options = array(
NODEIMAGEBLOCK_INCLUDE_LISTED | NODEIMAGEBLOCK_INCLUDE_NOT_LISTED => 'All attached images',
NODEIMAGEBLOCK_INCLUDE_LISTED => 'Only attached images that are listed',
NODEIMAGEBLOCK_INCLUDE_NOT_LISTED => 'Only attached images that are not listed',
);
$form[nodeimageblock_images_to_include] = array(
'#type' => 'select',
'#title' => 'Images to show',
'#description' => 'Select which attached images to show.',
'#options' => $options,
'#default_value' => nodeimageblock_variable_images_to_include(),
);
// Ask for imagecache preset to use (if any).
if (module_exists('imagecache')) {
$options = _imagecache_get_presets();
$options[0] = t('<none>');
ksort($options);
$form[nodeimageblock_imagecahce_preset] = array(
'#type' => 'select',
'#title' => 'Imagecache preset',
'#description' => 'Select the Imagecache preset to use for the images in the Node Image Block 2.',
'#options' => $options,
'#default_value' => nodeimageblock_variable_imagecahce_preset(),
);
}
return $form;
}
/**
* Implements the 'save' operator of te hook_block().
*/
function nodeimageblock_block_save($delta, $edit) {
nodeimageblock_variable_images_to_include($edit['nodeimageblock_images_to_include']);
nodeimageblock_variable_imagecahce_preset($edit['nodeimageblock_imagecahce_preset']);
}
/**
* Implements the 'view' operator of te hook_block().
*/
function nodeimageblock_block_view($delta) {
// Abort if the page viewed isn't a node.
if (arg(0) != 'node' || !is_numeric($nid = arg(1))) return;
// Load the images. Abort if there are no images.
if (!($images = _nodeimageblock_images($nid))) return;
// Get the base path for the original file path and the source path.
$path_base = base_path();
$src_base = _nodeimageblock_source_base();
// Build the image objects that will be passed into the themable function.
foreach ($images as $image) {
$item = new stdClass();
$item->path = "$path_base$image->filepath";
$item->desc = $image->description;
$item->src = "$src_base$image->filepath";
$items[] = $item;
}
// Theme and return the block.
$block['subject'] = '';
$block['content'] = theme('nodeimageblock', $items);
return $block;
}
/**
* Returns an array of file object representing image files attached to the
* node id $nid.
*/
function _nodeimageblock_images($nid) {
if (($files = module_invoke('upload', 'load', node_load($nid)))) {
return array_filter($files, '_nodeimageblock_images_filter');
}
}
/**
* Returns true if $file is an image which should be viewed.
*/
function _nodeimageblock_images_filter($file) {
static $image_mime = array("image/gif", "image/png", "image/jpeg", "image/pjpeg");
$flags = nodeimageblock_variable_images_to_include();
return in_array($file->filemime, $image_mime) && ($flags & NODEIMAGEBLOCK_INCLUDE_LISTED && $file->list || $flags & NODEIMAGEBLOCK_INCLUDE_NOT_LISTED && !$file->list);
}
/**
* Returns the base path of the images.
*/
function _nodeimageblock_source_base() {
$base_path = base_path();
$preset = nodeimageblock_variable_imagecahce_preset();
if ($preset && module_exists('imagecache')) {
$preset = _imagecache_preset_load($preset);
$base_path .= file_directory_path() ."/imagecache/$preset/";
}
return $base_path;
}
/******************************************************************************
* PERSISTED VARIABLES
*****************************************************************************/
/**
* The persisted variable 'images_to_include' contains the flags indicating
* which images to be included.
*/
function nodeimageblock_variable_images_to_include($include = null) {
return _nodeimageblock_variable('nodeimageblock_images_to_include', $include, NODEIMAGEBLOCK_INCLUDE_LISTED | NODEIMAGEBLOCK_INCLUDE_NOT_LISTED);
}
/**
* The persisted variable 'preset' containing the selected imagecache preset.
*/
function nodeimageblock_variable_imagecahce_preset($preset = null) {
return _nodeimageblock_variable('nodeimageblock_imagecahce_preset', $preset, 0);
}
/**
* Sets and gets the named persisted variable.
*/
function _nodeimageblock_variable($name, $value = null, $default = null) {
if (isset($value)) {
variable_set($name, $value);
}
return variable_get($name, $default);
}
/******************************************************************************
* HELP
*****************************************************************************/
/**
* Returns full help text.
*/
function _nodeimageblock_help() {
$help = <<<EOT
<!-- Copyright (C) !year Thomas Barregren <mailto:thomas@webbredaktoren.se> -->
<style type="text/css" media="all">
/*<![CDATA[*/
code, kbd, pre { padding: 1px; font-family: "Bitstream Vera Sans Mono", Monaco, "Lucida Console", monospace; background-color: #EDF1F3; }
/*]]>*/
</style>
<p>
<a href="http://drupal.org/node/48676">Node Image Block 2</a> is a Drupal module that provides a block that displays all images attached to a node.
</p><!--break-->
<h2>
Requirements
</h2>
<p>
To install Node Image Block 2 you need:
</p>
<ul>
<li>
<a href="http://drupal.org/project/drupal">Drupal 5.x</a>
</li>
<li>
<a href="http://drupal.org/handbook/modules/upload">Upload module</a>
</li>
</ul>
<h2>
Installation
</h2>
<p>
Install Node Image Block 2 as follows:
</p>
<ol>
<li>
<p>
Enable and configure the <a href="http://drupal.org/handbook/modules/upload">Upload module</a>, following the instructions for that module.
</p>
</li>
<li>
<p>
Download the latest stable version of Node Image Block 2 from its <a href="http://drupal.org/node/48676">project page</a>.
</p>
</li>
<li>
<p>
Unpack the downloaded file into <code>sites/all/modules</code> or the modules directory of your site.
</p>
</li>
<li>
<p>
Go to <a href="/admin/build/modules">Administer » Site building » Modules</a> and enable the module.
</p>
</li>
</ol>
<h2>
Configuration
</h2>
<p>
Node Image Block 2 is configured as described below:
</p>
<ol>
<li>
<p>
Go to <a href="/admin/build/block">Administer » Site building » Blocks</a> and enable <em>Node Image Block 2</em>.
</p>
</li>
<li>
<p>
Click on the <a href="/admin/build/block/configure/nodeimageblock/0">configure</a> link of Node Image Block 2, and locate the <em>Block specific settings</em>.
</p>
</li>
<li>
<p>
Enter the title of the block in the text field called <em>Block title</em>. Leave empty (or write <code><none></code>) for no title.
</p>
</li>
<li>
<p>
In the drop down menu called <em>Images to show</em>, choose whether to display all images or only those images that are checked or those not checked to be included in the Upload module's list of attached files.
</p>
</li>
<li>
<p>
Make sure the remaning block settings are as desired, and press the <em>Save block</em> button.
</p>
</li>
</ol>
<p>
If the <a href="http://drupal.org/project/imagecache">Imagecache module</a> is installed, there is a third settings available in the <em>Block specific settings</em>. It is configured as follows:
</p>
<ol>
<li>
<p>
If not already existing, go to <a href="/admin/settings/imagecache">Administer » Site configuration » Image cache</a> and add at least one preset that can be used to generate and cache resized and/or cropped versions of the images to be displayed. Return to the <a href="/admin/build/block/configure/nodeimageblock/0">configuration page</a> of Node Image Block 2.
</p>
<p>
In the drop down menu called <em>Imagecache preset</em>, choose the preset to use on the images displayed by Node Image Block 2.
</p>
</li>
</ol>
<h2>
Usage
</h2>
<p>
It is trivial to use Node Image Block 2. Just upload one or more images to the node. For each image to be displeyd, enter a description and make sure that the <em>List</em> checkbox is set according to the Node Image Block 2 settings.
</p>
<h2>
Theming
</h2>
<p>
The default theming of Node Image Block 2 follows:
</p>
<pre>
<div id="nodeimageblock">
<div>
<img src="<em>\$src</em>" alt="<em>\$desc</em>" title="<em>\$desc</em>" />
<p><em>\$desc</em></p>
</div>
</div>
</pre>
<p>
where the inner <code><div></code>-block is repeared for each image to be displayed. <code><em>\$src</em></code> is the path to the image to be displayed and <code><em>\$desc</em></code> is the description of the image.
</p>
<h3>
Template file
</h3>
<p>
The easiest way to change this theming, is to copy <code>nodeimageblock.tpl.php</code> from the folder with the Node Image Block 2 module, e.g. <code>site/all/modules/nodeimageblock</code>, to the folder containing the theme's <code>page.tpl.php</code> and edit is as needed. This works for all themes based on the built-in <a href="http://drupal.org/phptemplate">PHPTemplate theme engine</a>.
</p>
<h3>
Themable function
</h3>
<p>
For advanced themers, and themes not based on the PHPTemplate theme engine, it is possible to override the themable function <code>theme_nodeimageblock(\$images)</code>, where <code>\$images</code> is an array of of image objects. Each image object has following attributes:
</p>
<dl>
<dt><code>\$src</code></dt>
<dd>
<p>
The path to the image to be shown. If an imagecache preset has been chosen in the block settings, this path will reflect that preset. Otherwise it is the same as the path to uploaded image.
</p>
</dd>
<dt><code>\$path</code></dt>
<dd>
<p>
The path to the originally uploaded image.
</p>
</dd>
<dt><code>\$desc</code></dt>
<dd>
<p>
A description of the image as given in the node's file attachement fieldset.
</p>
</dd>
</li>
</dl>
<p>
The default implementation returns the default theming described in the previous subsection.
</p>
<h2>
Author
</h2>
<p>
Node Image Block 2 is developed by <a href="http://drupal.org/user/16678">Thomas Barregren</a>. The author can be contacted for paid customizations of this module as well as Drupal consulting, installation, development, and customizations.
</p>
<p>
The development of Node Image Block 2 has been sponsored by
</p>
<ul>
<li><a href="http://www.sspa.se/">SSPA</a>, and</li>
<li><a href="http://www.webbredaktoren.se/">Webbredaktören</a>.</li>
</ul>
<h2>
Credits
</h2>
<p>
This module is a complete rewrite of the original Node Image Block developed by <a href="http://drupal.org/user/13164">Mike Carter</a>.
</p>
<h2>
License
</h2>
<p>
Node Image Block 2 !version. Copyright © 2007–!year <a href="http://drupal.org/user/16678">Thomas Barregren</a>.
</p>
<p>
Node Image Block 2 is free software; you can redistribute it and/or modify it under the terms of the <a href="http://www.gnu.org/licenses/gpl.html#SEC1">GNU General Public License</a> as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
</p>
<p>
Node Image Block 2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the <a href="http://www.gnu.org/licenses/gpl.html#SEC1">GNU General Public License</a> for more details.
</p>
<p>
You should have received a copy of the <a href="http://www.gnu.org/licenses/gpl.html#SEC1">GNU General Public License</a> along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</p>
EOT;
$version = str_replace(array('$Re'.'vision:', ' $'), array('', ''), '$Revision: 2.x $');
$year = substr('$Date: 2008/01/03 00:00:00 $', 7, 4);
return t($help, array('!version' => $version, '!year' => $year));
}