Reputation: 254
How can I delete the automatic attributes that Drupal 7 puts on images?
Upvotes: 2
Views: 5660
Reputation: 29689
With Drupal 7, you just need to implement hook_preprocess_image()
, as preprocess functions are executed for every theme function, not just the ones using a template file. In your case, the following code should be enough.
function mymodule_preprocess_image(&$variables) {
foreach (array('width', 'height') as $key) {
unset($variables[$key]);
}
}
Since $variables['attributes']
also contains the image attributes, the following code is more complete.
function mymodule_preprocess_image(&$variables) {
$attributes = &$variables['attributes'];
foreach (array('width', 'height') as $key) {
unset($attributes[$key]);
unset($variables[$key]);
}
}
Replace mymodule with the short name of your module/theme.
Preprocess functions are preferable when you need to alter the variables passed to a theme function/template file. Theme functions should be overridden only when you need to alter the output they return. In this case, you just need to alter the variables, and thus it is not necessary to override the theme function. Using the preprocess hook, your code will be compatible with future Drupal versions.
Upvotes: 12