2

I wrote custom rules action to get a list of active simplenews subscribers, but I can not process it with "loop" cycle. Action should get list of active simplenews subscribers ("entity_fetched" array). Then I pass it to "loop" for processing (and to get "list-item" variable) and there are no errors. But when I try to set status message (add action - system - set message) with "list-item:uid", system says: "Data selector list-item:uid for parameter message is invalid". More, I can not select "list-item" token at all. I think, there are some mistakes when the code returns "entity_fetched" array. There is my code:

<?php

   function custom_simplenews_rules_rules_action_info() {
      $actions = array(
         'custom_simplenews_rules_actions_get_subscribers' => array(
            'label' => t('Get subscribers by Simplenews Newsletters category'),
            'group' => t('Custom'),
            'parameter' => array(
               'category' => array(
                  'type' => 'text',
                  'label' => t('Category'),
                  'options list' => 'custom_simplenews_rules_options_list',
                  'description' => t('Specify category to select users from'),
               ),
            ),
            'provides' => array(
               'entity_fetched' => array('type' => 'list', 'label' => t('Fetched entity')),
            ),
         ),
      );

      return $actions;
   }

   function custom_simplenews_rules_options_list() {
      $taxonomy_vids_objects = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE module = :module", array(':module' => 'simplenews'))->fetchAll();

      $taxonomy_vids_array = array();
      foreach ($taxonomy_vids_objects as $value) {
         $taxonomy_vids_array[$value->vid] = $value->vid;
      }

      $taxonomy_vids = implode(',', $taxonomy_vids_array);
      $taxonomy_tids = db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE vid IN (:vids)", array(':vids' => $taxonomy_vids))->fetchAll();

      $categories = array();
      foreach ($taxonomy_tids as $value) {
         $categories[$value->tid] = $value->name;
      }

      return $categories;
   }

   function custom_simplenews_rules_actions_get_subscribers(&$category) {
      $categories_array = array();
      foreach ($category as $key => $value) {
         $categories_array[$key] = $key;
      }

      $categories = implode(',', $categories_array);
      $subscribers_snids = db_query("SELECT snid FROM {simplenews_subscription} WHERE vid IN (:vid)", array(':vid' => array_keys($categories)))->fetchAll();

      $subscribers_uids = array();
      foreach ($subscribers_snids as $value) {
         $subscribers_uids[$value->snid] = db_query("SELECT uid FROM {simplenews_subscriber} WHERE snid = :snid AND activated = 1", array(':snid' => $value->snid))->fetchAll();
      }

      $users_list = entity_load('user', $subscribers_uids);

      return array('entity_fetched' => array_values($users_list));
   }

Can not determine what I've done wrong. Please help!

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Clive
    Commented Aug 18, 2017 at 7:29

1 Answer 1

1

From the very beginning, I went the wrong way. The problem is solved with the vievs_rules module. I made a view of simplenews subscribers, with relations with subscriber:email and subscription:category, then added filters by term (subscription category), by user (active or not) and subscription status. I used a user entity as resulting variable. Then I just imported the view into views loop (Rules UI) and voila!

Very thanks to @Pierre.Vriens for pointing me to views_rules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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