I’m still learning how Drupal (6) puts things together. We have a situation where we have a page that has an image attached to it. When we view the page, we need the full path to the image that’s attached. You would think it would be pretty simple. You would expect that somewhere in the $node object is a url for the image that is attached. But that’s just not the case.
Drupal calls the page a node. It also calls the image a node. You have a table that says “this node is attached to that node” by ids. Specificially, image_attach.nid is your page, and image_attach.iid is the node id of the image. Then you have an image table, which lists the images by node id, with a size, and a file id. (Confused yet?) We use image_attach.iid and the size we’re looking for to get image.fid. And then there’s one last table, which is files, which lists by id, and has the actual filepath. We can use image.fid to get the files.filepath we want.
So I wrote a function to do all of that nonsense for me. You pass it the node id of the page which has the image attached to it, and it returns the path/to/the/attached/image/thank/you/very/much.jpg.
function lookupImagePath($nid, $thumbnail = false) {
if ($thumbnail) {
$size = 'thumbnail';
} else {
$size = '_original';
}
$sql = 'SELECT f.filepath ';
$sql .= 'FROM image_attach a, image i, files f ';
$sql .= 'WHERE a.iid = i.nid ';
$sql .= 'AND i.fid = f.fid ';
$sql .= 'AND a.nid = ' . $nid . ' ';
$sql .= 'AND i.image_size = "' . $size . '" ';
$res = db_query($sql);
$row = db_fetch_array($res);
$filepath = $row['filepath'];
return $filepath;
} // end lookupImagePath
Boy Baukema writes:
when using the term ‘evil’ for a technical solution it means that it ‘does harm to your aesthetic and engineering judgment’.
I’m not pointing any fingers…
Common problem when building web pages. You want a DIV that’s the same height as the browser window, but you don’t know what the height of the browser window is. It changes from user to user. Well, jQuery to the rescue.
var browser_window_height = 0;
$(document).ready(function() {
// this sets the height of the div on init
$('#fullscreen').each(
function() {
browser_window_height = $(window).height();
$('#info').text(browser_window_height.toString());
$(this).css('height', browser_window_height.toString() + 'px');
}
);
// this says, if they resize the window, change the height of the div
$(window).resize(function() {
var new_browser_window_height = $(window).height();
if (new_browser_window_height < browser_window_height) {
// if the new window size is smaller, resize quickquickquick
$('#fullscreen').css('height', new_browser_window_height.toString() + 'px');
} else {
$('#fullscreen').animate({
'height' : new_browser_window_height.toString() + 'px'
}, "slow");
}
browser_window_height = new_browser_window_height;
$('#info').text(browser_window_height.toString());
});
});
View the page (and full source) here.
I’m working in Drupal 6 and we have a page template page.tpl.php, which handles most everything. It has a bunch of stuff for the header, and it handle the sidebar on the left, and the content in the middle of the page. The content in the middle of the page comes from whatever node you’re viewing, and it gets run through node.tpl.php first. This formats the node for output. Thing is, I have a particular node (with node ID 77) that I want to be formatted differently.
Drupal says you can make a new template called page-node-77.tpl.php and format the node content accordingly. But I don’t want to have two templates that display the same stuff (like the header and the sidebar on the left). All I want to do is change the node template. What I want to be able to do is make a new file called node-77.tpl.php and have that be used instead of node.tpl.php.
After much research, I find that includes/theme.inc has the function template_preprocess_node, which is where I want to put my code. At the end of this function is
$variables['template_files'][] = 'node-'. $node->type;
This allows us to define template files for nodes by type, but not by ID. So add
$variables['template_files'][] = 'node-'. $node->nid;
and you’re all set.
If your $100,000 CMS is written in Java and only runs on Windows, you lose.