-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxref.module
285 lines (267 loc) · 8.2 KB
/
xref.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
// $Id$
/**
* Implementation of hook_block().
*/
function xref_block($op = 'list', $delta = 0) {
switch ($op) {
case 'list':
return array(
'create' => array('info' => t('Crossref: Create referrer')),
'xref' => array('info' => t('Crossref: Referrers')),
);
case 'view':
switch ($delta) {
case 'create':
$node = menu_get_object();
if ($node && !empty($node->nid)) {
$xref_links = xref_links($node->type, $node->nid);
if (!empty($xref_links)) {
return array('subject' => t('Reference this'), 'content' => theme('links', $xref_links));
}
}
break;
case 'xref':
$node = menu_get_object();
if ($node) {
$xref_view = xref_view($node->type, $node->nid);
if (!empty($xref_view)) {
return array('content' => $xref_view);
}
}
break;
}
break;
}
}
/**
* Implemenation of hook_nodeapi().
*/
function xref_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'view':
// Node page
if ($page && !$teaser) {
// Xref links
if (variable_get('xref_links_'. $node->type, 'template') == 'node') {
$xref_links = xref_links($node->type, $node->nid);
if (!empty($xref_links)) {
$links = theme('links', $xref_links);
$node->content['xref_links'] = array(
'#weight' => -10,
'#value' => theme('xref_links', $links, t('Reference this via')),
);
}
}
// Xref view
if (variable_get('xref_view_'. $node->type, true)) {
$xref_view = xref_view($node->type, $node->nid);
if (!empty($xref_view)) {
$node->content['xref_view'] = array('#weight' => 100, '#value' => $xref_view);
}
}
}
break;
}
}
/**
* Implementation of hook_form_alter.
*
* Adds xref setting to nodetype editing pages.
*/
function xref_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$fields = xref_fields($form['#node_type']->type);
if (!empty($fields)) {
$types = array();
foreach ($fields as $k => $v) {
$types[$v['type_name']] = $v['type_name'];
}
$form['xref'] = array(
'#type' => 'fieldset',
'#title' => t('Cross reference settings'),
'#description' => t('This type of content can be referenced by: %type', array('%type' => implode(', ', $types))),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['xref']['xref_links'] = array(
'#type' => 'select',
'#title' => t('Display "Reference this via" links'),
'#options' => array(
FALSE => '-- '. t('Don\'t display') .' --',
'template' => t('Provide $xref_links variable in node.tpl.php'),
'node' => t('Display in node content'),
),
'#default_value' => variable_get('xref_links_'. $form['#node_type']->type, 'template'),
);
$form['xref']['xref_view'] = array(
'#type' => 'checkbox',
'#title' => t('Display summary of referencing content'),
'#default_value' => variable_get('xref_view_'. $form['#node_type']->type, true),
);
}
}
}
/**
* Implementation of hook_theme().
*/
function xref_theme() {
return array('xref_links' => array());
}
/**
* Theme xref links.
*/
function theme_xref_links($links, $label) {
return "<div class='xref clear-block'><div class='label'>$label</div>$links</div>";
}
/**
* Preprocessor for theme_node().
*/
function xref_preprocess_node(&$vars) {
$node = $vars['node'];
if (variable_get('xref_links_'. $node->type, 'template') == 'template') {
$links = xref_links($node->type, $node->nid);
if (!empty($links)) {
$links = theme('links', $links);
$vars['xref_links'] = theme('xref_links', $links, t('Reference this'));
}
}
}
/**
* API function.
*/
function xref_fields($nodetype, $reset = FALSE) {
static $fields;
if (!isset($fields) || $reset) {
$fields = array();
}
if (!isset($fields[$nodetype])) {
$fields[$nodetype] = array();
// Get nodereference fields that may reference this node
foreach (content_fields() as $field) {
if ($field['type'] == 'nodereference') {
foreach ($field['referenceable_types'] as $type) {
if ($type === $nodetype) {
$fields[$nodetype][$field['field_name']] = $field;
}
}
}
}
}
return $fields[$nodetype];
}
/**
* Provides links to create any nodes that could potentially reference this node.
*/
function xref_links($nodetype, $nid) {
$fields = array_keys(xref_fields($nodetype));
if ($fields) {
// Wrap each field in quotes in prep for SQL IN()
foreach ($fields as $k => $v) {
$fields[$k] = "'$v'";
}
// Build link of nodetypes with instances of all valid referencing fields
$links = array();
$node_types = node_get_types();
$fields = implode(',', $fields);
$result = db_query("SELECT field_name, type_name FROM {content_node_field_instance} WHERE widget_active = 1 AND field_name IN ($fields)");
while ($row = db_fetch_object($result)) {
if (node_access('create', $row->type_name)) {
$node = node_load($nid);
$type = $row->type_name;
$item = menu_get_item('node/add/'. str_replace('_', '-', $type));
if ($item['access']) {
$links[$type] = array(
'title' => t('via @nodetype', array('@nodetype' => $item['title'])),
'href' => $item['href'],
'query' => "edit[{$row->field_name}][0][nid][nid]= ". check_plain($node->title) ." [nid: {$nid}]",
);
}
}
}
return $links;
}
return '';
}
/**
* Provides a view of any nodes that reference the given node.
*
* @param $nodetype
* the node type of the content to detect references to.
*
* @param $nid
* the nid of the content to detect references to.
*
* @param $fieldname
* Optional. The id of the particular field to be used to build the view. If
* omitted the first field returned by xref_fields() will be used.
*
* @param $viewname
* Optional.
*/
function xref_view($nodetype, $nid, $fieldname = null, $viewname = null) {
$output = '';
$fields = xref_fields($nodetype);
if (empty($fields)) {
return '';
}
if (!empty($fieldname)) {
$field = $fields[$fieldname];
}
else {
$field = array_shift($fields);
}
if (empty($viewname)) {
$viewname = 'xref';
}
// Xref nodes
if ($view = views_get_view($viewname)) {
$field_info = content_database_info($field);
module_load_include('inc', 'content', 'includes/views/content.views');
$view->set_display();
$handler = $view->display['default']->handler;
$handler->override_option('arguments', array(
$field['field_name'] => array(
'default_action' => 'not found',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'php',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 0,
'id' => $field['field_name'] .'_nid',
'table' => content_views_tablename($field),
'field' => $field['field_name'] .'_nid',
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '',
'default_argument_php' => '',
'validate_argument_node_type' => array(),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(),
'validate_argument_type' => 'tid',
'validate_argument_is_member' => 0,
'validate_argument_php' => '',
),
));
$view->pre_execute(array($nid));
$view->execute();
if (count($view->result)) {
$output = $view->render();
}
}
return $output;
}
/**
* Implementation of hook_views_api().
*/
function xref_views_api() {
return array('api' => 2);
}