Posted by AndrzejG on September 23, 2011 at 3:04am
Is any way to get values of custom fields in the entity fetched (for example from entity reference), not created ones?
Use case: I have content type Global for input some data, text and decimal, and some reference fields to other entities. I want Rules to modify some field values in referenced entities based on the values inputed in Global. Rules are triggered on saving new Global or (in other cases) on updating Global.
Maybe should I use some modules like Entity Relationships or so?
Comments
"Entity has field"
Using the "Entity has field" (or "content is of type") condition will make the fields become available with data selection.
Check out http://nodeone.se/node/987 for a screencast showing how this can be done.
Good luck!
//Johan Falk
**
Check out NodeOne's Drupal Learning Library! 190+ screencasts and exercises, for Drupal introduction, advanced configuration, and coding. Creative Commons license!
I tried this two methods,
I tried this two methods, however, it doesn't work.
For example I want add new decimal variable as entity-fetched:0:field-quantity (where last segment after colon is hand typed in), the error message is:
The integrity check failed with the following error message: "Data selector entity-fetched:0:field-quantity for parameter value is invalid.", despite there IS a decimal value inside that field.
The same is if I want to set a data value.
What conditions?
If you have to type in the last part by hand, it means that Rules doesn't know about your field. Also, that you have the name "entity-fetched" suggests to me that you have manually loaded the entity with a rules action, rather than just using the tokens straight off.
Try adding a condition "entity has field" to first tell Rules that your loaded node(?) has a reference field.
Then add another condition "entity has field" to check that the referenced entity has a field as well – checking on the entity "node:field_your_ref_field:0".
I hope this makes some kind of sense. Good luck!
//Johan Falk
**
Check out NodeOne's Drupal Learning Library! 190+ screencasts and exercises, for Drupal introduction, advanced configuration, and coding. Creative Commons license!
Thanks Johan,The problem was
Thanks Johan,
The problem was just wrong thinking on my part. My brain wanted to use rules scheduler to schedule the maintenance that I wanted done, but now I know Rules does not work that way. I was able to get it working, by using Rules Scheduler correctly. It turned out to be very easy.
1) Create a component (rule set)
2) Add a rule to that rule set
3) Create a triggered rule to schedule execution of the rule set
The part that I did not find intuitive was need for the triggered rule. I was thinking more of a rule that just batch processed all nodes that met certain condition at a specified interval. Drupal however needs the event to get everything started, which just means doing the scheduling when the node is created rather than it for all nodes by batch at a specified interval.
Thanks again for the help,
-=Daniel=-
LOL sorry wrong thread...
LOL sorry wrong thread... thanks for the help anyway ;)
still not working for me
I have a similar situation that isn't working. I have a node that references another node and I want to set a value of a field in the referenced node but the field is not available in the data selector. What's the best way to set the value of that field with Rules? I've tried using "Entity has field" conditions for both the primary node and the referenced node but the fields on the referenced node are still not available...
Apparently, the ordering is important
I put the "Entity has field" conditions at the top of the list and the "Content is of type" and "Data comparison" further down, and now the fields in the referenced entity are available to me. This "trick" should probably be documented as there are probably a lot of people who are going to get bogged down by this...
Interesting
Interesting. Do You mean "Entity having field" is that of referenced type?
(I understood that in Conditions You type first something like "Entity has field" - node as a parameter, and [node:field-custom-of referenced-node] as a value. Than, below, You input Data Comparison with the referencing node. Am I right?).
Hmm, it cannot work, impossible as [node] is referencing entity, not referenced.
OK, finally I have it worked.
Sadly, I didn't understand Itangalo's first comment and lost much time.
Thank both of You.
The order matters!
Thanks dude! You are right the order of the condition matters!
Look at the comparison I did https://www.diffchecker.com/7d20qqpb (on the left is without order and in the right is ordered)
best regards
CEO at heydru! / Senior Developer at Lullabot
Also, kind of a pain...
It's also kind of a pain in that I have to add an "Entity has field" condition for each and every field in the referenced entity that I might need to use in my rule. (I need to copy values from one node's fields to the other.) Is there an easier way?
It also doesn't seem to work for computed fields.
Check for the bundle
You should be able to check for the specific bundle. For example, if you need to copy values from one commerce product to another, you could add a Data comparison condition on commerce-product:type equals product (or whatever commerce product bundle you are expecting). This should then give you all of the fields available on that bundle.
I'll try that but...
I'll try that but my nodes are of different types and I anticipate I'll eventually need to do it with non-node entities as well. Even so, it should still work. If not, I'll manage. I understand it's much cleaner without exposing all of those possibilities by default. Thanks for all the help!
Finally...
Finally, have You solved a problem? I didn't.related problem
Hello,
I've been trying to manage how to use rules for a simple calculation in a node trying to follow the instructions you gave but I cannot make it as rules doesn't appear to be aware of fields from referenced nodes.
I will explain it:
I have a content type "ad" that has a reference field (created with entity reference module) called "fare". This field is referencing a "fare" that has another field "price". The content type "ad" also has a field called "number of issues contracted". Finally the "ad" has another field called "total price". I want to populate the field "total price" with multiplying the "number of issues contracted" by the "price" of the "fare".
Any way I tried to do it, rules can never recover the value of the price form the "fare" content type as the node I am saving is the "ad" one.
At the moment I am watching all the learning series from nodeone by Johan Falk, that by the way are giving me lots of ideas for other tasks but for the moment I cannot fin a way to manage my problem.
Any help will be welcome!
Well, watching the videos and
Well,
watching the videos and reading again the comments from everybody made me solve it finally. I'll write it down just in case someone has the same problem: even though really is a compilation of what the others said:
On event: before saving content
Conditions:
1. Entity has field
Entity: Data selector: node
Field Value: field_ad_fare
2. Entity has field
Entity: Data selector: node: field_ad_fare
Field Value: field_price
3. Entity is of type
Content Data selector: node
Content types Value: "ad"
Actions:
1. Calculate a value
Input value 1 Data selector: node:field-ad-num-contracted
Operator Value: *
Input value 2 Data selector: node:field-ad-fare:field-price
2. Set a data value
Selected data: node:field-ad-totalprice
Value Data selector: "result"
Well, I hope this helps!