0

I'm building a custom module to extend the default image field. It adds additional attributes that relate to the image field on a per content type basis. At this point it will take and store the attributes => values and when calling hook_preprocess_field() they can be returned (printed) so I can rest assured that they are indeed storing and relating correctly. The final part of the puzzle is to assign my twig template and insert my custom attributes. I thought that hook_theme() would do the job but so far I have failed and I can't find any suitable documentation on the subject when it comes to fields or image fields.

How do I tell Drupal to use my custom module's template when rendering an image that is linked?

6
  • What do you mean by "extend the default image field"? This could be creating a new field type, changing the plugin class to use your own class, making a template for image render types.
    – mradcliffe
    Sep 14 '16 at 19:50
  • 1
    @mradliffe, thanks for chiming in. I wasn't sure if this question would gain any traction. By extending, I mean that I don't want to change the field formatter on the display settings page for the content type. I just want to add options for a few attributes (rel, target, class). This is specific to image fields and specific to the image linking to "file". I have built custom field formatters for other projects but in this case, I am specifically interested in extending the default image field formatter rather than creating a new one. Thanks for any assistance/feedback you can provide. Sep 14 '16 at 21:08
  • 1
    A little more digging shows that the default image formatter is located at: 'core/themes/stable/templates/field/image-formatter.html.twig'. I need to override that and provide my own within my module. hook_theme() doesn't seem to do the job. I may be able to use hook_theme_registry_alter(), providing I can get it to use the path I give it to my module. It may be a little heavy handed though. Sep 14 '16 at 23:26
  • I understand now. If you want to override the class used by a field type, then hook_field_info_alter is the only way. Change the $info['image']['class'] or what not to your new class. And then it needs a drush cr && drush entup I think you don't need to provide an annotation. I haven't tried that before.
    – mradcliffe
    Sep 14 '16 at 23:41
  • 1
    Thanks for that... I've now been looking at the hook_field_info_alter() and it might be useful to change the $info['image'][default_formatter] to my own in its place. I've been looking at a lot of approaches on this so I need to give it some more thought first. Sep 15 '16 at 4:29
0

Answering my own question. The solution was to use: hook_theme_registry_alter()

/** * Implements hook_theme_registry_alter */ function MY_MODULE_theme_registry_alter(&$theme_registry) { $theme_registry['image_formatter']['path'] = drupal_get_path('module', 'MY_MODULE') . '/templates/field'; }

In my module I have the templates/field directory with the template image-formatter.html.twig, so no other changes were necessary.

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.