| 1 |
<?php |
<?php |
| 2 |
// $Id: nodereference.module,v 1.38 2006/11/15 13:44:28 yched Exp $ |
// $Id: nodereference.module,v 1.39.2.38 2008/08/04 22:28:16 yched Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 6 |
* Defines a field type for referencing one node from another. |
* Defines a field type for referencing one node from another. |
| 7 |
*/ |
*/ |
| 8 |
|
|
|
/** |
|
|
* Implementation of hook_help(). |
|
|
*/ |
|
|
function nodereference_help($section) { |
|
|
switch ($section) { |
|
|
case 'admin/settings/modules#description': |
|
|
return t('<strong>CCK:</strong> Defines a field type for referencing one node from another. <em>Note: Requires content.module.</em>'); |
|
|
} |
|
|
} |
|
| 9 |
|
|
| 10 |
/** |
/** |
| 11 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 26 |
*/ |
*/ |
| 27 |
function nodereference_field_info() { |
function nodereference_field_info() { |
| 28 |
return array( |
return array( |
| 29 |
'nodereference' => array('label' => 'Node Reference'), |
'nodereference' => array('label' => t('Node Reference')), |
| 30 |
); |
); |
| 31 |
} |
} |
| 32 |
|
|
| 56 |
$views[t('Default Views')][$view->name] = $view->name; |
$views[t('Default Views')][$view->name] = $view->name; |
| 57 |
} |
} |
| 58 |
if (count($views) > 1) { |
if (count($views) > 1) { |
| 59 |
$form['advanced_view'] = array( |
$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'] == '--', |
| 64 |
|
); |
| 65 |
|
$form['advanced']['advanced_view'] = array( |
| 66 |
'#type' => 'select', |
'#type' => 'select', |
| 67 |
'#title' => t('Advanced - Nodes that can be referenced'), |
'#title' => t('View'), |
| 68 |
'#options' => $views, |
'#options' => $views, |
| 69 |
'#default_value' => isset($field['advanced_view']) ? $field['advanced_view'] : '--', |
'#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>'. |
'#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>'), |
| 71 |
'Note :'. |
); |
| 72 |
'<ul><li>This will discard the "Content types" settings above. Use the view\'s "filters" section instead.</li>'. |
$form['advanced']['advanced_view_args'] = array( |
| 73 |
'<li>Use the view\'s "fields" section to display additional informations about candidate nodes on node creation/edition form.</li>'. |
'#type' => 'textfield', |
| 74 |
'<li>Use the view\'s "sort criteria" section to determine the order in which candidate nodes will be displayed.</li></ul>'), |
'#title' => t('View arguments'), |
| 75 |
|
'#default_value' => isset($field['advanced_view_args']) ? $field['advanced_view_args'] : '', |
| 76 |
|
'#required' => FALSE, |
| 77 |
|
'#description' => t('Provide a comma separated list of arguments to pass to the view.'), |
| 78 |
); |
); |
| 79 |
} |
} |
| 80 |
} |
} |
| 84 |
$settings = array('referenceable_types'); |
$settings = array('referenceable_types'); |
| 85 |
if (module_exists('views')) { |
if (module_exists('views')) { |
| 86 |
$settings[] = 'advanced_view'; |
$settings[] = 'advanced_view'; |
| 87 |
|
$settings[] = 'advanced_view_args'; |
| 88 |
} |
} |
| 89 |
return $settings; |
return $settings; |
| 90 |
|
|
| 97 |
case 'filters': |
case 'filters': |
| 98 |
return array( |
return array( |
| 99 |
'default' => array( |
'default' => array( |
| 100 |
'list' => '_nodereference_filter_handler', |
'list' => '_nodereference_filter_handler', |
| 101 |
'list-type' => 'list', |
'list-type' => 'list', |
| 102 |
'operator' => 'views_handler_operator_or', |
'operator' => 'views_handler_operator_or', |
| 103 |
'value-type' => 'array', |
'value-type' => 'array', |
| 104 |
'extra' => array('field' => $field), |
'extra' => array('field' => $field), |
| 105 |
), |
), |
| 106 |
); |
); |
| 107 |
} |
} |
| 108 |
} |
} |
| 109 |
|
|
| 112 |
*/ |
*/ |
| 113 |
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) { |
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) { |
| 114 |
switch ($op) { |
switch ($op) { |
| 115 |
case 'view': |
case 'validate': |
| 116 |
|
$refs = _nodereference_potential_references($field, TRUE); |
| 117 |
foreach ($items as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 118 |
$items[$delta]['view'] = content_format($field, $item, 'default', $node); |
$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'])))); |
| 123 |
|
} |
| 124 |
|
} |
| 125 |
} |
} |
| 126 |
return theme('field', $node, $field, $items, $teaser, $page); |
return; |
| 127 |
} |
} |
| 128 |
} |
} |
| 129 |
|
|
| 133 |
function nodereference_field_formatter_info() { |
function nodereference_field_formatter_info() { |
| 134 |
return array( |
return array( |
| 135 |
'default' => array( |
'default' => array( |
| 136 |
'label' => 'Default', |
'label' => t('Title (link)'), |
| 137 |
'field types' => array('nodereference'), |
'field types' => array('nodereference'), |
| 138 |
), |
), |
| 139 |
'plain' => array( |
'plain' => array( |
| 140 |
'label' => 'Plain text', |
'label' => t('Title (no link)'), |
| 141 |
|
'field types' => array('nodereference'), |
| 142 |
|
), |
| 143 |
|
'full' => array( |
| 144 |
|
'label' => t('Full node'), |
| 145 |
|
'field types' => array('nodereference'), |
| 146 |
|
), |
| 147 |
|
'teaser' => array( |
| 148 |
|
'label' => t('Teaser'), |
| 149 |
'field types' => array('nodereference'), |
'field types' => array('nodereference'), |
| 150 |
), |
), |
| 151 |
); |
); |
| 155 |
* Implementation of hook_field_formatter(). |
* Implementation of hook_field_formatter(). |
| 156 |
*/ |
*/ |
| 157 |
function nodereference_field_formatter($field, $item, $formatter, $node) { |
function nodereference_field_formatter($field, $item, $formatter, $node) { |
| 158 |
$text = ''; |
static $titles = array(); |
| 159 |
if (!empty($item['nid'])) { |
|
| 160 |
$referenced_node = node_load($item['nid']); |
// We store the rendered nids in order to prevent infinite recursion |
| 161 |
if ($referenced_node) { |
// when using the 'full node' / 'teaser' formatters. |
| 162 |
$text = l($referenced_node->title, 'node/'. $referenced_node->nid); |
static $recursion_queue = array(); |
| 163 |
|
|
| 164 |
|
if (empty($item['nid']) || !is_numeric($item['nid'])) { |
| 165 |
|
return ''; |
| 166 |
|
} |
| 167 |
|
|
| 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(); |
| 172 |
|
} |
| 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'; |
| 179 |
|
} |
| 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; |
| 184 |
} |
} |
| 185 |
} |
} |
| 186 |
|
|
| 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 : ''; |
| 190 |
|
} |
| 191 |
|
|
| 192 |
switch ($formatter) { |
switch ($formatter) { |
| 193 |
|
case 'full': |
| 194 |
|
return $referenced_node ? node_view($referenced_node, FALSE) : ''; |
| 195 |
|
|
| 196 |
|
case 'teaser': |
| 197 |
|
return $referenced_node ? node_view($referenced_node, TRUE) : ''; |
| 198 |
|
|
| 199 |
case 'plain': |
case 'plain': |
| 200 |
return strip_tags($text); |
return check_plain($titles[$item['nid']]); |
| 201 |
|
|
| 202 |
default: |
default: |
| 203 |
return $text; |
return $titles[$item['nid']] ? l($titles[$item['nid']], 'node/'. $item['nid']) : ''; |
| 204 |
} |
} |
| 205 |
} |
} |
| 206 |
|
|
| 210 |
function nodereference_widget_info() { |
function nodereference_widget_info() { |
| 211 |
return array( |
return array( |
| 212 |
'nodereference_select' => array( |
'nodereference_select' => array( |
| 213 |
'label' => 'Select List', |
'label' => t('Select List'), |
| 214 |
'field types' => array('nodereference'), |
'field types' => array('nodereference'), |
| 215 |
), |
), |
| 216 |
'nodereference_autocomplete' => array( |
'nodereference_autocomplete' => array( |
| 217 |
'label' => 'Autocomplete Text Field', |
'label' => t('Autocomplete Text Field'), |
| 218 |
'field types' => array('nodereference'), |
'field types' => array('nodereference'), |
| 219 |
), |
), |
| 220 |
); |
); |
| 223 |
/** |
/** |
| 224 |
* Implementation of hook_widget(). |
* Implementation of hook_widget(). |
| 225 |
*/ |
*/ |
| 226 |
function nodereference_widget($op, &$node, $field, &$node_field) { |
function nodereference_widget($op, &$node, $field, &$items) { |
| 227 |
if ($field['widget']['type'] == 'nodereference_select') { |
if ($field['widget']['type'] == 'nodereference_select') { |
| 228 |
switch ($op) { |
switch ($op) { |
| 229 |
case 'prepare form values': |
case 'prepare form values': |
| 230 |
$node_field_transposed = content_transpose_array_rows_cols($node_field); |
$items_transposed = content_transpose_array_rows_cols($items); |
| 231 |
$node_field['default nids'] = $node_field_transposed['nid']; |
$items['default nids'] = $items_transposed['nid']; |
| 232 |
break; |
break; |
| 233 |
|
|
| 234 |
case 'form': |
case 'form': |
| 235 |
$form = array(); |
$form = array(); |
| 236 |
|
|
| 237 |
$options = _nodereference_potential_references($field, true); |
$options = _nodereference_potential_references($field, TRUE); |
| 238 |
foreach ($options as $key => $value) { |
foreach ($options as $key => $value) { |
| 239 |
$options[$key] = _nodereference_item($field, $value); |
$options[$key] = _nodereference_item($field, $value, FALSE); |
| 240 |
} |
} |
| 241 |
if (!$field['required']) { |
if (!$field['required']) { |
| 242 |
$options = array(0 => t('<none>')) + $options; |
$options = array(0 => t('<none>')) + $options; |
| 246 |
$form[$field['field_name']]['nids'] = array( |
$form[$field['field_name']]['nids'] = array( |
| 247 |
'#type' => 'select', |
'#type' => 'select', |
| 248 |
'#title' => t($field['widget']['label']), |
'#title' => t($field['widget']['label']), |
| 249 |
'#default_value' => $node_field['default nids'], |
'#default_value' => $items['default nids'], |
| 250 |
'#multiple' => $field['multiple'], |
'#multiple' => $field['multiple'], |
| 251 |
|
'#size' => $field['multiple'] ? min(count($options), 6) : 0, |
| 252 |
'#options' => $options, |
'#options' => $options, |
| 253 |
'#required' => $field['required'], |
'#required' => $field['required'], |
| 254 |
'#description' => $field['widget']['description'], |
'#description' => content_filter_xss(t($field['widget']['description'])), |
| 255 |
); |
); |
| 256 |
|
|
| 257 |
return $form; |
return $form; |
| 258 |
|
|
| 259 |
case 'process form values': |
case 'process form values': |
| 260 |
if ($field['multiple']) { |
if ($field['multiple']) { |
| 261 |
if (empty($node_field['nids'])) { |
// if nothing selected, make it 'none' |
| 262 |
// if nothing selected, make it 'none' |
if (empty($items['nids'])) { |
| 263 |
$node_field['nids'] = array('0'); |
$items['nids'] = array(0 => '0'); |
| 264 |
} |
} |
| 265 |
|
// drop the 'none' options if other items were also selected |
| 266 |
if (count($node_field['nids']) > 1) { |
elseif (count($items['nids']) > 1) { |
| 267 |
// drop the 'none' options if other items were also selected |
unset($items['nids'][0]); |
|
unset($node_field['nids'][0]); |
|
| 268 |
} |
} |
| 269 |
$node_field = content_transpose_array_rows_cols(array('nid' => $node_field['nids'])); |
|
| 270 |
|
$items = array_values(content_transpose_array_rows_cols(array('nid' => $items['nids']))); |
| 271 |
} |
} |
| 272 |
else { |
else { |
| 273 |
$node_field[0]['nid'] = $node_field['nids']; |
$items[0]['nid'] = $items['nids']; |
| 274 |
} |
} |
| 275 |
// Remove the widget's data representation so it isn't saved. |
// Remove the widget's data representation so it isn't saved. |
| 276 |
unset($node_field['nids']); |
unset($items['nids']); |
| 277 |
|
foreach ($items as $delta => $item) { |
| 278 |
|
$items[$delta]['error_field'] = $field['field_name'] .'][nids'; |
| 279 |
|
} |
| 280 |
} |
} |
| 281 |
} |
} |
| 282 |
else { |
else { |
| 283 |
switch ($op) { |
switch ($op) { |
| 284 |
case 'prepare form values': |
case 'prepare form values': |
| 285 |
foreach ($node_field as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 286 |
if (!empty($node_field[$delta]['nid'])) { |
if (!empty($items[$delta]['nid'])) { |
| 287 |
$node_field[$delta]['default node_name'] = db_result(db_query(db_rewrite_sql('SELECT n.title FROM {node} n WHERE n.nid = %d'), $node_field[$delta]['nid'])); |
$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 |
$node_field[$delta]['default node_name'] .= ' [nid:'. $node_field[$delta]['nid'] .']'; |
$items[$delta]['default node_name'] .= ' [nid:'. $items[$delta]['nid'] .']'; |
| 289 |
} |
} |
| 290 |
} |
} |
| 291 |
break; |
break; |
| 296 |
|
|
| 297 |
if ($field['multiple']) { |
if ($field['multiple']) { |
| 298 |
$form[$field['field_name']]['#type'] = 'fieldset'; |
$form[$field['field_name']]['#type'] = 'fieldset'; |
| 299 |
$form[$field['field_name']]['#title'] = t($field['widget']['label']); |
$form[$field['field_name']]['#description'] = content_filter_xss(t($field['widget']['description'])); |
|
$form[$field['field_name']]['#description'] = $field['widget']['description']; |
|
| 300 |
$delta = 0; |
$delta = 0; |
| 301 |
foreach ($node_field as $item) { |
foreach ($items as $item) { |
| 302 |
if ($item['nid']) { |
if ($item['nid']) { |
| 303 |
$form[$field['field_name']][$delta]['node_name'] = array( |
$form[$field['field_name']][$delta]['node_name'] = array( |
| 304 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 305 |
'#title' => '', |
'#title' => ($delta == 0) ? t($field['widget']['label']) : '', |
| 306 |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
| 307 |
'#default_value' => $item['default node_name'], |
'#default_value' => $item['default node_name'], |
| 308 |
'#required' => ($delta == 0) ? $field['required'] : FALSE, |
'#required' => ($delta == 0) ? $field['required'] : FALSE, |
| 313 |
foreach (range($delta, $delta + 2) as $delta) { |
foreach (range($delta, $delta + 2) as $delta) { |
| 314 |
$form[$field['field_name']][$delta]['node_name'] = array( |
$form[$field['field_name']][$delta]['node_name'] = array( |
| 315 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 316 |
'#title' => '', |
'#title' => ($delta == 0) ? t($field['widget']['label']) : '', |
| 317 |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
| 318 |
'#default_value' => '', |
'#default_value' => '', |
| 319 |
'#required' => ($delta == 0) ? $field['required'] : FALSE, |
'#required' => ($delta == 0) ? $field['required'] : FALSE, |
| 325 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 326 |
'#title' => t($field['widget']['label']), |
'#title' => t($field['widget']['label']), |
| 327 |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
'#autocomplete_path' => 'nodereference/autocomplete/'. $field['field_name'], |
| 328 |
'#default_value' => $node_field[0]['default node_name'], |
'#default_value' => $items[0]['default node_name'], |
| 329 |
'#required' => $field['required'], |
'#required' => $field['required'], |
| 330 |
'#description' => $field['widget']['description'], |
'#description' => content_filter_xsst($field['widget']['description'])), |
| 331 |
); |
); |
| 332 |
} |
} |
| 333 |
return $form; |
return $form; |
| 334 |
|
|
| 335 |
case 'validate': |
case 'validate': |
| 336 |
foreach ($node_field as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 337 |
$error_field = $field['field_name'].']['.$delta.'][node_name'; |
$error_field = $field['field_name'] .']['. $delta .'][node_name'; |
| 338 |
if (!empty($item['node_name'])) { |
if (!empty($item['node_name'])) { |
| 339 |
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches); |
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches); |
| 340 |
if (!empty($matches)) { |
if (!empty($matches)) { |
| 341 |
// explicit nid |
// explicit nid |
| 342 |
list(, $title, $nid) = $matches; |
list(, $title, $nid) = $matches; |
| 343 |
$refs = _nodereference_potential_references($field, TRUE); |
if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) { |
| 344 |
if (!in_array($nid, array_keys($refs))) { |
form_set_error($error_field, t('%name : Title mismatch. Please check your selection.', array('%name' => t($field['widget']['label'])))); |
|
form_set_error($error_field, t('This post can\'t be referenced.')); |
|
|
} |
|
|
elseif (!empty($title) && ($n = $refs[$nid]) && $title != $n->node_title) { |
|
|
form_set_error($error_field, t('Title mismatch. Please reiterate your selection.')); |
|
| 345 |
} |
} |
| 346 |
} |
} |
| 347 |
else { |
else { |
| 348 |
// no explicit nid |
// no explicit nid |
| 349 |
$refs = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE); |
$nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE); |
| 350 |
if (empty($refs)) { |
if (empty($nids)) { |
| 351 |
form_set_error($error_field, t('No post with that title can be referenced.')); |
form_set_error($error_field, t('%name: Found no valid post with that title.', array('%name' => t($field['widget']['label'])))); |
| 352 |
|
} |
| 353 |
|
else { |
| 354 |
|
// TODO: |
| 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)); |
| 359 |
} |
} |
| 360 |
} |
} |
| 361 |
} |
} |
| 363 |
return; |
return; |
| 364 |
|
|
| 365 |
case 'process form values': |
case 'process form values': |
| 366 |
foreach ($node_field as $delta => $item) { |
foreach ($items as $delta => $item) { |
| 367 |
$nid = 0; |
$nid = 0; |
| 368 |
if (!empty($item['node_name'])) { |
if (!empty($item['node_name'])) { |
| 369 |
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches); |
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches); |
| 377 |
// the best thing would be to present the user with an additional form, |
// 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 |
// allowing the user to choose between valid candidates with the same title |
| 379 |
// ATM, we pick the first matching candidate... |
// ATM, we pick the first matching candidate... |
| 380 |
$nids = _nodereference_potential_references($field, false, $item['node_name'], true); |
$nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE); |
| 381 |
$nid = array_shift(array_keys($nids)); |
$nid = (!empty($nids)) ? array_shift(array_keys($nids)) : 0; |
| 382 |
} |
} |
| 383 |
} |
} |
| 384 |
|
// Remove the widget's data representation so it isn't saved. |
| 385 |
|
unset($items[$delta]['node_name']); |
| 386 |
if (!empty($nid)) { |
if (!empty($nid)) { |
| 387 |
$node_field[$delta]['nid'] = $nid; |
$items[$delta]['nid'] = $nid; |
| 388 |
// Remove the widget's data representation so it isn't saved. |
$items[$delta]['error_field'] = $field['field_name'] .']['. $delta .'][node_name'; |
|
unset($node_field[$delta]['node_name']); |
|
| 389 |
} |
} |
| 390 |
else { |
elseif ($delta > 0) { |
| 391 |
// Don't save empty fields when they're not the first value (keep '0' otherwise) |
// Don't save empty fields when they're not the first value (keep '0' otherwise) |
| 392 |
if ($delta > 0) {unset($node_field[$delta]);} |
unset($items[$delta]); |
| 393 |
} |
} |
| 394 |
} |
} |
| 395 |
break; |
break; |
| 401 |
* Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user. |
* Fetch an array of all candidate referenced nodes, for use in presenting the selection form to the user. |
| 402 |
*/ |
*/ |
| 403 |
function _nodereference_potential_references($field, $return_full_nodes = FALSE, $string = '', $exact_string = false) { |
function _nodereference_potential_references($field, $return_full_nodes = FALSE, $string = '', $exact_string = false) { |
| 404 |
if (module_exists('views') && ($view = views_get_view($field['advanced_view']))) { |
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 |
// advanced field : referenceable nodes defined by a view |
| 406 |
// let views.module build the query |
// let views.module build the query |
| 407 |
|
|
| 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'])); |
| 413 |
|
} |
| 414 |
|
|
| 415 |
if (isset($string)) { |
if (isset($string)) { |
| 416 |
views_view_add_filter($view, 'node', 'title', $exact_string ? '=' : 'contains', $string, null); |
views_view_add_filter($view, 'node', 'title', $exact_string ? '=' : 'contains', $string, null); |
| 417 |
} |
} |
| 429 |
$view->page_type = 'list'; |
$view->page_type = 'list'; |
| 430 |
|
|
| 431 |
// make sure the query is not cached |
// make sure the query is not cached |
| 432 |
unset($view->query); |
unset($view->query); // Views 1.5- |
| 433 |
|
$view->is_cacheable = FALSE; // Views 1.6+ |
| 434 |
|
|
| 435 |
$view_result = views_build_view('result', $view); |
$view_result = views_build_view('result', $view, $view_args); |
| 436 |
$result = $view_result['result']; |
$result = $view_result['result']; |
| 437 |
} |
} |
| 438 |
else { |
else { |
| 444 |
if (isset($field['referenceable_types'])) { |
if (isset($field['referenceable_types'])) { |
| 445 |
foreach ($field['referenceable_types'] as $related_type) { |
foreach ($field['referenceable_types'] as $related_type) { |
| 446 |
if ($related_type) { |
if ($related_type) { |
| 447 |
$related_types[] = " type = '%s'"; |
$related_types[] = " n.type = '%s'"; |
| 448 |
$args[] = $related_type; |
$args[] = $related_type; |
| 449 |
} |
} |
| 450 |
} |
} |
| 457 |
} |
} |
| 458 |
|
|
| 459 |
if (isset($string)) { |
if (isset($string)) { |
| 460 |
$string_clause = $exact_string ? " AND title = '%s'" : " AND title LIKE '%%%s%'"; |
$string_clause = $exact_string ? " AND n.title = '%s'" : " AND n.title LIKE '%%%s%'"; |
| 461 |
$related_clause = "(". $related_clause .")". $string_clause; |
$related_clause = "(". $related_clause .")". $string_clause; |
| 462 |
$args[] = $string; |
$args[] = $string; |
| 463 |
} |
} |
| 492 |
$matches = array(); |
$matches = array(); |
| 493 |
|
|
| 494 |
foreach (_nodereference_potential_references($field, TRUE, $string) as $row) { |
foreach (_nodereference_potential_references($field, TRUE, $string) as $row) { |
| 495 |
$matches[$row->node_title .' [nid:'. $row->nid .']'] = _nodereference_item($field, $row, TRUE); |
$matches[$row->node_title .' [nid:'. $row->nid .']'] = _nodereference_item($field, $row); |
| 496 |
} |
} |
| 497 |
print drupal_to_js($matches); |
print drupal_to_js($matches); |
| 498 |
exit(); |
exit(); |
| 499 |
} |
} |
| 500 |
|
|
| 501 |
function _nodereference_item($field, $item, $html = false) { |
function _nodereference_item($field, $item, $html = TRUE) { |
| 502 |
if (module_exists('views') && ($view = views_get_view($field['advanced_view']))) { |
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); |
$output = theme('nodereference_item_advanced', $item, $view); |
| 504 |
if (!$html) { |
if (!$html) { |
| 505 |
$output = strip_tags($output); |
// 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); |
| 508 |
} |
} |
| 509 |
} |
} |
| 510 |
else { |
else { |
| 511 |
$output = theme('nodereference_item_simple', $item); |
$output = theme('nodereference_item_simple', $item); |
| 512 |
|
$output = $html ? check_plain($output) : $output; |
| 513 |
} |
} |
| 514 |
return $output; |
return $output; |
| 515 |
} |
} |
| 538 |
* Provide a list of users to filter on. |
* Provide a list of users to filter on. |
| 539 |
*/ |
*/ |
| 540 |
function _nodereference_filter_handler($op, $filterinfo) { |
function _nodereference_filter_handler($op, $filterinfo) { |
| 541 |
$options = array('' => ''); |
$options = array(0 => t('<empty>')); |
| 542 |
$options = $options + _nodereference_potential_references($filterinfo['extra']['field']); |
$options = $options + _nodereference_potential_references($filterinfo['extra']['field']); |
| 543 |
return $options; |
return $options; |
| 544 |
|
} |
| 545 |
|
|
| 546 |
|
/** |
| 547 |
|
* Implementation of hook_panels_relationships(). |
| 548 |
|
*/ |
| 549 |
|
function nodereference_panels_relationships() { |
| 550 |
|
$args = array(); |
| 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', |
| 559 |
|
); |
| 560 |
|
return $args; |
| 561 |
|
} |
| 562 |
|
|
| 563 |
|
/** |
| 564 |
|
* Return a new panels context based on an existing context |
| 565 |
|
*/ |
| 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); |
| 570 |
|
} |
| 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); |
| 574 |
|
} |
| 575 |
|
} |
| 576 |
|
} |
| 577 |
|
|
| 578 |
|
/** |
| 579 |
|
* Settings form for the panels relationship. |
| 580 |
|
*/ |
| 581 |
|
function nodereference_node_from_noderef_settings_form($conf) { |
| 582 |
|
$options = array(); |
| 583 |
|
foreach (content_fields() as $field) { |
| 584 |
|
if ($field['type'] == 'nodereference') { |
| 585 |
|
$options[$field['field_name']] = t($field['widget']['label']); |
| 586 |
|
} |
| 587 |
|
} |
| 588 |
|
$form['field_name'] = array( |
| 589 |
|
'#title' => t('Node reference field'), |
| 590 |
|
'#type' => 'select', |
| 591 |
|
'#options' => $options, |
| 592 |
|
'#default_value' => $conf['field_name'], |
| 593 |
|
'#prefix' => '<div class="clear-block">', |
| 594 |
|
'#suffix' => '</div>', |
| 595 |
|
); |
| 596 |
|
|
| 597 |
|
return $form; |
| 598 |
} |
} |