Posted by owntheweb on March 26, 2012 at 4:11pm
Howdy,
I'm attempting to make a two-way node reference using Rules (D7) for an online magazine. Editors reference articles to their issues. I want Rules to load up a referenced issue and reference the article being saved.
In rules I have the following so far:
Events:
- before saving content
Conditions:
- content is type: article
- AND
- node:field-issue:nid is not empty
Actions:
- fetch entity by id (node:field-issue:nid)
- [! I can't seem to find any fields here to edit when using Rules data options other than body, source, author, roles !]
Any thoughts on where to get started? Do I need another module or do I need to build my own module to make this happen? Thanks much in advance for your feedback. :D
Best regards,
Chris

Comments
possible solution: custom action module
Looking deeper at related posts, a custom action module may do the trick:
http://dev.nodeone.se/node/832
and
http://dev.nodeone.se/node/895
source:
http://groups.drupal.org/node/172224
Would you agree this is the best course of action?
Thanks again,
Chris
Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb
owntheweb, I guess you have
owntheweb, I guess you have to check on the bundle/node-type of the second entity before trying to get any fields, as the fields are dependent to the bundle. Therefore you would need an additional ruleset.
working solution
derhasi,
Thanks for your feedback. I wasn't having much luck using additional rulesets or components for this, but I did manage to make a custom action module to make it happen the way I needed. This is a starter band-aid action, but I have just what I need to start developing a full solution using custom actions with Rules (yay!). In case someone else needs an example, here's a somewhat generalized version to look at:
magazine_features.info
name = Magazine Featuresdependencies[] = rules
dependencies[] = references
description = Example custom action for use with Rules that makes use of parameters for two-way node references. When saving an article, this action is used to load up a n issue and reference the article from the issue.
package = Custom Modules
core = 7.x
magazine_features.module
<?php
function magazine_features_rules_action_info() {
$action = array(
'magazine_features_article_to_issue' => array(
'label' => t('Reference issue to article'),
'group' => t('Magazine Features'),
'parameter' => array(
'issue_id' => array(
'type' => 'integer',
'label' => t('Issue'),
),
'article_id' => array(
'type' => 'integer',
'label' => t('Article'),
),
),
),
);
return $action;
}
function magazine_features_article_to_issue($issue_id, $article_id) {
$issue_node = node_load($issue_id);
$match = false;
foreach($issue_node->field_articles['und'] as $article) {
if($article['nid'] == $article_id) {
$match = true;
break;
}
}
if($match == true) {
//no action needed
drupal_set_message(t('This article is already happily assigned to <b><a href="/node/@nid">@issue_title</a></b>.', array('@issue_title' => $issue_node->title, "@nid" => $sw_issue)));
} else {
$issue_node->field_articles['und'][] = array('nid' => $article_id);
node_save($issue_node);
drupal_set_message(t('This article has been successfully referenced in the issue: <b><a href="/node/@nid">@issue_title</a></b>.', array('@issue_title' => $issue_node->title, "@nid" => $article_id)));
}
}
//no closing php recommended
?>
And you can pretend to see editors calmly writing and referencing articles without thinking about it at:
http://www.spacefoundation.org/media/space-watch
:D
Chris
Worlds to explore. Worlds to create.
Blog: http://www.christopherstevens.cc/blog
Twitter: http://www.twitter.com/owntheweb
Nice solution!
Hi Chris,
Had a situation where I needed to duplicate a field from a child node to it's parent (one to one relationship) so using the above, slightly modified, I got it working. So thank you very much for posting that!
Cheers,
Tim.
Thumbs up!
Thank you, Chris your solution worked form me.
Cheers