Magento2: I cannot move the block

Created on 31 Oct 2016  路  31Comments  路  Source: magento/magento2

Preconditions

Magento 2.4-develop
Php v. 7.2

Steps to reproduce

  1. Open catalog module -> catalog_category_view.xml
  2. Try to move a toolbar outside the block.
<referenceContainer name="content.top">
    <container name="product_list_toolbar_container" htmlTag="div"/>
</referenceContainer>
<move element="product_list_toolbar" destination="product_list_toolbar_container"/>

(product_list_toolbar_container - my container).

Actual result

  1. Fatal error: Uncaught Error: Call to a member function getSize() on null in \vendor\magento\module-catalog\view\frontend\templates\product\list\toolbar.phtml:26 Stack trace: ...

I think I know why the error occurs. The block cannot be found when he outside.

  1. https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Block/Product/ListProduct.php#L215
  2. https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Block/Product/ListProduct.php#L196

move-block

Expected Result

The block successfully moved

Additional information

This is a problem of the system architecture. How I can solve it?

Catalog Confirmed Format is valid Ready for Work P3 ready for dev Reproduced on 2.1.x Reproduced on 2.2.x Reproduced on 2.3.x Reproduced on 2.4.x S3 Dev.Experience bug report

Most helpful comment

@mbernabeu Working on the issue.

All 31 comments

Well, some of the blocks are supposed to be inside other blocks or exist in layout by design. I don't think putting "if block exists" conditions everywhere is a wise idea. Only if product listing without toolbar is a real-world use case such check should be added.

Why do you need to move toolbar block outside? If it's really necessary, you can just render its markup in another place in your HTML and not render a markup in original place.

by design

bad design

I have a custom layout, why do I need.

Also I cannot remove the block. How can I hide it?

<referenceBlock name="product_list_toolbar" remove="true"/>

As you can see from implementation blocks are mixed with collection model and thus some data is obtained from toolbar block and defines model state. In this (poor) implementation product list block does not make sense without toolbar block (which is not just visual representation but also a storage for some collection flags).

You cannot remove block from layout without additional fixes but you can remove it from HTML markup by changing its or product list template.

So, I have to make a crutch.

Exactly, until product list block implementation is decoupled from toolbar you cannot just change layout structure without consequences.

Since it's quite old code it does not seem to me that it's a big problem.

I solved the problem moved my blocks inside the category.products block and now I can move the product_list_toolbar within the category.products where I want.

@springimport thank you for your report.
Please, identify which version of Magento you are running.

@veloraven Magento ver. 2.1.2.

@veloraven having similar problem on Magento 2.1.2:

Steps to reproduce issue:

  1. change block order in layout: make layered navigation render before product list
  2. go to page 2
  3. when layered navigation starts render it produces query with limit 20, offset 0 (AFIK this occurs because ListProduct::_beforeToHtml, Toolbar::setCollection haven't called on this state)
  4. we got an error like "no products found for your query".

@springimport How did you solve this problem? I'm facing same issue. I want to move product toolbar outside columns container like this http://prntscr.com/f6335o.
But I'm also getting same error i.e. Fatal error: Uncaught Error: Call to a member function getSize() on null in

@deepak-netwin Try to use a several wrappers for default containers.

@springimport Could explain what you mean with adding several wrappers for default containers? We're having the same issue.

@PieterCappelle Instead of moving toolbar, wrap toolbar with content via new blocks and remove classes and tags from toolbar block (currently not working, because m2.2 shows validation errors on this).

@springimport, thank you for your report.
We've created internal ticket(s) MAGETWO-82523 to track progress on the issue.

distributed-cd

@mcspronko thank you for joining. Please accept team invitation here and self-assign the issue.

Is there any news about these issues? I have the same problem

@mbernabeu Working on the issue.

This issue was already fixed I thought? Isn't this related to this one?
https://github.com/magento/magento2/issues/9413

The #9413 seems like related to the current issue, however, it is hard to follow the list of commits from that ticket. I will double check with 2.3 version and if it is resolved I will close this ticket. If not, I will provide a fix. I have 2.2 installed that is why the issue exists.

This issue is still here in 2.2.5

@mcspronko Did you manage to get anywhere sorting out which commits from #9413 were relevant to this ticket?

EDIT: This seems the most relevant so far https://github.com/magento/magento2/commit/818cf61d28821cc2a5435c60b6308adcb5a3ea26

The toolbar still cannot be moved outside of the product list because of this: ListProduct.php#L509

$this->setChild('toolbar', $toolbar);

No matter what you do, this will __always__ make the toolbar a child of the product list block. And since _all_ of the methods are private, you can't even easily just replace this function.

PR provided: https://github.com/magento/magento2/pull/19259~~

// edit: turns out more complex changes are needed, see explanation here: https://github.com/magento/magento2/pull/19259#issuecomment-439693832

I have outlined (yet another) workaround in this Magento Stackexchange thread: https://magento.stackexchange.com/a/250433/37461

Hi @engcom-Alfa. 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:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@engcom-Alfa Thank you for verifying the issue.

Unfortunately, not enough information was provided to acknowledge ticket. Please consider adding the following:

Once all required information is added, please add label "Issue: Confirmed" again.
Thanks!

This issue is reproduced on the fresh 2.4-develop instance

Steps to reproduce:

  1. Open catalog module -> catalog_category_view.xml
  2. Try to move a toolbar outside the block.
<referenceContainer name="content.top">
    <container name="product_list_toolbar_container" htmlTag="div"/>
</referenceContainer>
<move element="product_list_toolbar" destination="product_list_toolbar_container"/>

Actual Result:
screen

:white_check_mark: Confirmed by @engcom-Alfa
Thank you for verifying the issue. Based on the provided information internal tickets MC-30052 were created

Issue Available: @engcom-Alfa, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

@orlangur ,

Well, some of the blocks are supposed to be inside other blocks or exist in layout by design

Really? Can you point us to the documentation that give that information to developers?

I don't think this is documented in detail anywhere. You'll have to look at the code yourself.

Was this page helpful?
0 / 5 - 0 ratings