diff --git a/node_reference/node_reference.api.php b/node_reference/node_reference.api.php new file mode 100644 index 0000000..0519642 --- /dev/null +++ b/node_reference/node_reference.api.php @@ -0,0 +1,56 @@ + array( + * 'title' => The node title, + * 'rendered' => The text to display in widgets (can be HTML) + * ), + * ... + * ) + * } + * or FALSE if there is no candidates. + */ +function hook_node_reference_FIELD_potential_references($field, $options) { + $query = db_select('node', 'n') + ->addField('n', 'nid') + ->addField('n', 'title', 'node_title') + ->condition('n.type', $field['settings']['referenceable_types'], 'IN'); + $result = $query->execute()->fetchAll(); + $references = array(); + foreach ($result as $node) { + $references[$node->nid] = array( + 'title' => $node->node_title, + 'rendered' => check_plain($node->node_title), + ); + } + return array($references); +} \ No newline at end of file diff --git a/node_reference/node_reference.module b/node_reference/node_reference.module index 417f258..6518122 100755 --- a/node_reference/node_reference.module +++ b/node_reference/node_reference.module @@ -801,6 +801,10 @@ function node_reference_potential_references($field, $options = array()) { } if ($references === FALSE) { + $references = _node_reference_potential_references_from_hook($field, $options); + } + + if ($references === FALSE) { $references = _node_reference_potential_references_standard($field, $options); } @@ -823,6 +827,32 @@ function _node_reference_potential_references_views($field, $options) { } /** + * Retrieves candidate referenceable nodes using hook_node_reference_FIELD_potential_references + */ +function _node_reference_potential_references_from_hook($field, $options) { + $references = array(); + $hook_name = 'node_reference_' . $field['field_name'] . '_potential_references'; + foreach (module_invoke_all($hook_name, $field, $options) as $refs) { + foreach ($refs as $ref_nid => $ref_titles) { + $references[$ref_nid] = $ref_titles; + } + } + uasort($references, '_node_reference_sort_ref'); + if ($options['limit']) { + $references = array_slice($references, $options['limit']); + } + + return $references; +} + +function _node_reference_sort_ref($x, $y) { + if ($x['rendered'] === $y['rendered']) { + return 0; + } + return $x['rendered'] < $y['rendered'] ? -1 : 1; +} + +/** * Helper function for node_reference_potential_references(). * * List of referenceable nodes defined by content types.