Hello everybody.
I'm a Drupal newbie and after four manuals read and many days of unsuccessful attempts I'm turning to you looking for help.

I want to define a function in my template.php and then use it in my node template. My subtheme is working and I can do everything flawlessly but this task.

Some time ago I found this tutorial http://www.signalkuppe.com/2007/06/20/gestire-le-immagini-con-drupal/ about building a gallery with imagecache and thickbox.

This is the code inside template:

 <div class="content">
  <div class="reportages">
   <p> <?php print theme('preview', 'copertina', $field_copertina[0]['filepath']); ?></p>
   <p><?php print $node->field_testo[0]['view'] ?><p>
   <p><strong>Altre immagini</strong>
   <hr />
   <br />
   <div class="miniature">
   <?php foreach ($field_immagine as $image) {
    print theme('gallery', 'thumbnail', $image['filepath']);
    }?>
   </div>
   </div>
   </div>

....inside template.php

function mytheme_gallery($namespace, $path, $alt = ' ', $title = ' ', $attributes = NULL) {
$attributes = drupal_attributes($attributes);
$imagecache_path =  file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
return '<a href="'. check_url(file_create_url($path)) .'" class="thickbox img-'.
$namespace .'" rel="immagine">
<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'"
class="thickbox" title="'. check_plain($title) .'" '. $attributes .' /></a>';
}
 
function mytheme_preview($namespace, $path, $alt = ' ', $title = ' ', $attributes = NULL) {
$attributes = drupal_attributes($attributes);
$imagecache_path =  file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
return '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'"
class="thickbox" title="'. check_plain($title) .'" '. $attributes .' />';
}

"galleria" and "thumbnail" are imagecache presets (to find imagecache path, right?)

I cleared cache and followed all the advices from my 4 manuals.
I tried to build a simpler function with no success.

My question is: what's the right way to do that?

Function side:
mytheme_functionname (variables coming from theme function){ ...}
mytheme_preprocess_functionname (variables coming from theme function){ ...}

TEmplate side
print theme(functionname, variables sent to function called)

Where am I wrong?

Thank you very much for any help you will give me

Comments

erik’s picture

Isn't what you're trying much easier to achieve by creating a view for it?
By using views instead of code, adding new features later on is easier as well.
Sorry I could not help you with the code-part.

il drugo’s picture

Thank you! I chose this way because I though it was more transparent. I could use views, but there are other functions I need to write and if I can't understand what's wrong the problem will be there again very soon.
I hope somebody can tell me what's my mistake...

nevets’s picture

The simple approach to doing this in Drupal 6 is to use an image field that allows multiple image, image cache for thumbnails (and full size image) plus lightbox 2. While the default output places the images vertically, a little css can be used to place them side by side.

Or as the previous comment mentions views could be used (there are a number of modules to support this).

As for why the code does not work I suspect the code is for Drupal 5 and would require changes for Drupal 6.

il drugo’s picture

Thank you nevets,
I'm using imagecache, I created the presets used by the functions.
I was planning to use lightbox2 but tagging (automatic option gives me some problem) the images would probably require a function.
I really need to understand how to define and use a function. I studied drupal 6 theme flow: themeName_hook($arguments) should be the right thing and template.php inside my subtheme folder should be the right place...
I can't undersand whether the code is wrong or for some reason I can't intercept the hook.

roper.’s picture

Any theme function must first be added to Drupal's theme registry via hook_theme(). That way, Drupal is aware of them and is able to interpret overrides (ex. mytheme_gallery() is an override of theme_gallery(), which in this case hasn't been defined).

Read through that API page, but basically somewhere in your template.php you want:

function mytheme_theme($existing, $type, $theme, $path) {
  return array(
    'gallery' => array(
      'arguments' => array('namespace', 'path', 'alt' => ' ', 'title' => ' ', 'attributes' => NULL),
    ),
    'preview' => array(
      'arguments' => array('namespace', 'path', 'alt' => ' ', 'title' => ' ', 'attributes' => NULL),
    ),
  );
}

That registers 'gallery' and 'preview' (or rather 'theme_gallery()' and 'theme_preview()') as valid, overridable functions. You'll have to clear the cache of course.

Now, having said all that, imagecache provides a theme function for printing images, why not just use those?

print theme('imagecache', 'thumbnail', $image['filepath']);
il drugo’s picture

Thanks roper.
You're right. I have to cycle every image of my multiple imgfield of the node, then tag them for thickbox or better lightbox2. That's because I think imagecache isn't enough for me.

I was aware of theme_hook but (I think) I read everywhere that drupal 6 automatically collects hook from template.php this way: http://drupal.org/node/223430

I also read somewhere that a function can't return something but just ass variales by reference and add it to the variable array available to templates for page building.

Does something of my thoughts makes sense to you?

roper.’s picture

The page you linked to is for preprocessors, which are almost exclusively for defining variables to be directly used in a .tpl file. And correct, you don't return anything in a preprocessor, simply modify/add to the variables array.

But you're not defining variables for use in a .tpl, you're making custom theme functions, in which case you need to implement hook_theme.

As for imagecache, it looks like you're trying to add a class to the image to trigger thickbox/lightbox..? You can do that with theme('imagecache'). See the page I linked for theme_imagecache, you can pass an attributes array. So you could do:

print theme('imagecache', 'thumbnail', $image['filepath'], '', '', array('class' => 'thickbox'));
il drugo’s picture

This is so eye-opening.
So while variables are automatically preprocessed

function mytheme_preprocess_page(&$vars) {
$vars['random_number'] = rand(1, 100);
}

<?php print $random_number; ?>

functions are instead to be registered in the hook system.(this is an override, but it's the same for any function, right?)

function theme_breadcrumb($breadcrumb) {
if (!empty($breadcrumb)) {
return '<div class="breadcrumb">'. implode(' » ', $breadcrumb) .'</div>';
}
}

print theme('breadcumb', $breadcumb)

Are you sure about that? Thank you!

roper.’s picture

Only theme functions need to be registered because they're run through THE theme() function, whose first argument is the hook. Drupal needs to be aware of these functions and their arguments. You don't, of course, need to register a standard regular old function. For example, in your original post, you COULD in theory do something like this:

  print mytheme_gallery('thumbnail', $image['filepath']);

because mytheme_gallery() exists as a standalone function in your template.php. But the whole purpose of theme functions is that they're overridable, which means they must be registered and called via theme().

:-)