I am uploading an image with imagefield and I want to add a class to Only local images are allowed..

Is this possible? Because I didn't find anything on how to do this with several hours of crawling.

Comments

himagarwal’s picture

I want to add a class to <img src="imagefile" class="caption" />

I didn't put code tags in my earlier post.

quicksketch’s picture

You can theme the output of ImageField by overridding theme_imagefield_item() and/or theme_imagefield_image in your theme. If you want to include a field for adding your custom class on a per-image basis, look into ImageField Extended.

himagarwal’s picture

Thank you for the reply.

I don't know php coding so I will not be able to theme that, however I already tried imagefield extended module but it didn't added class="caption" to the image generated by imagefield rather it added a label called "class" with value as "caption" on the body, similar to other cck text field.

I thought imagefield extended module was meant to add other html attributes like: "border", "align", "class"; to <img /> generated by imagefield but it didn't.

Another problem with imagefield extended module is that it is not compatible with imagecache + lightbox2 as I am using these two modules for image generated by imagefield.

The only thing that worked was when I added class="caption" to the function theme_imagecache generated by imagecache module in template.php, but the problem is that class="caption" is showing for each and every imagecache presets.

function theme_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $absolute = TRUE) {
  // Check is_null() so people can intentionally pass an empty array of
  // to override the defaults completely.
  if (is_null($attributes)) {
    $attributes = array('class' => 'imagecache imagecache-'. $presetname);
  }
  if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
    $attributes['width'] = $image['width'];
    $attributes['height'] = $image['height'];
  }

  $attributes = drupal_attributes($attributes);
  $imagecache_url = imagecache_create_url($presetname, $path, FALSE, $absolute);
  return '<img class="caption" src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}

Is it possible to make this code work for only one or two pre-defined imagecache presets?

quicksketch’s picture

First of all DON'T HACK MODULES. Theme functions are intended to be overridden in your template.php file. If you can hack a module to add a class, you can easily override the output in template.php by following a few simple steps. Hacking modules will make your site impossible to upgrade in the future when new versions of the modules come out, making your site vulnerable to security vulnerabilities and unable to receive bug fixes.

See the handbook on beginning theming: http://drupal.org/node/457740

ImageField Extended only adds fields on input, it does not affect output unless you override the theme functions for outputing.

You can theme some ImageCache presets but not others by overridding theme_imagecache_formatter_default() in template.php.

quicksketch’s picture

See identical request at #980634: Overriding theme_imagecache_formatter_default, which handled it properly.

himagarwal’s picture

I again tested the imagefield extended module because you said it will add input fields like: "align" etc. in the img tag rendered by imagefield but it didn't show up anything, after disabling the imagefield override.

I even tested this with garland theme and still nothing shows up in the imagefield input. Don't know why? Is there some steps that I'm missing?

Because if imagefield extended module does what you said then it would be the best way.

pitxels’s picture

very sad about not finding the class adding in the UI, the module is almost perfect for its porpuse