function nodereference_count_get_count in Nodereference Count 7
Same name and namespace in other branches
- 6 nodereference_count.module \nodereference_count_get_count()
Get the count of node references to a particular node.
Parameters
$settings: The settings for this field instance.
$nid: The nid of the node being referenced.
Return value
A count of the number of references to the node.
1 call to nodereference_count_get_count()
- nodereference_count_field_presave in ./
nodereference_count.module - Implements hook_field_presave().
File
- ./
nodereference_count.module, line 194 - Defines a field type for counting the references to a node.
Code
function nodereference_count_get_count($settings, $nid) {
// Get the db info for the node reference fields.
$db = nodereference_count_get_fields_db($settings['counted_reference_fields']);
if (!empty($db)) {
// Use the first field for the initial query.
$query = db_select('node', 'n');
$query
->fields('n', array(
'nid',
));
$alias = $query
->innerJoin($db[0]['table'], 'nr', '%alias.entity_id = n.nid');
$query
->condition("{$alias}.{$db[0]['column']}", $nid);
if ($settings['count_only_published']) {
$query
->condition('n.status', 1);
}
unset($db[0]);
// Add each additional field to the query via a UNION ALL.
foreach ($db as $d) {
$select = db_select('node', 'n');
$select
->fields('n', array(
'nid',
));
$alias = $select
->innerJoin($d['table'], 'nr', '%alias.entity_id = n.nid');
$select
->condition("{$alias}.{$d['column']}", $nid);
if ($settings['count_only_published']) {
$select
->condition('n.status', 1);
}
$query
->union($select, 'UNION ALL');
}
$query
->addTag('nodereference_count');
return $query
->countQuery()
->execute()
->fetchField();
}
return 0;
}