0

I am trying to learn how exactly hooks and extensions are used. I am using expresso-store and have a function I need to call when the order is complete and gets to the success page (on hook 'store_order_complete_end') and two other functions that I need to call on events, both on a button click.

Could I include all three of these functions in one extension or does each function needs its own extension?

1

2 Answers 2

3

Extensions are for backend or server side stuff, whereas Modules can interact via the frontend or EE Control Panel. So an Extension captures a hook from another class, but can't capture a form submission or Ajax call from the frontend. Whereas a Module can't tap into a hook. But you can roll both an extension and a module into a single add-on (same name, shared folder, relevant separate files (ext.???, mod.???, etc).

Multiple hooks in a single extension: Multiple Hooks, One Extension

A Module would be able to capture the events via ACTion URLs. Example install code (upd.module-name.php):

    //###   Create ACTion IDs for the relevant functions   ###
    $formActions = array(
        "form_submission",
        "start_booking",
        "payment_notification"
    );
    foreach ($formActions as $method) {
        $data = array(
            'class' => $this->class,
            'method' => $method
        );
        ee()->db->insert('actions', $data);
    }//###   End of foreach

In the above example, my module has public functions for:

  • form_submission
  • start_booking
  • payment_notification

To render out the Action URL in your module (e.g. passing the ACTion URL to your frontend form or button click):

    $actionID = ee()->functions->fetch_action_id(__CLASS__, 'form_submission');

Although in theory, if your events triggered other modules or core EE functionality, e.g. Freeform, member login, etc, then you could use other hooks to tap into the event. But from your question, I'm guessing you want custom events not related to existing EE functionality or other add-ons.

3

It depends on whether the events that trigger the functions trigger a hook. If a button click, form submission, page load etc triggers a hook, then an extension is the right place for your custom functionality. If no hook is triggered and you need to respond to arbitrary clicks or form submissions, then a module or plugin would be appropriate.

You can react to any number of hooks in a single extension file and structure your code with as many public or private methods as you like in order to do so. Typically a single method will react to a single hook. However, after a hook has executed (or before), there's no way for other methods inside your extension file to get executed... they only exist in the context of the execution of a hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.