summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpfrenssen2017-07-11 19:47:17 (GMT)
committerRenato Goncalves de Araujo2017-07-11 19:47:17 (GMT)
commit85bbef808a96c6088969fa223ebedc435dc4fabb (patch)
treef5856e2c305e90d139ec8a79a11645f2c985786c
parent3b89238ea2862730a93f6d083360e17d4bc05fbb (diff)
Issue #1724132 by pfrenssen, RenatoG: Provide a function to easily retrieve references from nodes
-rw-r--r--node_reference/node_reference.module36
1 files changed, 36 insertions, 0 deletions
diff --git a/node_reference/node_reference.module b/node_reference/node_reference.module
index 6480f6c..b4f52ac 100644
--- a/node_reference/node_reference.module
+++ b/node_reference/node_reference.module
@@ -1297,3 +1297,39 @@ function node_reference_views_filter_options($field_name) {
return $options;
}
+
+/**
+ * Returns nodes of the given type(s) that are referred to in a given node.
+ *
+ * @param object $node
+ * The node object from which the references should be retrieved.
+ * @param array $types
+ * An array listing the desired content types. If omitted all referenced nodes
+ * are returned, regardless of type.
+ *
+ * @return array
+ * An associative array of nodes of the given types that are referred to in
+ * the given node, keyed by nid.
+ */
+function node_reference_get_referred_nodes($node, array $types = array()) {
+
+ $nodes = array();
+
+ if (!isset($node->type)) {
+ return $nodes;
+ }
+
+ foreach (field_info_fields() as $field_name => $field_info) {
+ if ($field_info['type'] == 'node_reference' && (empty($types) || array_intersect($types, $field_info['settings']['referenceable_types'])) && array_intersect(array($node->type), $field_info['bundles']['node'])) {
+ $field_items = field_get_items('node', $node, $field_name);
+ if (is_array($field_items) && !empty($field_items)) {
+ foreach ($field_items as $field_item) {
+ if (($referred_node = node_load($field_item['nid'])) && (empty($types) || in_array($referred_node->type, $types))) {
+ $nodes[$field_item['nid']] = $referred_node;
+ }
+ }
+ }
+ }
+ }
+ return $nodes;
+}