I search around but cannot find anything that can provide this. Here it goes:-
I have a views that would display a list of available sports to register on a child node (of content type named Child). So when user viewing the child node, together with the information about the child such as age, class, dob etc, a list of available sports for them to register would be displayed below. This can easily be achieved with views_attach. I created a views with all field that I want, add another Node Content display and set it to display on Child content type.
Now I want to add another field, using the standard Global custom field that would be a link to a form that they need to fill in to register. For the path to this form, I need 2 information - The child node id and the sports node id. Something like:-
child/[nid_of_child]/sports/register/[nid_of_sports].
The problem is the [nid_of_child], it's not available in the list of fields that I can add to the views. Looking in this module code, especially in hook_nodeapi, I know that the views that being displayed is attached to the current node, so somehow the node properties already available to the views (around line 119 of views_attach.module):-
if ($view->access($info['display'])) {
$view->current_node = $node;
$result = $view->execute_display($info['display']);
if (!empty($result)) {
.... //
So, taking a look at how views_customfield work (actually how to provide additional field to the views2), I managed to get this working. Basically it just defining the hook_views_data and hook_views_handler to define the new field and return the value of $this->view->current_node->nid in the handler.
Some blind copy n paste from views_customfield module:-
function views_attach_views_data() {
$data['attachfield']['table']['group'] = t('Attachfield');
$data['attachfield']['table']['join'] = array(
'#global' => array(),
);
$data['attachfield']['attached_nid'] = array(
'title' => t('Attached Node nid'),
'help' => t('Nid of attached node'),
'field' => array(
'handler' => 'views_attach_handler_field_nid',
'click sortable' => FALSE,
'notafield' => TRUE,
),
);
return $data;
}
class views_attach_handler_field_nid extends views_handler_field {
var $rownumber;
function init(&$view, $options) {
parent::init($view, $options);
$this->rownumbers = 0;
}
function query() {
// Override parent::query() and don't alter query.
$this->field_alias = 'views_attach_nid_'. $this->position;
}
function render($values) {
return $this->view->current_node->nid;
}
}
If this is something useful to views_attach, I'm willing to create proper patch for this or if someone could lead me to better solution without me hacking the code.