如何向CCK图像字段添加HTML属性 [英] How to add an HTML attribute to a CCK image field

查看:95
本文介绍了如何向CCK图像字段添加HTML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向实际图片标签添加id =background的属性。

I am trying to add an attribute of "id="background" to the actual image tag.

<?php print render($content['field_bg_image']); ?>

吨的文章,他们都关心hook_form_alter,这不是一个表单,这只是一个图像字段,我只需要一个ID属性,它的背景值,我知道我可以使用javascript,但我想使用Drupal并没有添加任何更多的javascript。

I've looked up tons of articles and they are all concerned with hook_form_alter and this is not a form. This is simply just an image field, I just need an ID attribute on it with a value of background. I know I can use javascript but I want to use Drupal and not add any more javascript.

推荐答案

图像渲染是通过 theme_image_formatter() 不允许你设置属性。

The image render is run through theme_image_formatter() which doesn't let you set attributes.

你可以通过手动构建图像来解决这个问题:

You can get around this by building the image up manually:

$img = theme('image', array(
  'path' => $content['field_bg_image'][0]['#item']['uri'],
  'alt' => $content['field_bg_image'][0]['#item']['alt'],
  'attributes' => array('id' => 'background')
));

$content['field_bg_image'][0] = array(
  '#markup' => $img
);

echo render($content['field_bg_image']);

这是未经测试的,所以让我知道,如果你有任何问题

That's untested so let me know if you have any problems

这篇关于如何向CCK图像字段添加HTML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆