Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created June 22, 2011 20:18
Show Gist options
  • Save msonnabaum/1041044 to your computer and use it in GitHub Desktop.
Save msonnabaum/1041044 to your computer and use it in GitHub Desktop.
<?php
/**
* Loads a node from cck values.
*/
function welcom_node_load($node_data, $type = NULL) {
// Create a new view
$view = views_new_view();
$view->name = 'cck_node_lookup';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('items_per_page', 1);
$handler->override_option('fields', array(
'nid' => array(
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
),
));
$filters = array();
foreach ((array)$node_data as $field_name => $value) {
$filters[$field_name . '_value'] = array(
'operator' => '=',
'value' => array(
'value' => $value,
),
'id' => $field_name . '_value',
'table' => 'node_data_' . $field_name,
'field' => $field_name . '_value',
);
}
if ($type) {
$filters['type'] = array(
'operator' => '=',
'value' => array(
$type => $type,
),
'id' => 'type',
'table' => 'node',
'field' => 'type',
);
}
$handler->override_option('filters', $filters);
$views[$view->name] = $view;
$view->execute();
if (isset($view->result[0])) {
$node = node_load($view->result[0]->nid);
return $node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment