Documentation claims that it is possible to change a block's template by the following layout update:
<referenceBlock name="navigation.sections">
<arguments>
<argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
</arguments>
</referenceBlock>
But It doesn't work.
Using action
node, instead of arguments
node, works fine:
<referenceBlock name="navigation.sections">
<action method="setTemplate">
<argument name="template" xsi:type="string">Magento_Theme::html/nav_sections.phtml</argument>
</action>
</referenceBlock>
Source:
http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#set_template
Try to change a block's template by the following layout update
e.g.:
Block's template is changed without any errors
Got exception
Hi @cescopag
You provided correct way with action method
Looks like mistake in public docs
Hi @cescopag!
Thank you for reporting this. Looks like there's a bug in magento code, that is why the code sample provided in docs doesn't work. But you found the correct temporary solution.
It must be temporarily as
Is this now fixed?
In 2.1.0 I still have to use 'action' and the online documentation is still wrong.
@digitalpianism No, sorry - I believe that's what we all tried first. At least I did, that's how I ended up here.
@LiamFielding my bad. I guess we gotta wait ^^
The reason why it doesn't work is because the method \Magento\Framework\View\Layout\Generator\Block::generateBlock:
When we want to overwrite an existing block, this one was at first initialized via a layout XML with an attribute template
.
Into the generateBlock
method, arguments
are passed to the block, then setTemplate
is called and then in the calling method process
the action is called.
So the priority is the following:
template
When a block was initialized with its attribute template
, arguments
will have no effect. That's why you need to use actions
protected function generateBlock(
Layout\ScheduledStructure $scheduledStructure,
Layout\Data\Structure $structure,
$elementName
) {
list(, $data) = $scheduledStructure->getElement($elementName);
$attributes = $data['attributes'];
if (!empty($attributes['group'])) {
$structure->addToParentGroup($elementName, $attributes['group']);
}
if (!empty($attributes['display'])) {
$structure->setAttribute($elementName, 'display', $attributes['display']);
}
// create block
$className = $attributes['class'];
$block = $this->createBlock($className, $elementName, [
'data' => $this->evaluateArguments($data['arguments'])
]);
if (!empty($attributes['template'])) {
$block->setTemplate($attributes['template']);
}
if (!empty($attributes['ttl'])) {
$ttl = (int)$attributes['ttl'];
$block->setTtl($ttl);
}
return $block;
}
Issue in progress @nsergo & @sambolek
Internal ticket to track issue progress: MAGETWO-66248
Closing the issue as the feature request. Please, see more details here https://github.com/magento/magento2/pull/8913#discussion_r106549969
@magento-engcom-team the PR you've referencing was also closed, as it required some changes in the pull request code.
So now both this issue and PR are closed but the issue still exists and documentation is still wrong.
In my opinion this issue should stay open until it's fixed.
If you consider this a feature request, please at last fix the documentation.
This issue still exists. We should reopen it.
@cescopag, thank you for your report.
We've acknowledged the issue and added to our backlog.
This is still not solved.
Solution:
if ($block->hasData('template')) { // set template agrument $block->setTemplate($block->getData('template')); } else { if (!empty($attributes['template'])) { $block->setTemplate($attributes['template']); } } if (!empty($attributes['ttl'])) { $ttl = (int)$attributes['ttl']; $block->setTtl($ttl); } return $block;
On Magento 2.3.2 seems to not be an issue any more
This issue still exist.
I tried this code in my customer module's catalog_product_view.xml
it's not working.
Doesn't work in 2.3.3-p1
Does not work in 2.3.5-p2
Hi @ihor-sviziev. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:
Component: XXXXX
label(s) to the ticket, indicating the components it may be related to.2.4-develop
branch@magento give me 2.4-develop instance
to deploy test instance on Magento infrastructure. 2.4-develop
branch, please, add the label Reproduced on 2.4.x
.FYI This issue was resolved by adjusting the docs and highlighting the applied priorities in https://github.com/magento/devdocs/pull/7977
Most helpful comment
@magento-engcom-team the PR you've referencing was also closed, as it required some changes in the pull request code.
So now both this issue and PR are closed but the issue still exists and documentation is still wrong.
In my opinion this issue should stay open until it's fixed.
If you consider this a feature request, please at last fix the documentation.