/[drupal]/contributions/modules/ReferencedByFilter/ReferencedByFilter.module
ViewVC logotype

Contents of /contributions/modules/ReferencedByFilter/ReferencedByFilter.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Mon Sep 8 08:43:08 2008 UTC (14 months, 2 weeks ago) by amitaibu
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +3 -1 lines
File MIME type: text/x-php
#275525 by Veggieryan - query according to the node's correct vid.
1 <?php
2 // $Id: ReferencedByFilter.module,v 1.12 2008/06/22 16:06:25 amitaibu Exp $
3
4 /**
5 * @file
6 * This module allows views to query the nodes referenced by a certain node reference field.
7 *
8 * You can use it to filter results from a node reference, and show referenced nodes with pager.
9 * Results are returned in the order they wree entered in the node reference field.
10 */
11
12
13 /**
14 * Implementation of hook_views_arguments().
15 */
16 function referencedbyfilter_views_arguments() {
17 $arguments = array(
18 'referencedbyfilter' => array(
19 'name' => t('Node Reference: Views on node reference'),
20 'handler' => t('referencedbyfilter_arg_nid'),
21 'option' => array(
22 '#type' => 'select',
23 '#options' => referencedbyfilter_retrieve_existing_reference_fields(),
24 ),
25 'help' => t('This argument allows views to query the nodes referenced by a certain node reference field.
26 In the options field specify the referencing field.'),
27 ),
28 );
29 return $arguments;
30 }
31
32 /**
33 * Handler function for referencedbyfilter_views_arguments().
34 */
35 function referencedbyfilter_arg_nid($op, &$query, $argtype, $arg = NULL) {
36 switch ($op) {
37 case 'filter':
38 $nid = (int)$arg;
39 if (is_int($nid)) {
40 $node_reference_table = 'content_'. $argtype['options'];
41 $referencing_field = $argtype['options'];
42 $node = node_load($nid);
43 if (!empty($node->$referencing_field)) {
44 $joininfo = array(
45 'type' => 'LEFT',
46 'left' => array(
47 'table' => 'node',
48 'field' => 'nid',
49 ),
50 'right' => array(
51 // Join to the referenced field.
52 'field' => $referencing_field .'_nid',
53 ),
54 );
55 $query->add_table($node_reference_table, FALSE, 1, $joininfo);
56 $query->add_field($referencing_field .'_nid', $node_reference_table);
57 $query->add_where($node_reference_table. '.nid = %d', $nid);
58 $vid=db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $nid));
59 $query->add_where($node_reference_table .'.vid = %d', $vid);
60 }
61 }
62 break;
63 }
64 }
65
66 /**
67 * Helper function to get all existing node reference fields.
68 */
69 function referencedbyfilter_retrieve_existing_reference_fields() {
70 $fields = content_fields();
71 foreach ($fields as $field) {
72 if ($field['type'] == 'nodereference') {
73 $existing_reference_fields[$field['field_name']] = $field['field_name'];
74 }
75 }
76 return $existing_reference_fields ;
77 }

  ViewVC Help
Powered by ViewVC 1.1.2