0

Is there a way to tell drupal that my module has a theme function which usually only works in my theme?

If I write in my theme's template.php:

function mytheme_image_style($variables) {

  // Determine the dimensions of the styled image.
  $dimensions = array(
    'width' => $variables['width'],
    'height' => $variables['height'],
  );

  image_style_transform_dimensions($variables['style_name'], $dimensions);

  $variables['width'] = $dimensions['width'];
  $variables['height'] = $dimensions['height'];

  // Determine the URL for the styled image.

  $variables['path'] = image_style_url($variables['style_name'], $variables['path']);

  // do additional stuff


  return theme('image', $variables);
}

everything works fine. If I move this code to a module it does not work, though:

function mymodule_image_style($variables) {

  // code


  return theme('image', $variables);
}

How can I use the theme_image_style function/hook in my module?

2

You need to use hook_theme_registry_alter() to replace the callback function for the image_style theme. Something like this should do it:

function MYMODULE_theme_registry_alter(&$registry) {
  $registry['image_style']['function'] = 'mymodule_image_style';
}
| improve this answer | |
0

I think you have to use hook_image_styles_alter instead of your function.

Here is an example found on drupal.org:

<?php
function mymodule_image_styles_alter(&$styles) {
  $styles['myimagestyle'] = array(
    'label' => 'My Image Style',
    'effects' => array(
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 500,
          'height' => 300,
          'upscale' => 1,
        ),
        'weight' => 0,
      ),
    ),
    'name' => 'myimagestyle', // You need this and the next line to avoid PHP notices
    'storage' => IMAGE_STORAGE_NORMAL,
  );

  return $styles;
}
?>
| improve this answer | |
  • this will just register/ add my image style as a possible image style to pick from, not quite what i am looking for but thanks :) – Alex Aug 14 '14 at 12:22
  • Sorry I thought you were asking for "How can I use the theme_image_style function/hook in my module?". My answer was "with hook_image_styles_alter". What are you looking for in addition? – Djouuuuh Aug 14 '14 at 12:29
  • how to call the theme_image_style function in a module rather than in my theme's template – Alex Aug 14 '14 at 12:30
  • oh, you want to keep your theme function in template.php but invoke it in a module? – Djouuuuh Aug 14 '14 at 12:45
  • 1
    You're looking for hook_theme_registry_alter' @Alex – Clive Aug 14 '14 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.