在这篇文章之后,我终于设法通过扩展Magento_Adminhtml_Controller_Action
并在它之前执行一些操作来捕获一个事件。但现在我正在尝试改进它,以便我可以捕获在管理面板上触发的另一个事件,并从那里将一个数组通过request
变量传递给前端的另一个事件。我有这些 SO 问题/答案,在这里和这里,但我无法实现我所需要的。我已经测试了observer code
使用die()
以确保执行线程进入正确的调用并且没问题。我正在使用 AlanStorm 的 CommerceBug,以防它可以用来解决这个问题。
这是我的 Observer.php 代码。
<?php
class Dts_Videotestimonials_Model_Observer {
public function hookToAdminhtmlControllerActionPreDispatch($observer)
{
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'videotestimonials_adminhtml_videotestimonialsbackend_post')
{
// dispatching our own event before action upload video is run and sending parameters we need
Mage::dispatchEvent("upload_video_before", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToUploadVideoBefore($observer)
{
//Hooking to our own event
$request = $observer->getEvent()->getRequest()->getParams();
// do something with product
$user = Mage::getSingleton('admin/session');
$userName = $user->getUser()->getFirstname();
$userEmail = $user->getUser()->getEmail();
$request['product_id'] = "16"; #$_product->getId(),
$request['author_email'] = $userEmail;
$request['author_name'] = $userName;
$request['video_link'] = "http://www.youtube.com/watch?v=y435u6kfExA&feature=youtube_gdata_player";
$request['video_type'] = "link";
$request['title'] = "AT&T Phone Nokia 2610";
$request['comment'] = "this is a comment";
Mage::dispatchEvent("vidtest_youtube_post", $request);
}
}
编辑:
这是完整的 config.xml
<?xml version="1.0"?>
<config>
<modules>
<Dts_Videotestimonials>
<version>0.1.0</version>
</Dts_Videotestimonials>
</modules>
<global>
<models>
<videotestimonials>
<class>Dts_Videotestimonials_Model</class>
<resourceModel>videotestimonials_mysql4</resourceModel>
</videotestimonials>
</models>
<events>
<controller_action_predispatch>
<observers>
<controller_action_before>
<type>singleton</type>
<class>videotestimonials/observer</class>
<method>hookToAdminhtmlControllerActionPreDispatch</method>
</controller_action_before>
</observers>
</controller_action_predispatch>
<upload_video_before>
<observers>
<upload_video_before>
<type>singleton</type>
<class>videotestimonials/observer</class>
<method>hookToUploadVideoBefore</method>
</upload_video_before>
</observers>
</upload_video_before>
</events>
<helpers>
<videotestimonials>
<class>Dts_Videotestimonials_Helper</class>
</videotestimonials>
</helpers>
<blocks>
<videotestimonials>
<class>Dts_Videotestimonials_Block</class>
</videotestimonials>
</blocks>
</global>
<admin>
<routers>
<videotestimonials>
<use>admin</use>
<args>
<module>Dts_Videotestimonials</module>
<frontName>videotestimonials</frontName>
</args>
</videotestimonials>
</routers>
</admin>
<adminhtml>
<menu>
<videotestimonials module="videotestimonials">
<title>Videotestimonials</title>
<sort_order>100</sort_order>
<children>
<videotestimonialsbackend module="videotestimonials">
<title>VideoTestimonials_Admin</title>
<sort_order>0</sort_order>
<action>videotestimonials/adminhtml_videotestimonialsbackend</action>
</videotestimonialsbackend>
<pending_video translate="title">
<title>Videos pendientes</title>
<sort_order>20</sort_order>
<action>videotestimonials/adminhtml_pendingvideos/pending</action>
</pending_video>
</children>
</videotestimonials>
</menu>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<videotestimonials translate="title" module="videotestimonials">
<title>Videotestimonials</title>
<sort_order>1000</sort_order>
<children>
<videotestimonialsbackend translate="title">
<title>VideoTestimonials_Admin</title>
</videotestimonialsbackend>
<pending_video translate="title">
<title>Videos pendientes</title>
<sort_order>20</sort_order>
</pending_video>
</children>
</videotestimonials>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<videotestimonials>
<file>videotestimonials.xml</file>
</videotestimonials>
</updates>
</layout>
</adminhtml>
<crontab>
<jobs>
<videotestimonials_videotestimonialscron>
<schedule><cron_expr>59 0 * */1 0</cron_expr></schedule>
<run><model>videotestimonials/cron::VideoTestimonialscron</model></run>
</videotestimonials_videotestimonialscron>
</jobs>
</crontab>
</config>