Hello,
I have a cck image with display : presset image linked to image
I copied function theme_imagecache_formatter_imagelink($element) in my themes folder and rename it basic_imagecache_formatter_imagelink($element).
I added attribut target = _blank, in the good way to open image in a new window.
It's not working.
It works if I hack directly imagecache module, same function.

Why does not it work in my theme?
I must add I did same for filefield and it worked.

Comments

sahuni’s picture

Component: imagecache_image module » Code
sahuni’s picture

Wwhen I click on presset, the bigger size image must open in a new window.
Is it OK to use theme_imagecache_formatter_imagelink in my template.php ?
It's not theme_filefield_file?

My new fuction in template.php is the following (I added ",target=> _blank" in last line)

function theme_imagecache_formatter_imagelink($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }

  // Extract the preset name from the formatter name.
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
  $style = 'imagelink';

  $item = $element['#item'];
  $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : '';
  $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL;

  $imagetag = theme('imagecache', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title']);
  $path = file_create_url($item['filepath']);
  $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}";
  return l($imagetag, $path, array('attributes' => array('class' => $class,'target' =>"_blank"), 'html' => TRUE));
}

Can you tell me why it does not work?

sahuni’s picture

I don't really understand because devel themer info gives theme_imagecache() function, but sensible code is in function theme_imagecache_formatter_imagelink.
At what level must I change something?

sahuni’s picture

In fact, I needed to add presset in my function name

babbage’s picture

I am having the identical problem, but have not solved it yet. Can you please explain exactly what you did to solve it? Normally, in a function like this I would replace the word "theme" with the name of my theme. Did you do this? Do you also have to put the name of your imagecache preset in the function name as well? If so, where exactly do you put it? I have tried multiple ways to combine these but it is not working... like you, changing the original function does exactly what I want so I know it isn't the code that is bad.

babbage’s picture

OK. Have figured this out. For any other poor soul who treads this same path, here is how you have to name the function:
MYTHEMNAME_imagecache_formatter_MYPRESETNAME_imagelink()

Personally a) this doesn't seem very obvious and b) it doesn't seem to be documented anywhere. Would be nice if it was. I'll try and travel back these ways and do that sometime in a more formal way.

Incidentally, it would appear this will not work if you use underscores in your preset names, and I suspect hyphens may be a problem as well although I haven't tested that and would be pleased to be wrong there.

alison’s picture

I agree with most of what's been said here. HOWEVER... in the meantime, I thought I'd share what I've come up with -- using this discussion and others -- in case it's helpful for anyone.

A little context: I have an image nodetype (using imagefield/filefield, not Image module) and I've created a View that uses the Lightbox2 trigger field thing so that I can have a gallery of thumbnails that link to large versions of the photos, with the images' titles, captions, etc. underneath (in the lightbox), along with the "next/previous/etc" lightbox functionality.

I wanted users to just have to enter a node title, an image file, and caption text for each image node. For uninteresting reasons, I ended up using the "title" field (part of the imagefield's "data") for the caption text.

Bottom line -- I wanted to use the node Title for the alt-text and "title" attributes of the images when they were displayed in the gallery (thumbnail and large versions). So, this is what I added to my template.php:

function MYTHEMENAME_imagecache_formatter_MYPRESETNAME_default($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }

  // Extract the preset name from the formatter name.
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
  $style = 'linked';
  $style = 'default';

  // the following was inspired by
  // theme_imagefield_image_imagecache_lightbox2 function
  // (in lightbox2.formatter.inc)
  $node = node_load($element['#item']['nid']);
  $nodetitle = $node->title;

  $item = $element['#item'];
  // $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : $nodetitle;
  $item['data']['alt'] = $nodetitle;	// because we aren't making users enter alt text
  // $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL;
  $item['data']['title'] = $nodetitle;	// because we're using the title field for the caption
  										// text, which is way too long for this attribute

  $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}";
  return theme('imagecache', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title'], array('class' => $class));
}

(And then again for my large-size preset.)

Anyway, like I said, hopefully this is helpful for someone! Let me know if I can provide any additional info/details.

fizk’s picture

Status: Active » Closed (fixed)

Please reopen if this is still an issue with ImageCache 6.x-2.0-rc1.