如何将HTML属性添加到CCK图像字段

jos*_*eon 1 drupal drupal-7 drupal-theming

我正在尝试将"id ="background"的属性添加到实际的图像标记中.

<?php print render($content['field_bg_image']); ?>
Run Code Online (Sandbox Code Playgroud)

我查了很多文章,他们都关注hook_form_alter,这不是一个表格.这只是一个图像字段,我只需要一个带有背景值的ID属性.我知道我可以使用javascript但我想使用Drupal而不是添加任何更多的javascript.

Cli*_*ive 5

运行图像渲染theme_image_formatter(),不允许您设置属性.

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

$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']);
Run Code Online (Sandbox Code Playgroud)

这是未经测试的,所以如果您有任何问题请告诉我