1

I have tried this with plugins and preferences and I seem to be missing the best practise method.

Why would Magento render the page so early that you can't modify the layout via plugin?

Am I missing something here?

I want to add an extra layout handle if the product has a certain attribute with x value. What is best practise here?

1 Answer 1

1

Possibly it isn't necessary to use a plugin if the event is provided.

Try use the event

layout_load_before

The event is dispatched in

\Magento\Framework\View\Layout\Builder:

protected function loadLayoutUpdates()
{
    Profiler::start('LAYOUT');
    /* dispatch event for adding handles to layout update */
    $this->eventManager->dispatch(
        'layout_load_before',
        ['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
    );
    Profiler::start('layout_load');
    $this->layout->getUpdate()->load();

    Profiler::stop('layout_load');
    Profiler::stop('LAYOUT');
    return $this;
}

Actually I think that magento 2 plugins is rather powerful feature, but, let me say, it breaks out of the application architecture. I’d rather prefer the event usage to the plugin usage.

5
  • This would run for every layout load for the sake of one type of product. I just can't bring myself to do it. Mar 15, 2017 at 13:51
  • You can check in your own observer class any product parameter, attribute or attribute set. Only if it correspond your specific condition - add layout handle to layout.
    – sergei.sss
    Mar 15, 2017 at 14:06
  • 1
    It is an example for magento 1 - but I assume, for magento 2 there are no essential distinctions: magebase.com/magento-tutorials/creating-custom-layout-handles
    – sergei.sss
    Mar 15, 2017 at 14:10
  • Would you recommend this with performance in mind? Mar 15, 2017 at 14:15
  • I can't see any performance problem, using the event only for the product page with simplest verification by product attribute.
    – sergei.sss
    Mar 15, 2017 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.