View's Block that pulls content based on a page's node author (UID)
The following argument code solves 2 problems I was facing. Problem #1, How to create a "related content" block using views that displays content authored by the same user who authored a node that's being viewed in full page mode (ie, node/XX). Problem #2 (which is much more interesting), how to display a block containing a custom view of nodeprofile fields describing the author of a node viewed in full page mode.
There are two steps:
1) Select the argument type: User: UID is Author.
(You can set it's default to whatever makes sense in your particular case, I set the default to "use empty text" because I don't want anything to display in the block if the view is not passed the appropriate argument. You could also do this with your block display settings too, only showing the block on pages with the URL: node/*)
2) Add the following argument handling code:
<?php
if ($view->build_type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
$node=node_load(arg(1));
$args[0] = $node->uid;
}
return $args;
?>(without the
<?php
?>The code above first checks to make sure that we're receiving arguments from a URL in the form of node/X (if you are using pathauto, that's okay, as it just displays an alias for the true URL). Then, it takes the node ID from the URL and uses the node_load function to load the node into the array $node. Finally, the view's $args[0] variable pulls the node's author ID (UID) and passes it to the UID argument.
Below is an export of a simple view that pulls a block containing the usernode for the author of a node. Using nodeprofile, nodefamily and views fusion, you can extend this view to show fields from additional nodeprofile content types.
<?php
$view = new stdClass();
$view->name = 'usernode_block';
$view->description = 'This block pulls the usernode associated with the author of a node.';
$view->access = array (
);
$view->view_args_php = 'if ($view->build_type == \'block\' && arg(0) == \'node\' && is_numeric(arg(1))) {
$node=node_load(arg(1));
$args[0] = $node->uid;
}
return $args;';
$view->page = FALSE;
$view->page_title = '';
$view->page_header = '';
$view->page_header_format = '3';
$view->page_footer = '';
$view->page_footer_format = '3';
$view->page_empty = '';
$view->page_empty_format = '3';
$view->page_type = 'node';
$view->url = '';
$view->use_pager = TRUE;
$view->nodes_per_page = '10';
$view->block = TRUE;
$view->block_title = '';
$view->block_header = '';
$view->block_header_format = '3';
$view->block_footer = '';
$view->block_footer_format = '3';
$view->block_empty = '';
$view->block_empty_format = '3';
$view->block_type = 'node';
$view->nodes_per_block = '10';
$view->block_more = FALSE;
$view->block_use_page_header = FALSE;
$view->block_use_page_footer = FALSE;
$view->block_use_page_empty = FALSE;
$view->sort = array (
);
$view->argument = array (
array (
'type' => 'uid',
'argdefault' => '7',
'title' => '',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'usernode',
),
),
);
$view->exposed_filter = array (
);
$view->requires = array(node);
$views[$view->name] = $view;
?>Blog Page code
I wanted to show author info also on the blog page, so I did a small addition to your code:
<?php
if ($view->build_type == 'block' && arg(0) == 'node' && is_numeric(arg(1))) {
$node=node_load(arg(1));
$args[0] = $node->uid;
}
if ($view->build_type == 'block' && arg(0) == 'blog' && is_numeric(arg(1))) {
$args[0] = arg(1);
}
return $args;
?>