如何将类添加到imagefield渲染的img中?

时间:2011-07-06 10:55:40

标签: drupal drupal-6 imagefield

我正在使用Imagefield上传图像,我想将一个类添加到<img src="imagefile" class="caption" />,因为没有为Imagefield渲染的图像定义类。

Drupal 6有可能吗?

2 个答案:

答案 0 :(得分:0)

您可以覆盖 theme_imagefield_image()功能,方法是将其复制到template.php 将其重命名为MYTHEME_imagefield_image()。这有点像:

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 .' />';
}

...在 template.php

清除缓存!产品:&gt;

修改

如果您使用的是Imagecache模块,那么您将覆盖 theme_imagecache()功能,最后用:

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 .' />';
}

...在 template.php 文件中。

答案 1 :(得分:0)

你到底想要做什么? 如果您尝试使用css为imagefield字段设置输出样式,您可以使用我之前假设的前一个div的类。所以如果你的html看起来像这样(我的,使用imagefield和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>

在你的css文件中使用这样的方式来设置你的图像样式(这个例子将图像放在彼此旁边而不是彼此之下,创建某种类型的图库)。

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