New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML syntax to change block's template #3356

Open
cescopag opened this Issue Feb 10, 2016 · 13 comments

Comments

@cescopag

cescopag commented Feb 10, 2016

Summary

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

Examples

Proposed solution

@shiftedreality

This comment has been minimized.

Member

shiftedreality commented Mar 10, 2016

Hi @cescopag

You provided correct way with action method
Looks like mistake in public docs

@almarchenko

This comment has been minimized.

almarchenko commented Mar 14, 2016

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 deprecated. I'll add a note in docs, and updated them when the bug is fixed.

@LiamFielding

This comment has been minimized.

LiamFielding commented Aug 22, 2016

Is this now fixed?

@dnadle

This comment has been minimized.

dnadle commented Aug 23, 2016

In 2.1.0 I still have to use 'action' and the online documentation is still wrong.

@LiamFielding

This comment has been minimized.

LiamFielding commented Sep 9, 2016

@digitalpianism No, sorry - I believe that's what we all tried first. At least I did, that's how I ended up here.

@digitalpianism

This comment has been minimized.

Contributor

digitalpianism commented Sep 9, 2016

@LiamFielding my bad. I guess we gotta wait ^^

@vzabaznov vzabaznov self-assigned this Oct 4, 2016

@diglin

This comment has been minimized.

Collaborator

diglin commented Nov 2, 2016

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 generateBlockmethod, 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:

  • actions
  • setTemplate via attribute template
  • arguments

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;
    }
@nsergo

This comment has been minimized.

Collaborator

nsergo commented Mar 16, 2017

Issue in progress @nsergo & @sambolek

@magento-team

This comment has been minimized.

Contributor

magento-team commented Jul 31, 2017

Internal ticket to track issue progress: MAGETWO-66248

@magento-engcom-team

This comment has been minimized.

Contributor

magento-engcom-team commented Sep 20, 2017

Closing the issue as the feature request. Please, see more details here #8913 (comment)

@tmotyl

This comment has been minimized.

tmotyl commented Sep 20, 2017

@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.

@magento-engcom-team magento-engcom-team moved this from TODO to Done in branch [2.3-develop] Sep 20, 2017

@DanielRuf

This comment has been minimized.

Member

DanielRuf commented May 8, 2018

This issue still exists. We should reopen it.

@engcom-backlog-pb

This comment has been minimized.

engcom-backlog-pb commented Sep 13, 2018

@cescopag, thank you for your report.
We've acknowledged the issue and added to our backlog.

@engcom-backlog-pb engcom-backlog-pb removed their assignment Sep 13, 2018

zetlen pushed a commit to magento-research/magento2 that referenced this issue Oct 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment