1

I have a parent block that has so many child blocks inside it. The parent block has these cache rules:

Header:

 $this->addData([
   'cache_lifetime' => 86400,
   'cache_tags' => ['block_beader']
 ]);

In the template header file I print the child like this:

<?php print $this->getChildHtml('main.customer'); ?>

The main.customer block has these cache rules:

public function getCacheTags()
{
    if ($this->_getSession()->isLoggedIn()) {
        $this->addModelTags($this->_getSession()->getCustomer());
    }

    return parent::getCacheTags();
}

The cache crashes in the parent block on Header, the main.customer child block rules are applied only when the Header parent block expires. This behavior is not the desired one, since I would like each block to have its own cache rules, even if nested.

Tips? I assumed that it worked as I thought (similar to Drupal 8)

1

The cache for one block contains the html of that block rendered completely.
This means it does not care about the child blocks it has.
If you want the block to be cached based on some settings in the session, then you need to set the same cache tags as you set on your child block.

On the other hand, you should not cache blocks related to session data.

0

The most simple solution is to add your cache tag rules to parent block (Header), like bellow:

public function getCacheTags()
{
    if ($this->_getSession()->isLoggedIn()) {
       $this->addModelTags($this->_getSession()->getCustomer());
    }

    return parent::getCacheTags();
}

Please notice that you may also need to rewrite Header block if this is not already done.

With this, you are telling the software to create a separated version of header cache if customer is logged in. This is very common on Full Page Cache modules.

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.