This article will talk about Event in Magento 2. As you know, Magento 2 is using the event driven architecture which will help too much to extend the Magento functionality. We can understand this event as a kind of flag that rises when a specific situation happens. We will use an example module Mageplaza_Example to exercise this lesson.
Dispatch event
In Magento 2 Events, we can use the class Magento\Framework\Event\Manager to dispatch event. For example, we create a controller action in Mageplaza_Example to show the word “Hello World” on the screen:
File: app/code/Mageplaza/Example/Controller/Hello/World.php
<?php
namespace Mageplaza\Example\Controller\Hello;
class World extends \Magento\Framework\App\Action\Action
{
public function execute()
{
echo 'Hello World';
exit;
}
}
Now we want to dispatch an magento 2 event list which allow other module can change the word displayed. We will change the controller like this:
File: app/code/Mageplaza/Example/Controller/Hello/World.php
<?php
namespace Mageplaza\Example\Controller\Hello;
class World extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$textDisplay = new \Magento\Framework\DataObject(array('text' => 'Mageplaza'));
$this->_eventManager->dispatch('example_hello_world_display_text', ['text' => $textDisplay]);
echo $textDisplay->getText();
exit;
}
}
The dispatch method will receive 2 arguments: an unique event name and an array data. In this example, we add the data object to the event and call it back to display the text.
Catch and handle event
Event area
Magento use area definition to manage the store. We will have a frontend area and admin area. With the configuration file, they can be put in 3 places:
Under etc/ folder is the configuration which can be used in both admin and frontend.
Under etc/frontend folder will be used for frontend area.
Under etc/adminhtml folder will be used for admin area.
The same with the event configuration file. You can create events configuration file for each area like this:
Admin area: app/code/Mageplaza/Example/etc/adminhtml/events.xml
Frontend area: app/code/Mageplaza/Example/etc/frontend/events.xml
Global area: app/code/Mageplaza/Example/etc/events.xml
Create events.xml
In this example, we only catch the event to show the word Mageplaza on the frontend so we should create an events.xml file in etc/frontend folder.
File: app/code/Mageplaza/Example/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framewo rk:Event/etc/events.xsd">
<event name="example_hello_world_display_text">
<observer name="hello_world_display" instance="Mageplaza\Example\Observer\ChangeDisplay Text" />
</event>
</config>
In this file, under config element, we define an event element with the name is the event name which was dispatch above. The class which will execute this event will be define in the observer element by instance attribute. The name of observer is used to identify this with other observers of this event.
With this events.xml file, Magento will execute class Mageplaza\Example\Observer\ChangeDisplayText whenever the dispatch method of this event was called on frontend area. Please note that, we place events.xml in the frontend area, so if you dispatch that event in the admin area (like admin controller), it will not run.
Observer
Now we will create a class to execute above event.
File: app/code/Mageplaza/Example/Observer/ChangeDisplayText.php
<?php
namespace Mageplaza\Example\Observer;
class ChangeDisplayText implements \Magento\Framework\Event\ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$displayText = $observer->getData('text');
$displayText->setText('Execute event successfully.');
return $this;
}
}
This class will implement the ObserverInterface and declare the execute method. You can see this simple method to know how it work.
Let’s flush cache and see the result.
List all events in Magento 2
Events in PHP Files
File Event name
app/code/Magento/Authorizenet/Controller/Directpost/Payment/Place.php checkout_directpost_placeOrder
app/code/Magento/Backend/Block/System/Store/Edit/AbstractForm.php adminhtml_store_edit_form_prepare_form
app/code/Magento/Backend/Block/Template.php adminhtml_block_html_before
app/code/Magento/Backend/Block/Widget/Grid.php backend_block_widget_grid_prepare_grid_before
app/code/Magento/Backend/Console/Command/CacheCleanCommand.php adminhtml_cache_flush_system
app/code/Magento/Backend/Console/Command/CacheFlushCommand.php adminhtml_cache_flush_all
app/code/Magento/Backend/Controller/Adminhtml/Cache/CleanImages.php clean_catalog_images_cache_after
app/code/Magento/Backend/Controller/Adminhtml/Cache/CleanMedia.php clean_media_cache_after
app/code/Magento/Backend/Controller/Adminhtml/Cache/CleanStaticFiles.php clean_static_files_cache_after
app/code/Magento/Backend/Controller/Adminhtml/Cache/FlushAll.php adminhtml_cache_flush_all
app/code/Magento/Backend/Controller/Adminhtml/Cache/FlushSystem.php adminhtml_cache_flush_system
app/code/Magento/Backend/Controller/Adminhtml/System/Design/Save.php theme_save_after
app/code/Magento/Backend/Controller/Adminhtml/System/Store/DeleteStorePost.php store_delete
app/code/Magento/Backend/Controller/Adminhtml/System/Store/Save.php store_group_save
app/code/Magento/Backend/Controller/Adminhtml/System/Store/Save.php NO_MATCH
app/code/Magento/Backend/Model/Auth.php backend_auth_user_login_success
app/code/Magento/Backend/Model/Auth.php backend_auth_user_login_failed
app/code/Magento/Backend/Model/Auth.php backend_auth_user_login_failed
app/code/Magento/Bundle/Block/Catalog/Product/View/Type/Bundle.php catalog_product_option_price_configuration_after
app/code/Magento/Bundle/Model/Product/Price.php prepare_catalog_product_collection_prices
app/code/Magento/Bundle/Model/Product/Price.php catalog_product_get_final_price
app/code/Magento/Bundle/Model/Product/Price.php catalog_product_get_final_price
app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php catalog_product_prepare_index_select
app/code/Magento/Bundle/Pricing/Price/BundleSelectionPrice.php catalog_product_get_final_price
app/code/Magento/Catalog/Block/Adminhtml/Category/Tab/Attributes.php adminhtml_catalog_category_edit_prepare_form
app/code/Magento/Catalog/Block/Adminhtml/Category/Tabs.php adminhtml_catalog_category_tabs
app/code/Magento/Catalog/Block/Adminhtml/Category/Tree.php adminhtml_catalog_category_tree_is_moveable
app/code/Magento/Catalog/Block/Adminhtml/Category/Tree.php adminhtml_catalog_category_tree_can_add_root_categ ory
app/code/Magento/Catalog/Block/Adminhtml/Category/Tree.php adminhtml_catalog_category_tree_can_add_sub_catego ry
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Advanced.php product_attribute_form_build
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php product_attribute_form_build_front_tab
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Front.php adminhtml_catalog_product_attribute_edit_frontend_ prepare_form
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php adminhtml_product_attribute_types
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Edit/Tab/Main.php product_attribute_form_build_main_tab
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Grid.php product_attribute_grid_build
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/NewAttribute/Product/Attributes.php adminhtml_catalog_product_edit_prepare_form
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/NewAttribute/Product/Attributes.php adminhtml_catalog_product_edit_element_types
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Set/Main.php adminhtml_catalog_product_attribute_set_main_html_ before
app/code/Magento/Catalog/Block/Adminhtml/Product/Attribute/Set/Toolbar/Main.php adminhtml_catalog_product_attribute_set_toolbar_ma in_html_before
app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Action/Attribute/Tab/Attributes.php adminhtml_catalog_product_form_prepare_excluded_fi eld_list
app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Attributes/Create.php adminhtml_catalog_product_edit_tab_attributes_crea te_html_before
app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Attributes.php adminhtml_catalog_product_edit_prepare_form
app/code/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Attributes.php adminhtml_catalog_product_edit_element_types
app/code/Magento/Catalog/Block/Adminhtml/Product/Grid.php adminhtml_catalog_product_grid_prepare_massaction
app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Gallery/Content.php catalog_product_gallery_prepare_layout
app/code/Magento/Catalog/Block/Product/AbstractProduct.php catalog_block_product_status_display
app/code/Magento/Catalog/Block/Product/ListProduct.php catalog_block_product_list_collection
app/code/Magento/Catalog/Block/Product/ProductList/Upsell.php catalog_product_upsell
app/code/Magento/Catalog/Block/Product/View/Options.php catalog_product_option_price_configuration_after
app/code/Magento/Catalog/Block/Product/View.php catalog_product_view_config
app/code/Magento/Catalog/Block/Rss/Category.php rss_catalog_category_xml_callback
app/code/Magento/Catalog/Block/Rss/Product/NewProducts.php rss_catalog_new_xml_callback
app/code/Magento/Catalog/Block/Rss/Product/Special.php rss_catalog_special_xml_callback
app/code/Magento/Catalog/Block/ShortcutButtons.php shortcut_buttons_container
app/code/Magento/Catalog/Controller/Adminhtml/Category/Delete.php catalog_controller_category_delete
app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php category_prepare_ajax_response
app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php catalog_category_prepare_save
app/code/Magento/Catalog/Controller/Adminhtml/Product/Action/Attribute/Save.php catalog_product_to_website_change
app/code/Magento/Catalog/Controller/Adminhtml/Product/Edit.php catalog_product_edit_action
app/code/Magento/Catalog/Controller/Adminhtml/Product/Gallery/Upload.php catalog_product_gallery_upload_image_after
app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php catalog_product_new_action
app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php controller_action_catalog_product_save_entity_afte r
app/code/Magento/Catalog/Controller/Category/View.php catalog_controller_category_init_after
app/code/Magento/Catalog/Controller/Product/Compare/Add.php catalog_product_compare_add_product
app/code/Magento/Catalog/Controller/Product/Compare/Remove.php catalog_product_compare_remove_product
app/code/Magento/Catalog/Helper/Product/View.php catalog_controller_product_view
app/code/Magento/Catalog/Helper/Product.php catalog_controller_product_init_before
app/code/Magento/Catalog/Helper/Product.php catalog_controller_product_init_after
app/code/Magento/Catalog/Model/Category.php _move_before
app/code/Magento/Catalog/Model/Category.php _move_after
app/code/Magento/Catalog/Model/Category.php category_move
app/code/Magento/Catalog/Model/Product/Action.php catalog_product_attribute_update_before
app/code/Magento/Catalog/Model/Product/Attribute/Source/Inputtype.php adminhtml_product_attribute_types
app/code/Magento/Catalog/Model/Product/Type/AbstractType.php NO_MATCH
app/code/Magento/Catalog/Model/Product/Type/Price.php catalog_product_get_final_price
app/code/Magento/Catalog/Model/Product.php _validate_before
app/code/Magento/Catalog/Model/Product.php _validate_after
app/code/Magento/Catalog/Model/Product.php catalog_product_is_salable_before
app/code/Magento/Catalog/Model/Product.php catalog_product_is_salable_after
app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php _load_before
app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php _load_after
app/code/Magento/Catalog/Model/ResourceModel/Category/Collection.php _add_is_active_filter
app/code/Magento/Catalog/Model/ResourceModel/Category/Flat/Collection.php _load_before
app/code/Magento/Catalog/Model/ResourceModel/Category/Flat/Collection.php _load_after
app/code/Magento/Catalog/Model/ResourceModel/Category/Flat/Collection.php _add_is_active_filter
app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php catalog_category_tree_init_inactive_category_ids
app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php catalog_category_flat_loadnodes_before
app/code/Magento/Catalog/Model/ResourceModel/Category/Tree.php catalog_category_tree_init_inactive_category_ids
app/code/Magento/Catalog/Model/ResourceModel/Category.php catalog_category_change_products
app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php catalog_prepare_price_select
app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php catalog_product_collection_load_after
app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php catalog_product_collection_before_add_count_to_cat egories
app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php catalog_product_collection_apply_limitations_after
app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php catalog_product_compare_item_collection_clear
app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/AbstractEav.php prepare_catalog_product_index_select
app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Decimal.php prepare_catalog_product_index_select
app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php prepare_catalog_product_index_select
app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Eav/Source.php prepare_catalog_product_index_select
app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/DefaultPrice.php prepare_catalog_product_index_select
app/code/Magento/Catalog/Model/ResourceModel/Product.php catalog_product_delete_after_done
app/code/Magento/Catalog/Model/Rss/Product/NotifyStock.php rss_catalog_notify_stock_collection_select
app/code/Magento/Catalog/Plugin/Model/Product/Action/UpdateAttributesFlushCache.php clean_cache_by_tags
app/code/Magento/CatalogImportExport/Model/Import/Product.php catalog_product_import_bunch_delete_after
app/code/Magento/CatalogImportExport/Model/Import/Product.php catalog_product_import_finish_before
app/code/Magento/CatalogImportExport/Model/Import/Product.php catalog_product_import_bunch_save_after
app/code/Magento/CatalogInventory/Model/Indexer/Stock/AbstractAction.php clean_cache_by_tags
app/code/Magento/CatalogRule/Block/Adminhtml/Promo/Catalog/Edit/Tab/Main.php adminhtml_promo_catalog_edit_tab_main_prepare_form
app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/Save.php adminhtml_controller_catalogrule_prepare_save
app/code/Magento/CatalogRule/Model/Indexer/AbstractIndexer.php clean_cache_by_tags
app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php catelogsearch_searchable_attributes_load_after
app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/Full.php catelogsearch_searchable_attributes_load_after
app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext.php catalogsearch_reset_search_result
app/code/Magento/Checkout/Block/QuoteShortcutButtons.php shortcut_buttons_container
app/code/Magento/Checkout/Controller/Cart/Add.php checkout_cart_add_product_complete
app/code/Magento/Checkout/Controller/Cart/UpdateItemOptions.php checkout_cart_update_item_complete
app/code/Magento/Checkout/Controller/Onepage/SaveOrder.php checkout_controller_onepage_saveOrder
app/code/Magento/Checkout/Controller/Onepage/Success.php checkout_onepage_controller_success_action
app/code/Magento/Checkout/Helper/Data.php checkout_allow_guest
app/code/Magento/Checkout/Model/Cart.php checkout_cart_product_add_after
app/code/Magento/Checkout/Model/Cart.php checkout_cart_update_items_before
app/code/Magento/Checkout/Model/Cart.php checkout_cart_update_items_after
app/code/Magento/Checkout/Model/Cart.php checkout_cart_save_before
app/code/Magento/Checkout/Model/Cart.php checkout_cart_save_after
app/code/Magento/Checkout/Model/Cart.php checkout_cart_product_update_after
app/code/Magento/Checkout/Model/Session.php custom_quote_process
app/code/Magento/Checkout/Model/Session.php checkout_quote_init
app/code/Magento/Checkout/Model/Session.php load_customer_quote_before
app/code/Magento/Checkout/Model/Session.php checkout_quote_destroy
app/code/Magento/Checkout/Model/Session.php restore_quote
app/code/Magento/Checkout/Model/Type/Onepage.php checkout_type_onepage_save_order_after
app/code/Magento/Checkout/Model/Type/Onepage.php checkout_submit_all_after
app/code/Magento/Cms/Block/Adminhtml/Page/Edit/Tab/Content.php adminhtml_cms_page_edit_tab_content_prepare_form
app/code/Magento/Cms/Block/Adminhtml/Page/Edit/Tab/Design.php adminhtml_cms_page_edit_tab_design_prepare_form
app/code/Magento/Cms/Block/Adminhtml/Page/Edit/Tab/Main.php adminhtml_cms_page_edit_tab_main_prepare_form
app/code/Magento/Cms/Block/Adminhtml/Page/Edit/Tab/Meta.php adminhtml_cms_page_edit_tab_meta_prepare_form
app/code/Magento/Cms/Controller/Adminhtml/Page/Delete.php adminhtml_cmspage_on_delete
app/code/Magento/Cms/Controller/Adminhtml/Page/Delete.php adminhtml_cmspage_on_delete
app/code/Magento/Cms/Controller/Adminhtml/Page/Save.php cms_page_prepare_save
app/code/Magento/Cms/Controller/Router.php cms_controller_router_match_before
app/code/Magento/Cms/Helper/Page.php cms_page_render