How to create a group page when creating a new group?
-
14-01-2021 - |
Question
When I create a new group (using the Organic groups module), I want to use the Rules module to create a new group referenced page.
I can create a new page using the Create new entity action, but I haven't been able to set the group reference/audience. I've tried using both Add entity to Group and Set a data value, but I can't figure out how to set the parameters.
{ "rules_create_1st_story_page" : {
"LABEL" : "Create 1st Story Page",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "og" ],
"ON" : { "node_insert--story" : { "bundle" : "story" } },
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "node",
"param_type" : "story_page",
"param_title" : "Page 1 - Start here",
"param_author" : [ "site:current-user" ]
},
"PROVIDE" : { "entity_created" : { "entity_created" : "Created entity" } }
}
},
{ "og_group_content_add" : { "entity" : [ "node" ], "group" : [ "node" ] } }
] }}
La solution
Have a look at the exported rule below, which is a variation of the (exported) rule in your question:
{ "rules_create_1st_group_page" : {
"LABEL" : "Create 1st Group Page",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules" ],
"ON" : [ "node_insert" ],
"IF" : [
{ "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "group" : "group" } } } }
],
"DO" : [
{ "entity_create" : {
"USING" : {
"type" : "node",
"param_type" : "post",
"param_title" : "Page 1 - Start here",
"param_author" : [ "site:current-user" ]
},
"PROVIDE" : { "entity_created" : { "grouppage_created" : "Created GroupPage" } }
}
},
{ "list_add" : { "list" : [ "grouppage-created:og-group-ref" ], "item" : [ "node" ] } },
{ "entity_save" : { "data" : [ "grouppage-created" ], "immediate" : 1 } }
]
}
}
Some more details about "my" variation, about the content types involved:
- "
Group
" is the content type I'm using for my OG groups. - "
Post
" is the content type which has an Entity reference field with machine nameog_group_ref
(and widget = OG reference), so that I can assign nodes of content type "Post" to the appropriate field. Be aware however, this field allows for unlimited values (not sure if that's your case also).
Some more details about the actual rule:
- Rules Event: After saving new content.
- Rules Condition: Content is of type
Group
. Rules Actions:
Create a new entity
, of typePost
(note thegrouppage_created
variable name I've used, instead of just the defaultentity_created
...).Add an item to a list
, whereas the list isgrouppage-created:og-group-ref
(a multi value field, remember, so you cannot/should not use "set a data value, a common mistake when creating custom rules ...), and the item to be added is ... you probably guessed it: the node (of typeGroup
), that just got saved.Save entity
, for thegrouppage_created
(not sure if this Rules Action is really needed, but IMO it's a kind of safe (best?) practise to be sure that this newly createdgrouppage_created
-entity also gets saved).
And yes I actually tested this rule in my own environment ... Sure enough, in my case a new node (of type Post
) got created, after I created a new Group
.
Your turn to adapt your rule (with your content types), to make it similar to my rule here (assuming you also allow multiple values for your og-group-ref
field. If yours is single value, then probably all you need to change is to replace the Rules Action Add an item to a list
by an action to Set a data value
(using similar variables).
PS: I'm assuming your question is about D7.