I have a simple block that is used in the footer of a multiple language website. The text in the block changes based on user language so I using the following render attributes, which seems to work well.
$render['#cache'] = [ 'contexts' => ['languages'] ];
This block contains an image that is included with the following:
if( (date('Y') %2) == 1)
$markup = '<img src="/themes/THEME/images/footer_odd_year.jpg">';
else
$markup = '<img src="/themes/THEME/images/footer_even_year.jpg">';
One image is used for even numbered years and a second image is used for odd numbered years.
What is the correct way to setup caching base on this image changing once per year?
I have a similar situation with the home page of the website which changes each day. My current plan is to clear all caching levels for the website early each morning just after mid-night. This includes the Drupal caches, memcached and AWS Cloudfront. For pages like the home page, which change daily, I will need to keep max-age short to limit caching in the user's browser.
Is there any better way?
$build['#cache']['contexts'][]
(if it seems to work nonetheless, this is because the language context is one of the three default contexts). – 4k4 Nov 19 '17 at 21:35if( (date('Y') % 2) == 1)
use odd year image else use even year image. – Greg Sims Nov 20 '17 at 15:44