0

I've added a custom module function to allow the is_front conditional to work inside of a block template. The purpose is to display the site slogan on the homepage, but not across the rest of the site. The rule works correctly, but the site's cache is causing it to render incorrectly. If I clear the cache from the front page, the slogan displays sitewide. If I clear the cache from another site page, then the slogan doesn't display on the front page.

I'm presuming it is because the block's display is being cached in some way. Is there a simple way around this other than adding a second instance of the block?

1

1 Answer 1

1

Here are some solutions I can think of:

Solution 1: Use CSS (easiest)

Make sure the slogan has some classname you can refer to. Example: site-slogan. Use body classes to change slogan visibility. Here's some rough CSS to get you started.

/* Hide slogan for all pages */
#block-site-branding .site-slogan {
  display: none;
}
/* Show slogan for front page */
body.path-frontpage #block-site-branding .site-slogan {
  display: block;
}

Solution 2: Use 2 block instances (not recommended)

  • Setup one block instance, say block-site-branding-front for the home page and configure it to show the slogan. Make this block visible only for <front> page.
  • Setup another block instance, say block-site-branding-inner for the inner pages and configure it to hide the slogan. Make this block visible for all but <front> page.

You can also define a custom block in a custom module, but that would be an overkill. I think the first solution should do the trick.

5
  • This is what I ended up doing. However, as it's going to be a high traffic site I figured that stopping it from rendering might be helpful. Any idea if that should be a scalability consideration or if it really doesn't matter?
    – Mrweiner
    Apr 14, 2017 at 17:15
  • Naa! That's just one line showing a slogan. Shouldn't cause a bit of trouble. You can simply hide it with CSS.
    – Jigarius
    Apr 14, 2017 at 17:26
  • Save the kilobytes, save the world
    – Mrweiner
    Apr 14, 2017 at 20:45
  • Your other option would be to prevent the block from being cached completely (has to be built on every site then, but that's a fairly optimized use case with dynamic page cache), you'd do that by setting #cache max-age = 0 somewhere in preprocess. Solution 2 will add the url.path cache contexts, which means the block would be cached differently for every URL, which is very inefficient. A more efficient approach would be to use the url.path.is_front cache context that is new in 8.3: drupal.org/docs/8/api/cache-api/cache-contexts
    – Berdir
    Apr 15, 2017 at 13:22
  • Thanks Berdir for the explanation. That is why I mentioned "not recommended" in the second solution and the CSS solution was chosen.
    – Jigarius
    Apr 15, 2017 at 17:56

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.