如何删除 Drupal 7 对图像添加的自动属性?
使用 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 版本兼容。