Create a list of dispatched events in Magento2

Magento2 has reduced the use of events. I guess the Magento plug-ins are more powerful. Today I would like to show, how to create a list of all dispatched events after visiting the product page for example. I have written a similar post a few years ago for older Magento versions.

We are searching for all calls of dispatch on an instance of Magento\Framework\Event\Manager. Here for example a bundle of really useful events in the function afterSave of the class Magento\Framework\Model\AbstractModel.

public function afterSave()
{
    $this->cleanModelCache();
    $this->_eventManager->dispatch('model_save_after', ['object' => $this]);
    $this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
    $this->_eventManager->dispatch($this->_eventPrefix . '_save_after', $this->_getEventData());
    $this->updateStoredData();
    return $this;
}

We will now change the function dispatch of the class Magento\Framework\Event\Manager.

public function dispatch($eventName, array $data = [])
{
    $eventName = mb_strtolower($eventName);
	// START CHANGES
	$eventLogFile = fopen('../var/log/events.log', 'a');
	fwrite($eventLogFile, $eventName . ' => ' . implode(', ', array_keys($data)) . "\n");
	fclose($eventLogFile);
	// END CHANGES
    \Magento\Framework\Profiler::start('EVENT:' . $eventName, ['group' => 'EVENT', 'name' => $eventName]);
    foreach ($this->_eventConfig->getObservers($eventName) as $observerConfig) {
        $event = new \Magento\Framework\Event($data);
        $event->setName($eventName);

        $wrapper = new Observer();
        $wrapper->setData(array_merge(['event' => $event], $data));

        \Magento\Framework\Profiler::start('OBSERVER:' . $observerConfig['name']);
        $this->_invoker->dispatch($observerConfig, $wrapper);
        \Magento\Framework\Profiler::stop('OBSERVER:' . $observerConfig['name']);
    }
    \Magento\Framework\Profiler::stop('EVENT:' . $eventName);
}

After that we visit a product page and the file var/log/events.log should be generated.

Eventlist of a product page

Here for example a list of all dispatched events with the given data after visiting a product page.

eventdata
core_collection_abstract_load_beforecollection
core_collection_abstract_load_aftercollection
model_load_beforeobject, field, value
store_load_beforeobject, field, value, data_object, store
model_load_afterobject
store_load_afterdata_object, store
store_collection_load_beforestore_collection
store_collection_load_afterstore_collection
store_group_load_beforeobject, field, value, data_object, store_group
store_group_load_afterdata_object, store_group
customer_session_initcustomer_session
theme_load_beforeobject, field, value, data_object, theme
theme_load_afterdata_object, theme
controller_action_predispatchcontroller_action, request
controller_action_predispatch_catalogcontroller_action, request
controller_action_predispatch_catalog_product_viewcontroller_action, request
catalog_controller_product_init_beforecontroller_action, params
catalog_product_load_beforeobject, field, value, data_object, product
catalog_entity_attribute_load_beforeobject, field, value, data_object, attribute
catalog_entity_attribute_load_afterdata_object, attribute
abstract_search_result_load_beforecollection
abstract_search_result_load_aftercollection
catalog_product_load_afterdata_object, product
eav_collection_abstract_load_beforecollection
catalog_product_collection_load_aftercollection
catalog_controller_product_init_afterproduct, controller_action
catalog_controller_product_viewproduct
catalog_product_collection_apply_limitations_aftercollection
model_save_beforeobject
core_abstract_save_beforedata_object, object
model_save_afterobject
clean_cache_by_tagsobject
core_abstract_save_afterdata_object, object
model_save_commit_afterobject
core_abstract_save_commit_afterdata_object, object
layout_load_beforefull_action_name, layout
depersonalize_clear_session
layout_generate_blocks_beforefull_action_name, layout
layout_generate_blocks_afterfull_action_name, layout
core_layout_block_create_afterblock
controller_action_postdispatch_catalog_product_viewcontroller_action, request
controller_action_postdispatch_catalogcontroller_action, request
controller_action_postdispatchcontroller_action, request
view_block_abstract_to_html_beforeblock
core_layout_render_elementelement_name, layout, transport
search_query_load_afterdata_object, search_query
page_block_html_topmenu_gethtml_beforemenu, block
catalog_category_tree_init_inactive_category_idstree
catalog_category_collection_load_beforecategory_collection
catalog_category_collection_load_aftercategory_collection
catalog_category_load_beforeobject, field, value, data_object, category
catalog_category_load_afterdata_object, category
page_block_html_topmenu_gethtml_aftermenu, transportObject
core_abstract_load_beforeobject, field, value, data_object
core_abstract_load_afterdata_object, object
currency_display_options_formingcurrency_options, base_code
catalog_block_product_status_displaystatus
catalog_product_is_salable_beforeproduct
catalog_product_is_salable_afterproduct, salable
shortcut_buttons_containercontainer, is_catalog_product, or_position
catalog_product_view_configresponse_object
rating_rating_collection_load_beforecollection
catalog_product_upsellproduct, collection, limit
cms_block_load_beforeobject, field, value, data_object
cms_block_load_afterdata_object, object
layout_render_before
layout_render_before_catalog_product_view
controller_front_send_response_beforerequest, response
website_load_beforeobject, field, value, data_object, website
website_load_afterdata_object, website
controller_action_predispatch_reviewcontroller_action, request
controller_action_predispatch_review_product_listajaxcontroller_action, request
review_controller_product_init_beforecontroller_action
review_controller_product_initproduct
review_controller_product_init_afterproduct, controller_action
controller_action_postdispatch_review_product_listajaxcontroller_action, request
controller_action_postdispatch_reviewcontroller_action, request
visitor_activity_savevisitor
review_review_collection_load_beforecollection
layout_render_before_review_product_listajax
Next Previous