2

How can I delete the automatic attributes that Drupal 7 puts on images?

4

1 回答 1

13

使用 Drupal 7,您只需要实现hook_preprocess_image(),因为每个主题功能都会执行预处理功能,而不仅仅是使用模板文件的功能。在您的情况下,以下代码应该足够了。

function mymodule_preprocess_image(&$variables) {
  foreach (array('width', 'height') as $key) {
    unset($variables[$key]);
  }
}

由于$variables['attributes']还包含图片属性,下面的代码比较完整。

function mymodule_preprocess_image(&$variables) {
  $attributes = &$variables['attributes'];

  foreach (array('width', 'height') as $key) {
    unset($attributes[$key]);
    unset($variables[$key]);
  }
}

mymodule替换为您的模块/主题的短名称。

当您需要更改传递给主题函数/模板文件的变量时,最好使用预处理函数。仅当您需要更改它们返回的输出时,才应覆盖主题函数。在这种情况下,您只需要更改变量,因此无需重写主题函数。使用预处理钩子,您的代码将与未来的 Drupal 版本兼容。

于 2012-05-29T18:56:32.390 回答