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.
Here for example a list of all dispatched events with the given data after visiting a product page.
event | data |
---|---|
core_collection_abstract_load_before | collection |
core_collection_abstract_load_after | collection |
model_load_before | object, field, value |
store_load_before | object, field, value, data_object, store |
model_load_after | object |
store_load_after | data_object, store |
store_collection_load_before | store_collection |
store_collection_load_after | store_collection |
store_group_load_before | object, field, value, data_object, store_group |
store_group_load_after | data_object, store_group |
customer_session_init | customer_session |
theme_load_before | object, field, value, data_object, theme |
theme_load_after | data_object, theme |
controller_action_predispatch | controller_action, request |
controller_action_predispatch_catalog | controller_action, request |
controller_action_predispatch_catalog_product_view | controller_action, request |
catalog_controller_product_init_before | controller_action, params |
catalog_product_load_before | object, field, value, data_object, product |
catalog_entity_attribute_load_before | object, field, value, data_object, attribute |
catalog_entity_attribute_load_after | data_object, attribute |
abstract_search_result_load_before | collection |
abstract_search_result_load_after | collection |
catalog_product_load_after | data_object, product |
eav_collection_abstract_load_before | collection |
catalog_product_collection_load_after | collection |
catalog_controller_product_init_after | product, controller_action |
catalog_controller_product_view | product |
catalog_product_collection_apply_limitations_after | collection |
model_save_before | object |
core_abstract_save_before | data_object, object |
model_save_after | object |
clean_cache_by_tags | object |
core_abstract_save_after | data_object, object |
model_save_commit_after | object |
core_abstract_save_commit_after | data_object, object |
layout_load_before | full_action_name, layout |
depersonalize_clear_session | |
layout_generate_blocks_before | full_action_name, layout |
layout_generate_blocks_after | full_action_name, layout |
core_layout_block_create_after | block |
controller_action_postdispatch_catalog_product_view | controller_action, request |
controller_action_postdispatch_catalog | controller_action, request |
controller_action_postdispatch | controller_action, request |
view_block_abstract_to_html_before | block |
core_layout_render_element | element_name, layout, transport |
search_query_load_after | data_object, search_query |
page_block_html_topmenu_gethtml_before | menu, block |
catalog_category_tree_init_inactive_category_ids | tree |
catalog_category_collection_load_before | category_collection |
catalog_category_collection_load_after | category_collection |
catalog_category_load_before | object, field, value, data_object, category |
catalog_category_load_after | data_object, category |
page_block_html_topmenu_gethtml_after | menu, transportObject |
core_abstract_load_before | object, field, value, data_object |
core_abstract_load_after | data_object, object |
currency_display_options_forming | currency_options, base_code |
catalog_block_product_status_display | status |
catalog_product_is_salable_before | product |
catalog_product_is_salable_after | product, salable |
shortcut_buttons_container | container, is_catalog_product, or_position |
catalog_product_view_config | response_object |
rating_rating_collection_load_before | collection |
catalog_product_upsell | product, collection, limit |
cms_block_load_before | object, field, value, data_object |
cms_block_load_after | data_object, object |
layout_render_before | |
layout_render_before_catalog_product_view | |
controller_front_send_response_before | request, response |
website_load_before | object, field, value, data_object, website |
website_load_after | data_object, website |
controller_action_predispatch_review | controller_action, request |
controller_action_predispatch_review_product_listajax | controller_action, request |
review_controller_product_init_before | controller_action |
review_controller_product_init | product |
review_controller_product_init_after | product, controller_action |
controller_action_postdispatch_review_product_listajax | controller_action, request |
controller_action_postdispatch_review | controller_action, request |
visitor_activity_save | visitor |
review_review_collection_load_before | collection |
layout_render_before_review_product_listajax |