Closed (fixed)
Project:
Rules
Version:
7.x-2.0-rc2
Component:
Rules Core
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
30 Dec 2011 at 06:43 UTC
Updated:
9 Dec 2016 at 20:58 UTC
Jump to comment: Most recent
Comments
Comment #1
Desi Raaj commentedComment #2
rwilson0429 commentedGlad you figured it out. I (and probably lots of others) have been trying to figure out the same thing. Would you share your solution? Thx.
Comment #3
webroru commented+1
Comment #4
Desi Raaj commentedThere are multiple way of doing this, but here is mine:
My Scenario: I wanted to increment a value of a user field that I created by 1 every time the user saved a node.
Rule:
Event: After saving new content
Conditions: Content is of type
- Data Selector: Node
- Value: [pick your content type(s)]
Action:
Fetch entity by id
-Value: User
-Data Selector: node:author:uid
-Provided variables: [leave as is or change if you want]
Execute custom PHP code
-$entity_fetched->"your_field"[LANGUAGE_NONE][0]['value'] = $entity_fetched->"your_field"[LANGUAGE_NONE][0]['value'] + 1;
Save Entity
-Data Selector: node:author
Comment #5
rwilson0429 commentedThanks. I want to automatically assign a client ID to a node when it is created. I want the id's to increment by 1 based on the highest value of the ids that have already been saved. Thought about just using the nid but, it is not specific to the content type. I would have thought that this would be simple to do without php coding but, I've run into a stumbling block. I will try your method to see if it works for me. Thanks again for posting your solution.
Comment #7
IWasBornToWin commentedat #5 you should be able to do that in the backend at mysql. If you can get to the field in the structure of the table there is a setting you can check to increment the value of a field by 1.
Comment #8
rwilson0429 commented@IWasBornToWin: Interesting. I am familiar with mysql ability to auto-increment a field's value but, I didn't know it would work properly in Drupal's table schema. I will give it a try. Thanks.
For now, I have accomplished the assignment of a value to the client number field using the Computed fields module and custom php code:
Comment #9
spineless commentedI am getting a weird error when I attempt to create a rule using php. What I want to do is increment a field when a comment is added. Here is my rule.
{ "rules_increment_comment_count" : {
"LABEL" : "increment_comment_count",
"PLUGIN" : "reaction rule",
"TAGS" : [ "Project_Management" ],
"REQUIRES" : [ "rules", "php", "comment" ],
"ON" : [ "comment_insert" ],
"IF" : [
{ "node_is_of_type" : {
"node" : [ "comment:node" ],
"type" : { "value" : { "mvp_customer_question" : "mvp_customer_question" } }
}
}
],
"DO" : [
{ "entity_fetch" : {
"USING" : { "type" : "node", "id" : [ "comment:node:field-total-comment-count" ] },
"PROVIDE" : { "entity_fetched" : { "entity_fetched" : "Fetched entity" } }
}
},
{ "php_eval" : { "code" : "$entity_fetched-\u003E\u0022field_total_comment_count\u0022[LANGUAGE_NONE][0][\u0027value\u0027] = $entity_fetched-\u003E\u0022field_total_comment_count\u0022[LANGUAGE_NONE][0][\u0027value\u0027] + 1;" } },
{ "entity_save" : { "data" : [ "entity-fetched" ] } }
]
}
}
When I execute this rule I get the following Rules error.
0 ms Rule incrament_comment_count fires.
1.541 ms Evaluating the action entity_fetch. [edit]
4.201 ms Unable to load node with id "4"
5.31 ms Unable to evaluate action entity_fetch. [edit]
5.585 ms Evaluating the action php_eval. [edit]
6.758 ms Unable to get variable entity_fetched, it is not defined.
7.884 ms Unable to evaluate action php_eval. [edit]
9.043 ms Unable to get variable entity_fetched, it is not defined.
10.114 ms Unable to evaluate action entity_save. [edit]
10.214 ms Rule increment_comment_count has fired.
The number 4 is the value in the field. The idea is that every time someone adds a comment the comment count increments. I need this because I want to display the approved and unapproved comments.
Any help would be great.
Comment #10
spineless commentedI was able to get this to work with the help of spovlot.
All you need to do is in actions
calculate a value
set a data value.
Comment #10.0
spineless commentedSolved
Comment #11
nanabrownee commentedHas anyone managed to sort it out with rules conditional?
Can't get it to work!
Or an alternative?
this is my export:
{ "rules_set_field_int_breakout_when_session_flagged" : {
"LABEL" : "Set field int breakout when session flagged",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "flag", "rules_conditional", "rules" ],
"ON" : {
"flag_flagged_int_breakout_1" : [],
"flag_flagged_external_breakout_session" : []
},
"DO" : [
{ "flag_fetch_node_by_user" : {
"USING" : { "flag" : "int_breakout_1", "flagging_user" : [ "flagging_user" ] },
"PROVIDE" : { "content_flagged_by_user" : { "content_flagged_by_user" : "Content flagged by user" } }
}
},
{ "CONDITIONAL" : [
{
"IF" : { "node_is_of_type" : {
"node" : [ "flagged-node" ],
"type" : { "value" : { "session" : "session" } }
}
},
"DO" : [
{ "CONDITIONAL" : [
{
"IF" : { "NOT data_is" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
},
"DO" : [
{
"WHILE" : { "data_is" : {
"data" : [ "flagged-node:field-assigned-seats" ],
"op" : "\u003C",
"value" : [ "flagged-node:field-max-seats" ]
}
},
"DO" : [
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
},
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-session-breakout" ],
"value" : [ "flagged-node" ]
}
},
{ "data_calc" : {
"USING" : {
"input_1" : [ "flagged-node:field-assigned-seats" ],
"op" : "+",
"input_2" : "1"
},
"PROVIDE" : { "result" : { "result" : "Calculation result" } }
}
},
{ "data_set" : {
"data" : [ "flagged-node:field-assigned-seats" ],
"value" : [ "result" ]
}
}
]
}
]
}
]
}
]
},
{
"ELSE IF" : { "node_is_of_type" : {
"node" : [ "flagged-node" ],
"type" : { "value" : { "external_session" : "external_session" } }
}
},
"DO" : [
{ "CONDITIONAL" : [
{
"IF" : { "NOT data_is" : {
"data" : [ "flagging-user:profile-main:field-external-session-selected" ],
"value" : "Yes"
}
},
"DO" : [
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-session-breakout" ],
"value" : [ "flagged-node" ]
}
},
{ "data_set" : {
"data" : [ "flagging-user:profile-main:field-internal-breakout-selected" ],
"value" : "Yes"
}
}
]
}
]
}
]
}
]
}
]
}
}
Comment #12
najamfzl commentedOne easy way is to use "offset" in "Set a data value" action. I came across this problem when I needed to count the items a site user has added to his cart or temporary storage. I added a variable (integer type) before initiating a loop on the nodes and then add node to a list (Add item to a list action) and after that used "Set data value" action with both field same (my variable) but just added "1" in offset. If someone is doing the same thing, it is very important to initialize the variable with zero (0) value and do not leave it blank. I hope it helps someone as it did to me.