6 * Defines a field type for referencing one node from another.
11 * Implementation of hook_menu().
13 function nodereference_menu($may_cache) {
17 $items[] = array('path' => 'nodereference/autocomplete', 'title' => t('node reference autocomplete'),
18 'callback' => 'nodereference_autocomplete', 'access' => user_access('access content'), 'type' => MENU_CALLBACK
);
25 * Implementation of hook_field_info().
27 function nodereference_field_info() {
29 'nodereference' => array('label' => t('Node Reference')),
34 * Implementation of hook_field_settings().
36 function nodereference_field_settings($op, $field) {
40 $form['referenceable_types'] = array(
41 '#type' => 'checkboxes',
42 '#title' => t('Content types that can be referenced'),
44 '#default_value' => isset($field['referenceable_types']) ?
$field['referenceable_types'] : array(),
45 '#options' => array_map('check_plain', node_get_types('names')),
47 if (module_exists('views')) {
48 $views = array('--' => '--');
49 $result = db_query("SELECT name FROM {view_view} ORDER BY name");
50 while ($view = db_fetch_array($result)) {
51 $views[t('Existing Views')][$view['name']] = $view['name'];
54 $default_views = _views_get_default_views();
55 foreach ($default_views as
$view) {
56 $views[t('Default Views')][$view->name
] = $view->name
;
58 if (count($views) > 1) {
59 $form['advanced'] = array(
60 '#type' => 'fieldset',
61 '#title' => t('Advanced - Nodes that can be referenced (View)'),
62 '#collapsible' => TRUE
,
63 '#collapsed' => !isset($field['advanced_view']) || $field['advanced_view'] == '--',
65 $form['advanced']['advanced_view'] = array(
67 '#title' => t('View'),
69 '#default_value' => isset($field['advanced_view']) ?
$field['advanced_view'] : '--',
70 '#description' => t('Choose the "Views module" view that selects the nodes that can be referenced.<br>Note :<ul><li>This will discard the "Content types" settings above. Use the view\'s "filters" section instead.</li><li>Use the view\'s "fields" section to display additional informations about candidate nodes on node creation/edition form.</li><li>Use the view\'s "sort criteria" section to determine the order in which candidate nodes will be displayed.</li></ul>'),
72 $form['advanced']['advanced_view_args'] = array(
73 '#type' => 'textfield',
74 '#title' => t('View arguments'),
75 '#default_value' => isset($field['advanced_view_args']) ?
$field['advanced_view_args'] : '',
77 '#description' => t('Provide a comma separated list of arguments to pass to the view.'),
84 $settings = array('referenceable_types');
85 if (module_exists('views')) {
86 $settings[] = 'advanced_view';
87 $settings[] = 'advanced_view_args';
91 case
'database columns':
93 'nid' => array('type' => 'int', 'not null' => TRUE
, 'default' => '0'),
100 'list' => '_nodereference_filter_handler',
101 'list-type' => 'list',
102 'operator' => 'views_handler_operator_or',
103 'value-type' => 'array',
104 'extra' => array('field' => $field),
111 * Implementation of hook_field().
113 function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
116 $refs = _nodereference_potential_references($field, TRUE
);
117 foreach ($items as
$delta => $item) {
118 $error_field = isset($item['error_field']) ?
$item['error_field'] : '';
119 unset($item['error_field']);
120 if (!empty($item['nid'])) {
121 if (!in_array($item['nid'], array_keys($refs))) {
122 form_set_error($error_field, t('%name : This post can\'t be referenced.', array('%name' => t($field['widget']['label']))));
131 * Implementation of hook_field_formatter_info().
133 function nodereference_field_formatter_info() {
136 'label' => t('Title (link)'),
137 'field types' => array('nodereference'),
140 'label' => t('Title (no link)'),
141 'field types' => array('nodereference'),
144 'label' => t('Full node'),
145 'field types' => array('nodereference'),
148 'label' => t('Teaser'),
149 'field types' => array('nodereference'),
155 * Implementation of hook_field_formatter().
157 function nodereference_field_formatter($field, $item, $formatter, $node) {
158 static
$titles = array();
160 // We store the rendered nids in order to prevent infinite recursion
161 // when using the 'full node' / 'teaser' formatters.
162 static
$recursion_queue = array();
164 if (empty($item['nid']) || !is_numeric($item['nid'])) {
168 if ($formatter == 'full' || $formatter == 'teaser') {
169 // If no 'referencing node' is set, we are starting a new 'reference thread'
170 if (!isset($node->referencing_node
)) {
171 $recursion_queue = array();
173 $recursion_queue[] = $node->nid
;
174 if (in_array($item['nid'], $recursion_queue)) {
175 // Prevent infinite recursion caused by reference cycles :
176 // if the node has already been rendered earlier in this 'thread',
177 // we fall back to 'default' (node title) formatter.
178 $formatter = 'default';
180 elseif ($referenced_node = node_load($item['nid'])) {
181 $referenced_node->referencing_node
= $node;
182 $referenced_node->referencing_field
= $field;
183 $titles[$item['nid']] = $referenced_node->title
;
187 if (!isset($titles[$item['nid']])) {
188 $title = db_result(db_query("SELECT title FROM {node} WHERE nid=%d", $item['nid']));
189 $titles[$item['nid']] = $title ?
$title : '';
192 switch ($formatter) {
194 return $referenced_node ?
node_view($referenced_node, FALSE
) : '';
197 return $referenced_node ?
node_view($referenced_node, TRUE
) : '';
200 return check_plain($titles[$item['nid']]);
203 return $titles[$item['nid']] ?
l($titles[$item['nid']], 'node/'.
$item['nid']) : '';
208 * Implementation of hook_widget_info().
210 function nodereference_widget_info() {
212 'nodereference_select' => array(
213 'label' => t('Select List'),
214 'field types' => array('nodereference'),
216 'nodereference_autocomplete' => array(
217 'label' => t('Autocomplete Text Field'),
218 'field types' => array('nodereference'),
224 * Implementation of hook_widget().
226 function nodereference_widget($op, &$node, $field, &$items) {
227 if ($field['widget']['type'] == 'nodereference_select') {
229 case
'prepare form values':
230 $items_transposed = content_transpose_array_rows_cols($items);
231 $items['default nids'] = $items_transposed['nid'];
237 $options = _nodereference_potential_references($field, TRUE
);
238 foreach ($options as
$key => $value) {
239 $options[$key] = _nodereference_item($field, $value, FALSE
);
241 if (!$field['required']) {
242 $options = array(0 => t('<none>')) + $options;
245 $form[$field['field_name']] = array('#tree' => TRUE
);
246 $form[$field['field_name']]['nids'] = array(
248 '#title' => t($field['widget']['label']),
249 '#default_value' => $items['default nids'],
250 '#multiple' => $field['multiple'],
251 '#size' => $field['multiple'] ?
min(count($options), 6) : 0,
252 '#options' => $options,
253 '#required' => $field['required'],
254 '#description' => content_filter_xss(t($field['widget']['description'])),
259 case
'process form values':
260 if ($field['multiple']) {
261 // if nothing selected, make it 'none'
262 if (empty($items['nids'])) {
263 $items['nids'] = array(0 => '0');
265 // drop the 'none' options if other items were also selected
266 elseif (count($items['nids']) > 1) {
267 unset($items['nids'][0]);
270 $items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids'])));
273 $items[0]['nid'] = $items['nids'];
275 // Remove the widget's data representation so it isn't saved.
276 unset($items['nids']);
277 foreach ($items as
$delta => $item) {
278 $items[$delta]['error_field'] = $field['field_name'] .
'][nids';
284 case
'prepare form values':
285 foreach ($items as
$delta => $item) {
286 if (!empty($items[$delta]['nid'])) {
287 $items[$delta]['default node_name'] = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $items[$delta]['nid']));
288 $items[$delta]['default node_name'] .
= ' [nid:'.
$items[$delta]['nid'] .
']';
295 $form[$field['field_name']] = array('#tree' => TRUE
);
297 if ($field['multiple']) {
298 $form[$field['field_name']]['#type'] = 'fieldset';
299 $form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description']));
301 foreach ($items as
$item) {
303 $form[$field['field_name']][$delta]['node_name'] = array(
304 '#type' => 'textfield',
305 '#title' => ($delta == 0) ?
t($field['widget']['label']) : '',
306 '#autocomplete_path' => 'nodereference/autocomplete/'.
$field['field_name'],
307 '#default_value' => $item['default node_name'],
308 '#required' => ($delta == 0) ?
$field['required'] : FALSE
,
313 foreach (range($delta, $delta + 2) as
$delta) {
314 $form[$field['field_name']][$delta]['node_name'] = array(
315 '#type' => 'textfield',
316 '#title' => ($delta == 0) ?
t($field['widget']['label']) : '',
317 '#autocomplete_path' => 'nodereference/autocomplete/'.
$field['field_name'],
318 '#default_value' => '',
319 '#required' => ($delta == 0) ?
$field['required'] : FALSE
,
324 $form[$field['field_name']][0]['node_name'] = array(
325 '#type' => 'textfield',
326 '#title' => t($field['widget']['label']),
327 '#autocomplete_path' => 'nodereference/autocomplete/'.
$field['field_name'],
328 '#default_value' => $items[0]['default node_name'],
329 '#required' => $field['required'],
330 '#description' => content_filter_xss( t( $field['widget']['description'] )),
336 foreach ($items as
$delta => $item) {
337 $error_field = $field['field_name'] .
']['.
$delta .
'][node_name';
338 if (!empty($item['node_name'])) {
339 preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
340 if (!empty($matches)) {
342 list(, $title, $nid) = $matches;
343 if (!empty($title) && ($n = node_load($nid)) && $title != $n->title
) {
344 form_set_error($error_field, t('%name : Title mismatch. Please check your selection.', array('%name' => t($field['widget']['label']))));
349 $nids = _nodereference_potential_references($field, FALSE
, $item['node_name'], TRUE
);
351 form_set_error($error_field, t('%name: Found no valid post with that title.', array('%name' => t($field['widget']['label']))));
355 // the best thing would be to present the user with an additional form,
356 // allowing the user to choose between valid candidates with the same title
357 // ATM, we pick the first matching candidate...
358 $nid = array_shift(array_keys($nids));
365 case
'process form values':
366 foreach ($items as
$delta => $item) {
368 if (!empty($item['node_name'])) {
369 preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
370 if (!empty($matches)) {
377 // the best thing would be to present the user with an additional form,
378 // allowing the user to choose between valid candidates with the same title
379 // ATM, we pick the first matching candidate...
380 $nids = _nodereference_potential_references($field, FALSE
, $item['node_name'], TRUE
);
381 $nid = (!empty($nids)) ?
array_shift(array_keys($nids)) : 0;
384 // Remove the widget's data representation so it isn't saved.
385 unset($items[$delta]['node_name']);
387 $items[$delta]['nid'] = $nid;
388 $items[$delta]['error_field'] = $field['field_name'] .
']['.
$delta .
'][node_name';
390 elseif ($delta > 0) {
391 // Don't save empty fields when they're not the first value (keep '0' otherwise)
392 unset($items[$delta]);
401 * Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user.
403 function _nodereference_potential_references($field, $return_full_nodes = FALSE
, $string = '', $exact_string = false
) {
404 if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
405 // advanced field : referenceable nodes defined by a view
406 // let views.module build the query
408 // arguments for the view
409 $view_args = array();
410 if (isset($field['advanced_view_args'])) {
411 // TODO: Support Tokens using token.module ?
412 $view_args = array_map(trim, explode(',', $field['advanced_view_args']));
415 if (isset($string)) {
416 views_view_add_filter($view, 'node', 'title', $exact_string ?
'=' : 'contains', $string, null
);
419 // we do need title field, so add it if not present (unlikely, but...)
420 $has_title = array_reduce($view->field
, create_function('$a, $b', 'return ($b["field"] == "title") || $a;'), false
);
422 views_view_add_field($view, 'node', 'title', '');
425 views_sanitize_view($view);
427 // make sure the fields get included in the query
429 $view->page_type
= 'list';
431 // make sure the query is not cached
432 unset($view->query
); // Views 1.5-
433 $view->is_cacheable
= FALSE
; // Views 1.6+
435 $view_result = views_build_view('result', $view, $view_args);
436 $result = $view_result['result'];
439 // standard field : referenceable nodes defined by content types
440 // build the appropriate query
441 $related_types = array();
444 if (isset($field['referenceable_types'])) {
445 foreach ($field['referenceable_types'] as
$related_type) {
447 $related_types[] = " n.type = '%s'";
448 $args[] = $related_type;
453 $related_clause = implode(' OR ', $related_types);
455 if (!count($related_types)) {
459 if ($string !== '') {
460 $string_clause = $exact_string ?
" AND n.title = '%s'" : " AND n.title LIKE '%%%s%'";
461 $related_clause = "(".
$related_clause .
")".
$string_clause;
465 $result = db_query(db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n WHERE ".
$related_clause .
" ORDER BY n.title, n.type"), $args);
468 if (db_num_rows($result) == 0) {
474 while ($node = db_fetch_object($result)) {
475 if ($return_full_nodes) {
476 $rows[$node->nid
] = $node;
479 $rows[$node->nid
] = $node->node_title
;
487 * Retrieve a pipe delimited string of autocomplete suggestions
489 function nodereference_autocomplete($field_name, $string = '') {
490 $fields = content_fields();
491 $field = $fields[$field_name];
494 foreach (_nodereference_potential_references($field, TRUE
, $string) as
$row) {
495 $matches[$row->node_title .
' [nid:'.
$row->nid .
']'] = _nodereference_item($field, $row);
497 print drupal_to_js($matches);
501 function _nodereference_item($field, $item, $html = TRUE
) {
502 if (module_exists('views') && isset($field['advanced_view']) && $field['advanced_view'] != '--' && ($view = views_get_view($field['advanced_view']))) {
503 $output = theme('nodereference_item_advanced', $item, $view);
505 // Views theming runs check_plain (htmlentities) on the values.
506 // We reverse that with html_entity_decode.
507 $output = html_entity_decode(strip_tags($output), ENT_QUOTES
);
511 $output = theme('nodereference_item_simple', $item);
512 $output = $html ?
check_plain($output) : $output;
517 function theme_nodereference_item_advanced($item, $view) {
518 $fields = _views_get_fields();
519 $item_fields = array();
520 foreach ($view->field as
$field) {
521 $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $item, $view);
522 // remove link tags (ex : for node titles)
523 $value = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $value);
524 if (!empty($value)) {
525 $item_fields[] = "<span class='view-field view-data-$field[queryname]'>$value</span>";;
528 $output = implode(' - ', $item_fields);
529 $output = "<span class='view-item view-item-$view->name'>$output</span>";
533 function theme_nodereference_item_simple($item) {
534 return $item->node_title
;
538 * Provide a list of nodes to filter on.
540 function _nodereference_filter_handler($op, $filterinfo) {
541 $options = array(0 => t('<empty>'));
542 $options = $options + _nodereference_potential_references($filterinfo['extra']['field']);
547 * Implementation of hook_panels_relationships().
549 function nodereference_panels_relationships() {
551 $args['node_from_noderef'] = array(
552 'title' => t('Node from reference'),
553 'keyword' => 'nodereference',
554 'description' => t('Adds a node from a node reference in a node context; if multiple nodes are referenced, this will get the first referenced node only.'),
555 'required context' => new
panels_required_context(t('Node'), 'node'),
556 'context' => 'nodereference_node_from_noderef_context',
557 'settings form' => 'nodereference_node_from_noderef_settings_form',
558 'settings form validate' => 'nodereference_node_from_noderef_settings_form_validate',
564 * Return a new panels context based on an existing context
566 function nodereference_node_from_noderef_context($context = NULL
, $conf) {
567 // If unset it wants a generic, unfilled context, which is just NULL
568 if (empty($context->data
)) {
569 return panels_context_create_empty('node', NULL
);
571 if (isset($context->data
->{$conf['field_name']}[0]['nid']) && ($nid = $context->data
->{$conf['field_name']}[0]['nid'])) {
572 if ($node = node_load($nid)) {
573 return panels_context_create('node', $node);
579 * Settings form for the panels relationship.
581 function nodereference_node_from_noderef_settings_form($conf) {
583 foreach (content_fields() as
$field) {
584 if ($field['type'] == 'nodereference') {
585 $options[$field['field_name']] = t($field['widget']['label']);
588 $form['field_name'] = array(
589 '#title' => t('Node reference field'),
591 '#options' => $options,
592 '#default_value' => $conf['field_name'],
593 '#prefix' => '<div class="clear-block">',
594 '#suffix' => '</div>',