Community & Support

Trigger module: Programmatically assigning actions to triggers

Hi, I try to make a module I develop to assign some actions it defines through the hook_action to appropriate triggers. Looking at the documentation, searching the web and looking at trigger module code I didn't find any function for actually assign an action to a trigger from inside a module's code. Studding the code of trigger.admin.inc I came up with this piece of code for doing what I want:

/*Assign actions to triggers. A dirty hack.*/
        require getcwd().'/'.drupal_get_path('module','trigger').'/'.'trigger.admin.inc';
        foreach (actions_actions_map(actions_get_all_actions()) as $aid => $action) {
                if ($action['callback'] == 'mymodule_add_user_action') {
                        $form_values['aid'] = $aid;
                        $form_values['hook'] = 'user';
                        $form_values['operation'] = 'insert';
                        trigger_assign_form_submit(array(), array('values' => $form_values));
                        drupal_set_message(t('MyModule add user action assigned.'));
                }
        }

I really don't like much this "hack". Is there a more straightforward way to do what I want through my module? Have I missed anything? What do you suggest?

Comments

Not so hacky to me

Subscribing. I think this might not be as hacky as you may think, Form API is probably the best bet if there is no existing api for you to interact with Drupal's core. I've seen people do that with creating roles (http://drupal.org/node/283261).

Agreed, not so hacky

I agree with Ye. It's not so hacky to use the Forms API when that's all you have.

I would recommend in the first line of code to use module_load_include() instead of require(), which will save some headaches, including edge cases like people moving the module out of the core folder and possibly requiring the file somewhere else in code.

(Username formerly my full name, Richard Eriksson.)

nobody click here