magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.56k stars 9.32k forks source link

Layout gets initialized twice if ViewInterface is not injected into Controller #33329

Closed mfickers closed 3 years ago

mfickers commented 3 years ago

I was working on a feature including a custom product list page, including filters for category and product attributes. For some reason, every active filter element got duplicated during rendering.

Through debugging I found out, that the method Magento\RequireJs\Block\Html\Head\Config::_prepareLayout would trigger a second build of page layout, resulting in every block being initialized twice. Since Magento\LayeredNavigation\Block\Navigation::_prepareLayout isn't idempotent, every active filter got added a second time.

It seems like the controller and the RequireJs block for some reason end up with separate instances of either the \Magento\Framework\View\Page\Config or the \Magento\Framework\View\Layout\Builder classes in their dependencies, because they have flags that should prevent a second build of the page layout.

I could prevent this behavior by injecting Magento\Framework\App\ViewInterface into my controller, though this is a deprecated class.

Preconditions (*)

Magento version is 2.4.1, but I suspect this bug exists ever since controller aren't required to extend the default Action anymore.

Steps to reproduce (*)

  1. Create a custom Controller rendering a page, make sure not to extend \Magento\Framework\App\Action\Action
  2. Inject Magento\Framework\View\Result\PageFactory
  3. In the execute method, return $this->pageFactory->create();
  4. Include a custom block in page layout, that implements the _prepareLayout method

Expected result (*)

  1. _prepareLayout should only be called once

Actual result (*)

  1. _prepareLayout is called twice

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @mfickers. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

mrtuvn commented 3 years ago

Tuna_Tweak.zip Try to reproduce your same scenario with my own custom module but seem result being opposite. Log file only show one. I have place logger to method _prepareLayout (custom block) to write log text to file Can you check that module fit same your case if you have a spare times ? I have tested my scenario in latest codebase magento

custom controller implements ActionInterface in layout page append custom block to content via xml this block extend (\Magento\Framework\View\Element\Template) clear cache and re-visit page by go to url controller by browser Feel free correct me if i'm doing wrong something

mfickers commented 3 years ago

I copied your module into the same project and there are two log messages for a single request, but I couldn't recreate the behavior on a fresh Magento 2.4.1 installation. Looks like this is caused by third party code.

I'm closing this issue, thank you for your time.