getChildHtml, $useCache and child containers#4919
Closed
bentideswell wants to merge 7 commits intomagento:developfrom
bentideswell:develop
Closed
getChildHtml, $useCache and child containers#4919bentideswell wants to merge 7 commits intomagento:developfrom bentideswell:develop
bentideswell wants to merge 7 commits intomagento:developfrom
bentideswell:develop
Conversation
Contributor
Author
|
I have now signed the CLA. |
Contributor
Author
|
For anyone else who is currently suffering with this issue, I have the following temporary solution so that you can keep developing. In the block that you're calling getChildHtml from, add the following code: |
|
Closed as PR does not have changed files |
Member
|
@bentideswell currently struggling with this bug as well. I'll try to free up some time to fix this issue. |
Contributor
Author
|
@tdgroot the fix posted above should resolve the issue. I've restructured my code not to need it but before I did this, the above worked well. |
4 tasks
Member
|
@bentideswell I've created a pull request: #14029 |
This was referenced Mar 20, 2018
This was referenced Jun 30, 2018
magento-engcom-team
pushed a commit
that referenced
this pull request
Oct 30, 2019
[TSG-CSL3] For 2.3 (pr35)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When calling the following method, the $useCache parameter is not applied to child containers, meaning that any blocks that are part of a child container are always taken from the cache, regardless of the initial value of $useCache.
\Magento\Framework\View\Element\AbstractBlock::getChildHtml($alias = '', $useCache = true)
This occurs because the \Magento\Framework\View\Layout::renderNonCachedElement method does not take $useCache as a parameter. This method then calls _renderContainer, which also doesn't take $useCache as a parameter. When _renderContainer then calls renderElement, although renderElement can accept the $useCache parameter, the value has been lost and the $useCache value is not passed to this method. As $useCache has a default value of true, blocks that are child to the container (that is child to the original block) always come from the cache, even if the developer specifically stated not to use the cache in the first call to getChildHtml.
To reproduce this, setup a layout that has the following:
You will need to change Vendor_Module for the correct path to the template files in the above XML.
In block1.phtml, add the following code:
In block2.phtml, add the following code:
For the first round of calling getChildHtml, the second parameter is skipped so the default value of true is used. The expected output is that the random number will be the same each time as after the first call, the result will be loaded from the cache.
On the second round of calling getChildHtml, the second parameter is set as false so the expected result would be that the output would be different each time. This is not what happens though and the same value is returned each time.
If you try this again with my changes, when $useCache is set to false, the output is different each time, meaning the cache is skipped for all child blocks.
This is my first pull request but hopefully it is a useful one. It's a sticking point for a project I am working on so I am happy get this merged as quick as possible!