nodehierarchy_views.module in Node Hierarchy 6.3
Embed a view of children onto a node.
File
nodehierarchy_views/nodehierarchy_views.moduleView source
<?php
/**
* @file
* Embed a view of children onto a node.
*
*/
/**
* Implementation of hook_theme().
*/
function nodehierarchy_views_theme() {
return array(
'nodehiararchy_children_embed' => array(
'arguments' => array(
$element,
),
),
);
}
/**
* Implementation of hook_perm().
*/
function nodehierarchy_views_perm() {
return array(
'access embedded child view',
'edit embedded child view',
);
}
/**
* Implementation of hook_content_extra_fields.
*/
function nodehierarchy_views_content_extra_fields($type_name) {
$extra = array();
if (nodehierarchy_node_can_be_parent($type_name)) {
$extra['nodehierarchy_children'] = array(
'label' => t('Node Hierarchy Children'),
'description' => t('Node Hierarchy embedded children view.'),
'weight' => 10,
);
$extra['nodehierarchy_children_links'] = array(
'label' => t('Node Hierarchy Add Child Links'),
'description' => t('Links to create a new child.'),
'weight' => 10,
);
}
return $extra;
}
/**
* Implementation of hook_nodeapi().
*/
function nodehierarchy_views_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'insert':
return nodehierarchy_views_insert($node);
break;
case 'update':
return nodehierarchy_views_update($node);
break;
case 'prepare':
return nodehierarchy_views_prepare_node($node);
break;
case 'load':
return nodehierarchy_views_load_node($node);
break;
case 'delete':
nodehierarchy_views_delete_node($node);
break;
case 'validate':
break;
case 'view':
if (!$teaser && user_access('access embedded child view')) {
nodehierarchy_views_embed_children($node);
}
break;
}
}
function nodehierarchy_views_nodehierarchy_node_form($node) {
$form = array();
$default_value = NULL;
if (!empty($node->nh_children_view)) {
$display = !empty($node->nh_children_view_display) ? $node->nh_children_view_display : 'default';
$default_value = $node->nh_children_view . ':' . $display;
}
if ($node && nodehierarchy_node_can_be_parent($node)) {
$form['nh_children_view'] = array(
'#type' => user_access('edit embedded child view') ? 'select' : 'value',
'#title' => t('Embed Children View'),
'#multiple' => FALSE,
'#options' => _nodehierarchy_views_view_options(),
'#required' => FALSE,
'#default_value' => $default_value,
'#description' => t("Embed a view containing this node's children into the node's page view"),
);
}
return $form;
}
/**
* Implementation of hook_nodehierarchy_node_type_settings_form().
*/
function nodehierarchy_views_nodehierarchy_node_type_settings_form($type) {
$form = array();
if (nodehierarchy_node_can_be_parent($type)) {
$form['children']['nh_view_default_' . $type] = array(
'#type' => 'select',
'#title' => t('Default Children View'),
'#multiple' => FALSE,
'#options' => _nodehierarchy_views_view_options(),
'#required' => FALSE,
'#default_value' => variable_get('nh_view_default_' . $type, NULL),
'#description' => t('Default for the embed children view feature.'),
'#weight' => -1,
);
}
return $form;
}
/**
* Implementation of hook_load().
*/
function nodehierarchy_views_load_node($node) {
$additions = db_fetch_array(db_query('SELECT nh_children_view, nh_children_view_display FROM {nodehierarchy_views} WHERE nid = %d', $node->nid));
return $additions;
}
/**
* Set a default embedded view.
*/
function nodehierarchy_views_prepare_node(&$node) {
// Set the default children view if there is one for this type and if the node has not been saved yet.
if (empty($node->nid) && !$node->nh_children_view && ($children_view = variable_get('nh_view_default_' . $node->type, NULL))) {
list($node->nh_children_view, $node->nh_children_display) = explode(':', $children_view);
}
}
/**
* Delete the node_hierarchy_views row when a node is deleted.
*/
function nodehierarchy_views_delete_node($node) {
db_query('DELETE FROM {nodehierarchy_views} WHERE nid = %d', $node->nid);
}
/**
* Update the given embedded view.
*/
function nodehierarchy_views_update($node) {
if (user_access('edit embedded child view')) {
db_query('DELETE FROM {nodehierarchy_views} WHERE nid = %d', $node->nid);
if (@$node->nh_children_view) {
list($view, $display) = explode(':', $node->nh_children_view);
db_query("INSERT INTO {nodehierarchy_views} (nid, nh_children_view, nh_children_view_display) VALUES (%d, '%s', '%s')", $node->nid, $view, $display);
}
}
}
/**
* Insert the given embedded view.
*/
function nodehierarchy_views_insert($node) {
// If the user has access to edit the view, update as usual.
if (user_access('edit embedded child view')) {
nodehierarchy_views_update($node);
}
elseif ($children_view = variable_get('nh_view_default_' . $node->type, NULL)) {
list($view, $display) = explode(':', $children_view);
db_query("INSERT INTO {nodehierarchy_views} (nid, nh_children_view) VALUES (%d, '%s')", $node->nid, $view, $display);
}
}
/**
* Add the embedded children view to the node body if appropriate.
*/
function nodehierarchy_views_embed_children(&$node) {
$display = @$node->nh_children_view_display ? @$node->nh_children_view_display : 'default';
// Get the arguments to send to the view. This should allow other view arguments to still work.
$arguments = explode('/', $_GET['q']);
// First arg will be 'node', the second will be the node id. Remove them.
array_shift($arguments);
array_shift($arguments);
// Add the node id of the given node.
array_unshift($arguments, $node->nid);
// Defer the rendering of the view until the theme function so it can be overriden or altered.
$node->content['nodehierarchy_children'] = array(
'#type' => 'nodehiararchy_children_embed',
'#nodehierarchy_view' => $node->nh_children_view,
'#nodehierarchy_view_display' => @$node->nh_children_view_display ? @$node->nh_children_view_display : 'default',
'#nodehierarchy_view_args' => $arguments,
'#weight' => function_exists('content_extra_field_weight') ? content_extra_field_weight($type, 'nodehierarchy_children') : 10,
);
$node->content['nodehierarchy_children_links'] = array(
"#value" => theme('nodehierarchy_new_child_links', $node),
"#weight" => function_exists('content_extra_field_weight') ? content_extra_field_weight($type, 'nodehierarchy_children_links') : 11,
);
}
/**
* Prepare a list of views for selection.
*/
function _nodehierarchy_views_view_options() {
$options = array();
$options[0] = '-- ' . t('NONE') . ' --';
$views = views_get_all_views();
foreach ($views as $view_name => $view) {
foreach ($view->display as $display_id => $display) {
if (_nodehierarchy_views_is_nh_view($view, $display_id)) {
$options["{$view_name}:{$display_id}"] = $view->name . ': ' . $display->display_title;
}
}
}
return $options;
}
/**
* Determine if the given view/display is node hierarchy compatible.
*/
function _nodehierarchy_views_is_nh_view($view, $display = 'default') {
$view
->set_display($display);
$view
->build($display);
$arg = reset($args = (array) $view->argument);
if (!empty($arg->table) && ($arg->table == "nh_parent" || $arg->table == "nh_ancestor")) {
return TRUE;
}
return FALSE;
}
/**
* Render the embedded chidren for the given node.
*/
function theme_nodehiararchy_children_embed($element) {
if (!empty($element['#nodehierarchy_view']) && ($view = views_get_view($element['#nodehierarchy_view']))) {
$display = $element['#nodehierarchy_view_display'];
$arguments = $element['#nodehierarchy_view_args'];
$view->override_path = "node";
return $view
->execute_display($display, $arguments);
}
return '';
}
Functions
Name | Description |
---|---|
nodehierarchy_views_content_extra_fields | Implementation of hook_content_extra_fields. |
nodehierarchy_views_delete_node | Delete the node_hierarchy_views row when a node is deleted. |
nodehierarchy_views_embed_children | Add the embedded children view to the node body if appropriate. |
nodehierarchy_views_insert | Insert the given embedded view. |
nodehierarchy_views_load_node | Implementation of hook_load(). |
nodehierarchy_views_nodeapi | Implementation of hook_nodeapi(). |
nodehierarchy_views_nodehierarchy_node_form | |
nodehierarchy_views_nodehierarchy_node_type_settings_form | Implementation of hook_nodehierarchy_node_type_settings_form(). |
nodehierarchy_views_perm | Implementation of hook_perm(). |
nodehierarchy_views_prepare_node | Set a default embedded view. |
nodehierarchy_views_theme | Implementation of hook_theme(). |
nodehierarchy_views_update | Update the given embedded view. |
theme_nodehiararchy_children_embed | Render the embedded chidren for the given node. |
_nodehierarchy_views_is_nh_view | Determine if the given view/display is node hierarchy compatible. |
_nodehierarchy_views_view_options | Prepare a list of views for selection. |