cancel
Showing results for 
Search instead for 
Did you mean: 

Need hooks for injecting content within the product list loop

0 Kudos

Need hooks for injecting content within the product list loop

Feature request from CNanninga, posted on GitHub Oct 28, 2015

Because the primary product list template contains all HTML for each product directly in the loop within this template, this means that the ability for a module to inject additional content for each product usually requires overriding this entire template.

Several hooks should be provided within the main loop of app/code/Magento/Catalog/view/frontend/templates/product/list.phtml to allow for this kind of content injection. (Such as "before" and "after" hooks for logical pieces of the product presentation, such as image, name, etc.)

See the product list template in the rwd/default theme of Magento 1 for an example. The core text list "name.after" was added to this block, and within the template all children of "name.after" are looped through, the individual product set on them, and their HTML output.

Something similar would achieve the appropriate purpose in M2. Containers for each of these "hooks" would be the appropriate choice. The solution differs from the above example from M1 in a couple of respects: 1) The logic of looping through the core text list children and setting the appropriate data was never best contained within the template in the first place. It would be cleaner were this logic contained in a block method. 2) Containers, not being objects, don't offer a direct way to retrieve their child block objects and manipulate the data on them before outputting (that I know of).

What I suggest is a generic way for specifying data that should be set on child blocks when they are being output. Thus these hooks could be output within the product list loop something like this: echo $block->getChildHtml('before_name_container', true, array('product' => $_product)) But this would require modifications to the signatures of, or creating other versions of, Magento\Framework\View\Element\AbstractBlock::getChildHtml, Magento\Framework\View\Layout::renderElement, Magento\Framework\View\Layout::renderNonCachedElement, etc. So before working on a pull request to introduce any such changes, I wanted to open this issue for discussion. Is this on the right track, or are there other suggestions for a way to achieve this objective?

2 Comments
apiuser
New Member

Comment from veloraven, posted on GitHub Nov 07, 2016

@CNanninga is this issue still actual?

apiuser
New Member

Comment from CNanninga, posted on GitHub Nov 07, 2016

This is still a valid feature request, yes. Architecturally, the solution could take any number of paths, but the HTML for a product list is still output in a single loop in a template, without any additional hook points, so it's still a challenge to find an unobtrusive way to inject additional content in a product list.