2

With Drupal 8, the page title is available through a block now. We have several content types where we have an Image field (field_image) and we constantly have to write templates per content type to place the title on top of the image.

It would be nice if we could use the view modes of each content type or some module to do this. Basically, something that does "take the field image, set it as a background, and place the title on top."

Is there a better way to do this programmatically as we cannot find any other way to do this?

  • 1
    You can override node.twig.html and config hidden block title with contentent type. – MrD Feb 10 '18 at 9:00
  • I've made something like this with an override of node.twig.html (as @MrD say) and used the "twig_tweak" to load the block and "twig_field_value" to display the image as background. Hope it helps ! – AlexB Feb 10 '18 at 10:13
  • This can be done using Drupal Views module. See youtube.com/watch?v=FtV78ufp9Jw. There is also the GD Header module. "This module provides a block with the page title and a configurable image in the background". – rwilson Dec 26 '19 at 8:49
1

There is several ways to solve this

1 make your own module

to do what you described

2 add theme templates

for each content type as you described

3 Use background_image module

The module is available for Drupal 8 promises to do just this (I meant to use it but have not actually)

According to the the project description provides a way to insert token values

Token - Provides a way to view which tokens are available to use when adding overlay text for a background image.

So you can probably use it with a field token to accomplish what you need

1

Background Images Formatter is another option for accomplishing this, especially when you might want different images on different pages.

This module provides an image formatter that allows you to set an image in background of a tag. The images are from a field of an entity and not from a configuration page or a custom entity or something else, so it's very easy to setup and manage.

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