12

I have installed a blog module onto my Magento 2 platform. This blog module has an observer which automatically adds a new menu item into the primary navigation. I wish to stop this.

The events.xml file looks like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="page_block_html_topmenu_gethtml_before">
        <observer name="aw_blog_add_topmenu_items" instance="Aheadworks\Blog\Observer\AddBlogToTopmenuItemsObserver" />
    </event>
</config>

As you can see it references the observer that adds the link into the primary navigation. Deleting events.xml solves my problem, but I'm looking for a method that doesn't involve modifying of core module files.

1 Answer 1

15

You still can disable observers. In Magento 1 it was done with <type>disabled</type>, in Magento 2 it's disabled="true":

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="page_block_html_topmenu_gethtml_before">
        <observer name="aw_blog_add_topmenu_items"
                  instance="Aheadworks\Blog\Observer\AddBlogToTopmenuItemsObserver"
                  disabled="true" />
    </event>
</config>

If you add this modified events.xml file to a custom module and make the custom module depend on Aheadworks_Blog, it should override the original one.

2
  • 3
    I'm curious if the observer merging process uses only the name attribute (seeing how it's unique). In which case, you could omit the somewhat long instance property: <observer name="aw_blog_add_topmenu_items" disabled="true".
    – nevvermind
    Commented Apr 24, 2016 at 10:59
  • as per vendor/magento/framework/Event/etc/events.xsd only name required - all other attributes are optional
    – Mammouth
    Commented Dec 21, 2021 at 15:02

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.