Hook Syntax

Spiro

Member
Hi,

I have been writing a few custom actions in order to integrate MW with our custom "frontend" system.

I am trying to figure out your hook syntax. Please could you give a bit of insight in to their structure.

So, for example i would like to execute an action (save the information to another database) after the customer saves their profile information on the customer/index.php/account/index page.

I have tried something like this, but it doesn't seem to work:
Yii::app()->hooks->doAction('customer_controller_account_before_save_data', array($this, '_SayHello'));

I will be doing a bit more of this type of work, so it would be good to understand how the hooks work.

Thanks!
 
@Spiro - Mailwizz hooks system works the same as the wordpress hooks system, where you can find a very good explanation at http://blog.teamtreehouse.com/hooks-wordpress-actions-filters-examples

In big lines, mailwizz defines actions and filters in various places of the application and you can hook into them from your extensions.
The difference between actions and filters:
--- When you hook into an action, you don't have to return the first parameter passed on the action, for example mailwizz defines this action that is executed before saving the data in all controllers:
PHP:
Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
    'controller'=> $this,
    'success'   => Yii::app()->notify->hasSuccess,
    'customer'  => $customer,
)));
As you see, the action will receive a single parameter, that is $collection.
Now, from your extension, you can hook into this action like this:
PHP:
Yii::app()->hooks->addAction('controller_action_save_data', function($collection) {
     $controller   = $collection->controller;
     $isSuccess = $collection->success;
     $customer  = $collection->customer;
     // do something with above variables.
});
--- When you hook into filters, you have to return the first parameter that is passed to the filter, so that other filters can see your changes or change it accordingly. For example, when sending a campaign, for each email that is sent, mailwizz defines following filter:
PHP:
// since 1.3.5.9
$emailParams = Yii::app()->hooks->applyFilters('console_command_send_campaigns_before_send_to_subscriber', $emailParams, $campaign, $subscriber, $customer, $server);
And we can hook into it like this:
PHP:
Yii::app()->hooks->addFilter('console_command_send_campaigns_before_send_to_subscriber', function($emailParams, $campaign, $subscriber, $customer, $server) {
     $emailParams['subject'] = 'Whatever else!';
     return $emailParams;
});
As we can see, this filter receives a lot of params, but we always have to return the first one, $emailParams which in this case, we modified and changed the subject line for the email.

Conclusion is that when using actions you cannot modify the params while when using filters you can do so.
Hope it helps, but if you have further questions, go for it ;)
 
Right, thanks for the detailed response.

How can i actually determine the names of these hooks. In example, i'd like to execute an action after the customer saves their profile information on the customer/index.php/account/index page. what is the name of this hook going to be?

Something like: customer_controller_action_save_data, or customer_controller_action_submit_form???

And is there any way to systimatically determine what these names would be?
 
@Spiro - the name would still be customer_controller_action_save_data, that is a global hook triggered after each save action.
the way you detect where exactly is triggered is determined by the controller variable, for example:
PHP:
Yii::app()->hooks->addAction('controller_action_save_data', function($collection) {
    if ($collection->controller->id == 'account' && $collection->success) {
         // the customer saved the data successfully, do something with $collection->customer
   }
});
In above example, we have access to those 3 variables from $collection: controller, success and customer.
If we look in the account controller itself, we can see:
PHP:
Yii::app()->hooks->doAction('controller_action_save_data', $collection = new CAttributeCollection(array(
    'controller'=> $this,
    'success'   => Yii::app()->notify->hasSuccess,
    'customer'  => $customer,
)));
So this is where the variables are passed. the controller and success variables will always be passed. Other controllers will pass extra variables and under different names, you have to look into the controller to see what exactly they pass.

And is there any way to systimatically determine what these names would be?
You can search for Yii::app()->hooks->doAction( and Yii::app()->hooks->applyFilters( and you'll find all the hooks mailwizz defines.
 
Back
Top