1

I'm using Drupal 8. I have a content type of “course”. Each course should have a “Banner” block in it that we can upload different background images to (per course).

I've created a “custom block type” of Banner and in this block type I have fields for “desktop image file”, “tablet image file” etc. I then created a “block” called Banner that uses this “custom block type" of Banner. Then, I added the block to the top of the “Content region” and set it to display for the course content type only. I can then see the block on the course pages. So far so good...

However, if I alter the text in the block, it changes the text on all pages... how do I configure it so that I can change the text and images I upload per course/page? Thanks in advance! (Hoping the answer doesn't involve installing extra modules).

  • 2
    Use a Views block and filter the data by a contextual filter. Instead of two places you can also store the banner fields in the content type itself and hide them in the display mode. Then the View would not filter a custom block, but the same content type you have as main display of the page. – 4k4 Jun 21 '18 at 11:55
  • 1
    see this related drupal.stackexchange.com/questions/252373/… – 4k4 Jun 21 '18 at 12:27
0

The trouble with this approach is that there are block definitions and block instances. A block definition is the code or custom block that defines the block's behavior.

A block instance is what you create when you place a block onto your theme and defined its settings (e.g. nodes to display it on, values for custom fields).

To have different banners for each class using this method you would need to create a different block instance for each class. (and set the display rules appropriately).

To use a single block, you will need to define a location outside the block that has the needed information. Probably on your course page. Then you can use either a view block as mentioned in the comments or write a custom block to pull the info. If it is only needed on the main course page, you could probably just use a custom content type twig file and add it when the page is rendered.

If you need the block on subpages, e.g. secondary pages of a course, you will probably have to write a custom block that knows how to relate the subpage to the main course page. E.g. get the course information from a course entity reference field on the subpage and then get the banner info from the main course page).

Note that with both views and custom blocks, you will need to define the cache tags appropriately so that the content is cached per page and not overall.

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.