► Subscribe to my channel:
► Skype: cuongnq87
Hi everybody, I am Cuong, Welcome back to my Magento 2 tutorial video series.
Today, I am going to show everybody a best practice, How to use events and observers in Magento 2.
Before doing a practice relates to this part, we learn about events and observers in Magento 2, how it can work.
Working with events and observers is one of the main ways to extend Magento functionality. The events and observers implementation in Magento 2 is based on the publish-subscribe pattern. Using events and observers, you can run your custom code in response to a specific Magento event or even a custom event.
Events are dispatched by modules when certain actions are triggered. In addition to its own events, Magento allows you to create your own events that can be dispatched in your code. When an event is dispatched, it can pass data to any observers configured to watch that event.
– Creating new events
Custom events can be dispatched by simply passing in a unique event name to the event manager when you call the dispatch function. Your unique event name is referenced in your module’s events.xml file where you specify which observers will react to that event.
– Event areas
Generally, the location of the events.xml file will be under the /etc directory. Observers that are associated with events here will watch for these events globally. The events.xml file can also be defined under the /etc/frontend and /etc/adminhtml directories to configure observers to only watch for events in those specific areas.
Observers are a certain type of Magento class that can influence general behavior, performance, or change business logic. Observers are executed whenever the event they are configured to watch is dispatched by the event manager.
– Subscribing to events
Observers can be configured to watch certain events in the events.xml file.
That is the way how to use events and observers in Magento 2.
To understand more about these we will do a practice.
Assigning the observer to watch an event named page_block_html_topmenu_gethtml_after and it will help us to add a text link to the default menu of Magento 2.
This the results we need to do.
Okie, let’s go.
Let’s do this practice, you need to follow steps by step:
Step 1: Create a new module called PHPCuong_Menu
– Create the namespace PHPCuong in the path appcode.
– Create the module named Menu in the path appcodePHPCuong.
– Create the file named registration.php in the path appcodePHPCuongMenu
– Create the file named module.xml in the path appcodePHPCuongMenuetc
End of the step #1, I have been completed the step to create a new module called PHPCuong_Menu.
Step 2: Subscribing to the event named page_block_html_topmenu_gethtml_after
– The Observers can be configured to watch certain events in the events.xml file.
– Create the new file named events.xml in the path appcodePHPCuongMenuetcfrontend
I put the events.xml file in the folder named frontend because I want this observer only to apply on the storefront.
– You must specify these elements:
event name (required) – The name of the event is dispatched.
observer name (required) – The name of the observer for the event definition.
instance (required) – The fully qualified class name of the observer. Use the following naming convention when you specify this element VendorModuleObserverClassName.
disabled – Determines whether this observer is active or not. The default value is false.
shared – Determines the lifestyle of the class. The default is false.
Step 3: Creating an observer
– Create a new file named TopmenuHtmlGetAfter.php in the path appcodePHPCuongMenuObserver.
– Create the new file named topmenu.phtml in the path appcodePHPCuongMenuviewfrontendtemplateshtml
Step 4: Test and see the results.
Run the comment lines following:
– php bin/magento setup:upgrade –keep-generated
– php bin/magento cache:flush
Go to the homepage
Yeah, It works perfectly.
Thank you for your watching. If you have any questions about this practice please feel free to leave a comment below.
Don’t forget to like, comment, share and subscribe to my channel for getting the latest videos.
Please do not hesitate to contact me if you need me to join your Magento project. My rate is $25/hour in Magento 1 and $30/hour in Magento 2.
Get the source codes of this practice in the github