开发者

How to add a class to img rendered by imagefield?

I am uploading an image with Imagefield and I want to add a class to <img src=开发者_开发问答"imagefile" class="caption" /> because there is no class defined for images rendered by Imagefield.

Is this possible in Drupal 6?


You could override the theme_imagefield_image() function, by copying it to your template.php and renaming it to MYTHEME_imagefield_image(). It'd be something like:

function MYTHEME_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
  //
  // ... same contents as the original one, except:
  //
  $attributes['class'] = $attributes['class'] ? "{$attributes['class']} my-custom-class" : 'my-custom-class';
  $attributes = drupal_attributes($attributes);
  return '<img '. $attributes .' />';
}

...in your template.php.

And clear the cache! :>

Edit:

If you are using the Imagecache module, then you would overwrite the theme_imagecache() function instead, ending up with:

function MYTHEME_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'];
  }

  // These two lines are what you need 
  $custom_class = 'your-custom-class';
  $attributes['class'] = $attributes['class'] ? "{$attributes['class']} {$custom_class}" : $custom_class;

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

...in your template.php file.


What exactly are you trying to do? In case you are trying to style your output for imagefield fields with css, you could just use the class of a previous div I suppose. So if your html looks something like this (mine does, using imagefield and imagecache):

<div class="field field-type-filefield field-field-images"> <!-- created by imagefield -->
  <div class="field-items">
    <div class="field-item odd">
      <a class="imagecache imagecache-thumbs imagecache-imagelink imagecache-thumbs_imagelink" href="http://yoursite/sites/default/files/originalFile.jpg">
        <img alt="" src="http://yoursite/sites/default/files/imagecache/thumbs/originalFile.jpg">
      </a>
    </div>
  </div>
</div>

In your css file use something like this to style your image (this example puts the images next to each other instead of below each other, creating some sort of gallery).

.field-field-images img
{
  float: left;
  margin-right: 8px;
  margin-bottom: 8px;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜