0

I have a form that is using the Amazon MWS API to pull in a list of reports to create a drop-down selection with options that contain the form id and name. I'm using the EE form helper to do this. Here's how the end of my method looks:

$xml = simplexml_load_string($response);
$options = array();
foreach ($xml->GetReportListResult->ReportInfo as $report) {
  $options[(string) $report->ReportId] = (string) str_replace('_', ' ', ucfirst(strtolower($report->ReportType)));
}
ee()->load->helper('form');
$form = form_open('ACT={action_id}');
$form .= form_label('Report types', 'report_types');
$form .= form_dropdown('report_types', $options);
$form .= cp_form_submit('Request Form', 'Processing your request');
return $form;

And in the template I'm doing this:

{exp:amazon_mws:get_report_list}

This is creating the form just fine. What I want this form to do is when someone selects a report type from the drop-down and hits Request Form it will run another function that will take the ReportId from the form and query the MWS API again to look for that report, save some stuff to the database (such as the report itself, the user, when it was created, etc) in order to display a list of reports.

The thing I'm struggling with is how actions are done in EE. There is not a lot of (if any?) documentation on this. The main thing I found was this article which is from 2012 and has broken reference links.

As far as I can tell, the way that something like this is done in EE is to use a ACT ID, as that article says.

http://example.com/index.php?ACT=123

Whenever ExpressionEngine encounters an ACTion URL, it attempts to run the code associated with the provided ACTion ID.

How it works

When an ACTion URL is loaded, ExpressionEngine looks in the exp_actions database table for an entry with the supplied ACTion ID. Each ACTion ID is associated with a class and method (i.e. the code to run).

Ok, great. That seems simple enough! I just need to know/create an ACTion ID for the method and voilà, it'll run when the form is processed. Only I don't know what the id is or what I would need to do to create it.

So I go to the documentation looking for action_id and I find this: fetch_action_id($class, $method). So I create a test method in my class and use that function to see what I get: <?php echo ee()->functions->fetch_action_id('Amazon_mws', 'action_id_test'); ?>

what I get is this:

{AID:Amazon_mws:action_id_test}

Which is basically just the same thing I passed to the function.

You can see in the form I created above that I put 'ACT={action_id}' in the form_open function. That's because I don't know how to do this and I assume you're supposed to put an action_id there.

What I also tried to do was look at how other pre-made forms in EE are constructed, so I looked at the built in login form and found this:

enter image description here

Ah-ha! There's a hidden ACT input with a value of 14. I'm assuming this is the ACTion id for the EE login method. I also noticed when I use the logout link it goes to a url like this http://www.example.com?ACT=15&csrf_token={token}. So this is clearly using the ACTion id to run a logout method. Login is 14, logout is 15. This makes sense. I also noticed that when I use the form helper it automatically creates a hidden csrf_token, so I'm thinking I must do something like the above hidden inputs with my form in order to make it work.

I've scoured the documentation for some clarification on this issue but haven't had much luck. I'm either not good at looking up what I need (entirely possible, I'm not a super experienced dev and am only just learning EE) or the documentation isn't there. And I'm sure that this is something that EE devs are using constantly that's more or less taken for granted.

What I think I need to do is to enter the method I'm trying to use into the exp_actions table so EE knows that this is an ACTion method, but I'm not sure how to do this and I'm not seeing any documentation about it.

Any help would be greatly appreciated! Thanks.

2

There is no magic method for this, as far as I know. It's pretty standard that in the upd.your_addon.php file, in the install function, you just add rows to your exp_actions table like so:

class Your_addon_upd {

    public $module_actions = array(
        'my_action_method_one',
        'my_action_method_two'
    );

    public function install()
    {

        foreach($this->module_actions as $method)
        {
            $data = array(
                'class'     => 'Your_addon',
                'method'    => $method
            );

            ee()->db->insert('actions', $data);
        }
    }

    /* ... Rest of the methods for this file ... */
}

Just make sure your uninstall function also removes those rows from the exp_actions table.

| improve this answer | |
  • Then there must be something awry with my upd file, because when I install/uninstall it doesn't seem to be running those methods. I have the install() set to insert the module data into the modules table and the action data into the actions table, but when I check the db for either of those I don't see them. Is there a step I'm missing to get the upd file to work properly? Thank you for your answer! – Shayne Apr 17 '19 at 18:41
  • 1
    Make sure your class name is correct (It has to be in this format: start with a capital letter, matches your add-on shortname (so, the folder name of the add-on), and end with _upd. So in my example, my add-on folder name is your_addon, and my update class is Your_addon_upd. Also make sure your update class has these public functions: __construct, install, update, and uninstall. I'm not sure if this helps, but that should work and those methods should fire. Add die("Install fired"); in your install method and run the module install to make sure it works. – jrothafer Apr 17 '19 at 23:44
  • 1
    I want to thank you for your help, jrothafer. All of my names were correct but the only thing that I left out was a public before my $version variable which was causing the class to fail right from the outset (but not throw any errors). I fixed that and now have a working update class! I've also updated my form to use the fetch_action_id function when constructing the form so it always has the right id. Again, thank you for your time. – Shayne Apr 18 '19 at 17:53
  • 1
    Right! I'm not sure why I didn't suggest turning on all error reporting first. Glad to help! – jrothafer Apr 18 '19 at 17:59

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.