| 1 |
<?php
|
| 2 |
// $Id: viewfield.theme.inc,v 1.3 2009/05/24 05:33:34 darrenoh Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Theme functions.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Return a themed view avoiding viewfield recursion.
|
| 11 |
*/
|
| 12 |
function theme_viewfield_formatter_default($element) {
|
| 13 |
global $_viewfield_stack;
|
| 14 |
// For safety's sake, we can only display 2 levels of viewfields.
|
| 15 |
if (count($_viewfield_stack) <= 2) {
|
| 16 |
list($view_name, $display) = explode('|', $element['#item']['vname'], 2);
|
| 17 |
$view_args = _viewfield_get_view_args($element['#item']['token_enabled'], $element['#item']['vargs'], $element['#node']);
|
| 18 |
$node = $element['#node'];
|
| 19 |
// Need to prevent recursive views and node building, but don't need to do
|
| 20 |
// it on new node previews.
|
| 21 |
if ($node->nid) {
|
| 22 |
_viewfield_nodestack_push($node->nid);
|
| 23 |
}
|
| 24 |
array_unshift($view_args, $view_name, $display);
|
| 25 |
$output = call_user_func_array('views_embed_view', $view_args);
|
| 26 |
// This node is "safe" again.
|
| 27 |
if ($node->nid) {
|
| 28 |
_viewfield_nodestack_pop();
|
| 29 |
}
|
| 30 |
return $output;
|
| 31 |
}
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Perform argument replacement
|
| 36 |
*/
|
| 37 |
function _viewfield_get_view_args($token_enabled, $vargs, $node) {
|
| 38 |
$args = array();
|
| 39 |
// Prevent token_replace() from running this function a second time
|
| 40 |
// before it completes the first time.
|
| 41 |
static $tokens = TRUE;
|
| 42 |
if ($tokens && !empty($vargs)) {
|
| 43 |
$pos = 0;
|
| 44 |
while ($pos < strlen($vargs)) {
|
| 45 |
$found = FALSE;
|
| 46 |
// If string starts with a quote, start after quote and get everything
|
| 47 |
// before next quote.
|
| 48 |
if (strpos($vargs, '"', $pos) === $pos) {
|
| 49 |
if (($quote = strpos($vargs, '"', ++$pos)) !== FALSE) {
|
| 50 |
// Skip pairs of quotes.
|
| 51 |
while (!(($ql = strspn($vargs, '"', $quote)) & 1)) {
|
| 52 |
$quote = strpos($vargs, '"', $quote + $ql);
|
| 53 |
}
|
| 54 |
$args[] = str_replace('""', '"', substr($vargs, $pos, $quote + $ql - $pos - 1));
|
| 55 |
$pos = $quote + $ql + 1;
|
| 56 |
$found = TRUE;
|
| 57 |
}
|
| 58 |
}
|
| 59 |
elseif (($comma = strpos($vargs, ',', $pos)) !== FALSE) {
|
| 60 |
// Otherwise, get everything before next comma.
|
| 61 |
$args[] = substr($vargs, $pos, $comma - $pos);
|
| 62 |
// Skip to after comma and repeat
|
| 63 |
$pos = $comma + 1;
|
| 64 |
$found = TRUE;
|
| 65 |
}
|
| 66 |
if (!$found) {
|
| 67 |
$args[] = substr($vargs, $pos);
|
| 68 |
$pos = strlen($vargs);
|
| 69 |
}
|
| 70 |
}
|
| 71 |
if ($token_enabled) {
|
| 72 |
$tokens = FALSE;
|
| 73 |
// If the view field is being loaded as a "view field" of "view row",
|
| 74 |
// instead of a simple "node field", the node object is not fully populated:
|
| 75 |
// we need a full node to perform a correct replacement.
|
| 76 |
$node_values = $node;
|
| 77 |
if ($node->build_mode == 'views') {
|
| 78 |
$node_values = node_load($node->nid);
|
| 79 |
}
|
| 80 |
$args = token_replace($args, 'node', $node_values);
|
| 81 |
$tokens = TRUE;
|
| 82 |
}
|
| 83 |
// For backwards compatibility, we scan for %nid, etc.
|
| 84 |
global $user;
|
| 85 |
foreach ($args as $key => $a) {
|
| 86 |
$args[$key] = strtr($a, array('%nid' => $node->nid, '%author' => $node_values->uid, '%viewer' => $user->uid));
|
| 87 |
}
|
| 88 |
}
|
| 89 |
return $args;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Function for adding a node ID to the global stack of node IDs. This prevents
|
| 94 |
* us from recursively building a node, with a view, with the node, with the
|
| 95 |
* view...
|
| 96 |
*/
|
| 97 |
function _viewfield_nodestack_push($nid) {
|
| 98 |
global $_viewfield_stack;
|
| 99 |
if (!isset($_viewfield_stack)) {
|
| 100 |
$_viewfield_stack = array();
|
| 101 |
}
|
| 102 |
$_viewfield_stack[] = $nid;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Function for removing a node ID from the global stack of node IDs when there
|
| 107 |
* is no longer a danger of building a node, with a view, with the node, with
|
| 108 |
* the view...
|
| 109 |
*/
|
| 110 |
function _viewfield_nodestack_pop() {
|
| 111 |
global $_viewfield_stack;
|
| 112 |
return array_pop($_viewfield_stack);
|
| 113 |
}
|
| 114 |
|
| 115 |
function theme_viewfield_select($element) {
|
| 116 |
if (!empty($element['#children'])) {
|
| 117 |
$field = $element['#field_info'][$element['#field_name']];
|
| 118 |
if ($field['multiple'] && $element['#delta'] == 0) {
|
| 119 |
// This is needed only for multiple viewfields.
|
| 120 |
drupal_add_css(drupal_get_path('module', 'viewfield') .'/theme/viewfield.css');
|
| 121 |
}
|
| 122 |
return '<div class="viewfield-select">'. $element['#children'] .'</div>';
|
| 123 |
}
|
| 124 |
}
|