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)

asked Mar 18, 2019 at 16:02

2 Answers 2

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.

answered Mar 25, 2019 at 21:29
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.

answered Mar 26, 2019 at 14:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.