0

I have this in my template:

<img src="{{ site_logo }}" alt="{{ 'Home'|t }}" />

I wrote a preprocess function to override site_logo:

function theme_preprocess_block(&$variables) 
{
    if (siteMode() == 1) 
    {
        $variables['site_logo'] = '/_logo1.png';
    }
    else {
        $variables['site_logo'] = '/_logoOther.png';
    }
}

However, if I clear cache when siteMode == 1, it shows logo1.png. But then if site_mode changes to 2, it doesn't show logoOther unless I again clear the cache. This leads me to believe it caches the resulting HTML. Which makes sense, I guess, but then how would I be able to do something like the above?

I also tried adding my own Twig Extension and then editing block--system-branding-block.html.twig like this:

{% if get_site_mode() == 1 %}
    <img src="/_logo1.png" alt="{{ 'Home'|t }}" /> 
{% endif %}
{% if get_site_mode() != 1 %}
    <img src="/_logoOther.png" alt="{{ 'Home'|t }}" /> 
{% endif %}

This also has the same result. It always shows the one until I clear cache.

Can someone please steer me in the right direction. What should I be using to accomplish what I want? which is to simply switch the logo based on the current mode. Where current mode is determined by a global function called "getSiteMode()".

Any advice?

UPDATE

I tried "set", which worked in html.tpl.php, but not in this template file i am currently working on. This leads me to believe the issue is that I need to disable caching for a specific template?

Anyone know how to do that?

  • What is inside getSiteMode()? – vijay Apr 24 '17 at 9:17
  • Just to understand, what code is siteMode() using? – kiamlaluno Apr 24 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.