1

I have written a D8 sub-theme. Base theme is bootstrap, but I don't think it really matters.

My base theme defines a very specific template field--node--field-image--article.html.twig that I would like to ignore, as if it did not exist. I would like to fallback to the next best match template, whatever that is.

I have already considered the following options, but they aren't entirely satisfactory.

  1. Delete the unwanted template from the base theme. However I then have to remember to delete the file again every time I update the base theme.
  2. Override the unwanted template with an identically named file in my sub-theme. In that file %include the preferred template. However this requires me to name a specific preferred template. I would rather let Drupal decide what template would be the best match excluding the unwanted one.
  3. I have discovered the function hook_theme_suggestions_alter. However I'm not sure that it solves my problem because I don't want to lose the ability to use any given suggestion in general - just that one very specific file.

I guess I will end up going with option 1) if I can't find a better idea.

10
  • 2
    Can you unset it from the $suggestions array? Won't it then fallback to the next closest match?
    – Kevin
    Feb 22, 2017 at 16:26
  • Gotta love base themes... Find the default one, and copy it to your theme directory and make the name identical so yours wins. If you ever update your basetheme, that file will be put back and then break your site. Just add the default to your theme and be done :D
    – iLLin
    Feb 22, 2017 at 16:28
  • I like @Kevin's idea better
    – iLLin
    Feb 22, 2017 at 16:29
  • @Kevin yes I was hoping to do that, it is a good thought, and I might end up doing that. But as I understand it, I can't specifically target the base theme - I would lose the ability to have field--node--field-image--article.html.twig in my sub-theme, or any sub-sub-themes (which handle minor variations per site). I can imagine that baffling me in 18 months time.
    – AdamS
    Feb 23, 2017 at 20:15
  • I mean if you want to supply your own twig file with that name, you can - and it will negate the base theme template due to specificity.
    – Kevin
    Feb 23, 2017 at 20:20

1 Answer 1

0

One way to remove the template from being in the list of suggestions, and ultimately not being used, would be to implement hook_theme_suggestions_alter, and unset it from the $suggestions array. This should effectively remove it, and the next specific template will be used instead.

Alternatively, you can copy the template file in from the base theme, and overwrite it to be a default rendering that is preferable to you (without removing the suggestion) and it will be used instead.

1
  • Thanks, that's a good start. I think given the level of confusion in the comment debate, the answer that I accept ought to include specific code (1st para) or exact filenames to copy (2nd para). Your second paragraph sounds similar to what iLLin is suggesting and like my 2nd option that I think doesn't work so well. Probably once there is some specific code to look at we can get to the bottom of it:-) Also I had pointed out a caution that ought to be documented in the answer.
    – AdamS
    Feb 25, 2017 at 10:16

Your Answer

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

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