Hi,

I want to add a new newsletter category after a new node is saved. I've created a rule that adds a new term, but the category is not created...
Is it possible to create a new category through rules module? Maybe with a bit of PHP?
Is there any API function to solve this issue?

Thanks!

Comments

strainyy’s picture

Issue summary: View changes

Any luck with this? I'd like to achieve the same thing...

strainyy’s picture

I've managed to create a new Newsletter whenever a user adds a taxonomy term of a certain type. Maybe this will help you work out how to do it with nodes.

Within your rule configuration, create an action of the type "Execute Custom PHP Code".
Enter the following code, but omit the tags as they are just for formatting this comment.

//Get the term ID
$simplenews_taxonomy_id = $term->tid;

//Create default Simplenews category object
$category = new stdClass();
$category->tid = $simplenews_taxonomy_id;
$category->format = 'html';
$category->priority = 0;
$category->receipt = 0;
$category->from_name = variable_get('site_name', 'Asist Local');
$category->email_subject = "[" . "node:title" . "]";
$category->from_address = variable_get('site_mail', '');
$category->hyperlinks = 1;
$category->new_account = 'off';
$category->opt_inout = 'double';
$category->block = 1;
$category->name = '';
$category->description = '';
$category->$simplenews_taxonomy_id = $simplenews_taxonomy_id;
 
//Finally we can save category!
simplenews_category_save($category);

And to delete the category when a certain taxonomy term is deleted create a new rule that reacts to the taxonomy term deleted event. Again, create an action of the type "Execute Custom PHP code". Simply place the following code in the text area:

//Deletes simplenews newsletter related to consultation term
$simplenews_taxonomy_id = $term->tid;

//Delete SimpleNews Category
$category = simplenews_category_load($simplenews_taxonomy_id);
simplenews_category_delete($category);

watchdog("SimpleNews", "SimpleNews Category Deleted: '" . $term->name . "'");

Once again, remember to delete the openning and closing PHP tags as they are just for formatting in this comment.

Hopefully this helps anyone having the same sort of SimpleNews automation issues.

olak’s picture

i have a similar usecase. when a node of certain type is created, i'd like a new newsletter category of the same name. as i understand, creating a taxonomy term of "newsletter" doesnt create a newsletter. you have to add it in admin/config/services/simplenews/add
and i dont see such an action in simplenews rules. @strainyy , can you post your whole rule (export it first)? it would be very helpful, to see how you implemented it.

neoxavier’s picture

Actually you dont need simplenews rules to do this.

In rules action, you need to pick create "new entity" then pick "simplenews Newsletter" as entity type

Note: in version 2 simplenews use entity rather than taxonomy category. in version 2 there is no newsletter category

techn0guy’s picture

@neoxavier

I don't see this option under the new entity field, am I missing something here? Also when I try using the custom PHP code, the name of the newsletter category doesn't populate... when looking into the module code it seems that a taxonomy term needs to be create too, but I can't get that to work

neoxavier’s picture

@techn0guy are you using simplenews version 2, because the version 1 uses taxonomy category and version 2 uses entity

Sebastian Hagens’s picture

Version: 7.x-2.x-dev » 7.x-1.1

This is how I managed it to get a working solution (version 7.x-1.1) to create a Newsletter category on a node save event:

Before the action where the PHP code is executed from #2 I've added these actions:

1. Fetch new entity (entity type: Taxonomy term)
2. Save entity (force save checked on true)

The taxonomy term name and category name are the same as the provided node:title value.

The export of my rule:

{ "rules_create_newsletter_category_when_new_og_is_created" : {
    "LABEL" : "Create newsletter category when new OG is created",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "node_insert--group" : { "bundle" : "group" } },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "taxonomy_term",
            "param_name" : "[node:title]",
            "param_vocabulary" : "newsletter"
          },
          "PROVIDE" : { "entity_created" : { "term" : "Term" } }
        }
      },
      { "entity_save" : { "data" : [ "term" ], "immediate" : "1" } },
      { "php_eval" : { "code" : "$simplenews_taxonomy_id = $term-\u003Etid;\r\n\r\n\/\/Create default Simplenews category object\r\n$category = new stdClass();\r\n$category-\u003Etid = $simplenews_taxonomy_id;\r\n$category-\u003Eformat = \u0027html\u0027;\r\n$category-\u003Epriority = 0;\r\n$category-\u003Ereceipt = 0;\r\n$category-\u003Efrom_name = variable_get(\u0027site_name\u0027, \u0027Asist Local\u0027);\r\n$category-\u003Eemail_subject = \u0022[\u0022 . \u0022node:title\u0022 . \u0022]\u0022;\r\n$category-\u003Efrom_address = variable_get(\u0027site_mail\u0027, \u0027\u0027);\r\n$category-\u003Ehyperlinks = 1;\r\n$category-\u003Enew_account = \u0027off\u0027;\r\n$category-\u003Eopt_inout = \u0027double\u0027;\r\n$category-\u003Eblock = 1;\r\n$category-\u003Ename = \u0027[term:name]\u0027;\r\n$category-\u003Edescription = \u0027\u0027;\r\n$category-\u003E$simplenews_taxonomy_id = $simplenews_taxonomy_id;\r\n\r\n\/\/Finally we can save category!\r\nsimplenews_category_save($category);" } }
    ]
  }
}