1

My aim is to trigger a rule via Ajax.

Until now I create a custom module called "iscrizione"

Here my iscrizione.rules.inc

function iscrizione_rules_event_info() {    
$items = array();

$items['iscrizione_evento'] = array(
  'label' => 'Iscrizione all\'evento',
  'group' => t('Links'),
  'variables' => array(
    'path' => array(
      'type' => 'array',
      'label' => t('Path accessed'),
      'description' => t('The path which has just been accessed.'),
    ),
    'account' => array(
      'type' => 'user',
      'label' => t('updated user'),
    ),
  ),
);

return $items; 
}

Here my iscrizione.module

function iscrizione_menu() {
$items = array();

$items['conferma/%'] = array(
'title' => 'Conferma Iscrizione',
'page callback' => 'iscrizione_evento',
'page arguments' => array(1),
'access arguments' => array('access content'),
);

return $items;
}

function iscrizione_evento($arg) {
echo $arg;
// rules_invoke_component('rules_stampa_messaggio', $arg);
rules_invoke_event('rules_iscrizione_iscrizione_evento', $arg);
}

I tried both functions (invoke event and component), but if I type the url I can see only the $arg printed.

No action was fired (as actions I put " Show a message on the site" and a "Create entity")

Any help?

  • 1
    If this is exact code from your module, then you're calling wrong rule in rules_invoke_event(). The rule name should be iscrizione_evento. i.e. rules_invoke_event('iscrizione_evento', $arg);. Check this link. – Yogesh Dec 15 '16 at 10:10
2

Within $items of your iscrizione_rules_event_info(), you used iscrizione_evento. So in your iscrizione.module you should refer to that. Try this variation of it:

function iscrizione_menu() {
$items = array();

$items['conferma/%'] = array(
'title' => 'Conferma Iscrizione',
'page callback' => 'iscrizione_evento',
'page arguments' => array(1),
'access arguments' => array('access content'),
);

return $items;
}

function iscrizione_evento($arg) {
echo $arg;
// rules_invoke_component('rules_stampa_messaggio', $arg);
rules_invoke_event('iscrizione_evento', $arg);
}

Maybe it would also help if you'd slightly adapt the readability of your code, by not using iscrizione_evento for different things. How about, eg, these variations of it:

  • menu_iscrizione_evento.
  • rules_event_iscrizione.
  • Thanks for your answer, from the examples I read in other pages I understood that the rules_invoke_event first argument was rules-machinename + rules_event-machinename. Now is working, thanks a lot! P.S. I modified the name as iscrizione_module and rules_event_iscrizione, more readable now ;) – Ponzio Pilato Dec 15 '16 at 13:58
2

Using the accepted answer I fixed my code, but then I ran into another problem, i.e.:

Notice: Undefined index: name in EntityMetadataWrapper->debugIdentifierLocation() (linea 246 di /home/contactbologna/public_html/sites/all/modules/entity/in‌​cludes/entity.wrappe‌​r.inc).

This new problem was around the variables type on event info.

The final version of the code now looks like this:

function iscrizione_module_menu() {
  $items['conferma/%'] = array(
  'title' => 'Conferma Iscrizione',
  'page callback' => 'iscrizione_evento_callback',
  'page arguments' => array(1),
  'access arguments' => array('access content'),
);
return $items;
}

function iscrizione_evento_callback($arg) {
//global $user;
if (user_is_logged_in()) 
{ 
  $user = user_uid_optional_load(); 
}
$path[] = $arg;
// rules_invoke_component('rules_stampa_messaggio', $arg);
rules_invoke_event('rules_event_iscrizione', $path, $user);
}

and

function iscrizione_module_rules_event_info() {
  $items = array();
  $items['rules_event_iscrizione'] = array(
    'label' => 'Iscrizione all\'evento',
    'group' => t('Links'),
    'variables' => array(
      'path' => array(
        'type' => 'array',
        'label' => t('Path accessed'),
        'description' => t('The path which has just been accessed.'),
      ),
      'account' => array(
        'type' => 'user',
        'label' => t('updated user'),
      ),
    ),
  );
return $items;
}

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.