I have created a custom module that declares a custom image formatter. Now I need to call the Core image module preprocess function 'template_preprocess_image_formatter'. How would I do that?

I tried to add it in the list of preprocess functions of my custom theme but it doesn't fire unless I clear the cache:

  * Implements hook_theme().
 function foo_theme() {
    'image_formatter_foo' => [
          'variables' => ['item' => NULL, 'item_attributes' => NULL, 'image_style' => NULL],
          'file' => 'foo.field.inc',
          'preprocess functions' => [

1 Answer 1


Looking at template_preprocess_image_formatter, I would try the following code below.

Replace mymodule with the machine name of the module. This assume foo is the machine name of your image formatter.

mymodule_preprocess_image(&$variables) {
  /* processing logic */

mymodule_preprocess_image_formatter_foo(&$variables) {
  /* processing logic */

I also recommend reviewing Drupal 8's Core Image module as it has some of the files that call the hooks you need.

  1. image.field.inc contains template_preprocess_image_formatter
  2. template_preprocess_image
  • @EricMorand did the answer work for you? If not let me know so i can make any edits to help resolve the issue. Jun 1, 2018 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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