| 1 |
<?php
|
| 2 |
|
| 3 |
|
| 4 |
function theme_relevant_content_field_formatter_general($variables) {
|
| 5 |
// Get the element from the variables
|
| 6 |
$element = $variables['element']['element'];
|
| 7 |
|
| 8 |
// If there are no items, return nothing. Anything else risks empty fields
|
| 9 |
if (empty($element['items']['#item'])) return;
|
| 10 |
|
| 11 |
// Get the type from the variables
|
| 12 |
$type = $variables['type'];
|
| 13 |
|
| 14 |
// We only need this bit for token_* formatters
|
| 15 |
if ($type == 'token_full' || $type == 'token_teaser') {
|
| 16 |
// Get the token_full settings and make sure they're plain (this is important)
|
| 17 |
$token_pattern = check_markup($element['#settings']['#item'][$type]['body'], $element['#settings']['#item'][$type]['format']);
|
| 18 |
}
|
| 19 |
|
| 20 |
// Init the output, count and num item variables
|
| 21 |
$output = '';
|
| 22 |
$count = 0;
|
| 23 |
$num_items = count($element['items']['#item']);
|
| 24 |
|
| 25 |
// Loop over all the items and create the field output
|
| 26 |
foreach ($element['items']['#item'] as $item) {
|
| 27 |
$attributes = array('class' => 'field-item ' . ($count % 2 ? 'even' : 'odd'));
|
| 28 |
|
| 29 |
// Append a "first" class to the row
|
| 30 |
if ($count == 0) {
|
| 31 |
$attributes['class'] .= ' first';
|
| 32 |
}
|
| 33 |
|
| 34 |
// Append a "last" class to the row
|
| 35 |
if ($count == $num_items - 1) {
|
| 36 |
$attributes['class'] .= ' last';
|
| 37 |
}
|
| 38 |
|
| 39 |
// Depending on the type, render a field
|
| 40 |
switch ($type) {
|
| 41 |
default:
|
| 42 |
case 'default' :
|
| 43 |
$result = l($item['title'], 'node/'. $item['nid']);
|
| 44 |
break;
|
| 45 |
|
| 46 |
case 'plain' :
|
| 47 |
$result = check_plain($item['title']);
|
| 48 |
break;
|
| 49 |
|
| 50 |
case 'teaser' :
|
| 51 |
case 'full' :
|
| 52 |
$result = drupal_render(node_build(node_load($item['nid']), $type));
|
| 53 |
break;
|
| 54 |
|
| 55 |
case 'token_full' :
|
| 56 |
case 'token_teaser' :
|
| 57 |
// Run a token replace on the content
|
| 58 |
$types = array('global' => NULL, 'node' => node_load($item['nid']));
|
| 59 |
$result = token_replace($token_pattern, $types);
|
| 60 |
break;
|
| 61 |
}
|
| 62 |
|
| 63 |
// Generate the field
|
| 64 |
$output .= theme('relevant_content_individual_field', array('attributes' => $attributes, 'result' => $result));
|
| 65 |
//$output .= '<div'. drupal_attributes($attributes) .'>'. $result .'</div>';
|
| 66 |
$count++;
|
| 67 |
}
|
| 68 |
|
| 69 |
// Wrap the result in a field-items wrapper to make it content-field.tpl.php consistent
|
| 70 |
return '<div class="field-items">'. $output .'</div>';
|
| 71 |
}
|
| 72 |
|
| 73 |
|
| 74 |
/**
|
| 75 |
* Theme wrapper for output of individual fields.
|
| 76 |
*/
|
| 77 |
function theme_relevant_content_individual_field($variables) {
|
| 78 |
return '<div'. drupal_attributes($variables['attributes']) .'>'. $variables['result'] .'</div>';
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Default formatter - hyperlinked nodes
|
| 83 |
*/
|
| 84 |
function theme_field_formatter_relevant_content_default($element) {
|
| 85 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'default'));
|
| 86 |
}
|
| 87 |
|
| 88 |
|
| 89 |
/**
|
| 90 |
* Optional formatter - plain text nodes
|
| 91 |
*/
|
| 92 |
function theme_field_formatter_relevant_content_plain($element) {
|
| 93 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'plain'));
|
| 94 |
}
|
| 95 |
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Optional formatter - teaser nodes
|
| 99 |
*/
|
| 100 |
function theme_field_formatter_relevant_content_teaser($element) {
|
| 101 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'teaser'));
|
| 102 |
}
|
| 103 |
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Optional formatter - full nodes
|
| 107 |
*/
|
| 108 |
function theme_field_formatter_relevant_content_full($element) {
|
| 109 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'full'));
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Optional formatter - Token "teaser" field
|
| 115 |
*/
|
| 116 |
function theme_field_formatter_relevant_content_token_teaser($element) {
|
| 117 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'token_teaser'));
|
| 118 |
}
|
| 119 |
|
| 120 |
|
| 121 |
/**
|
| 122 |
* Optional formatter - Token "full" field
|
| 123 |
*/
|
| 124 |
function theme_field_formatter_relevant_content_token_full($element) {
|
| 125 |
return theme('relevant_content_field_formatter_general', array('element' => $element, 'type' => 'token_full'));
|
| 126 |
}
|
| 127 |
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Theme function for rendering the relevant nodes into a block.
|
| 131 |
*
|
| 132 |
* This is provided as an item list by default, however a themer can
|
| 133 |
* easily override this to make a teaser list or table.
|
| 134 |
*
|
| 135 |
* Each item will default to a hyperlink to the node unless the token pattern is set.
|
| 136 |
*
|
| 137 |
* @param $nodes
|
| 138 |
* Associative array where the key is the node id and the value is the node title
|
| 139 |
* @param $header
|
| 140 |
* Optional string to display at the top of the block
|
| 141 |
*/
|
| 142 |
function theme_relevant_content_block($variables) {
|
| 143 |
$settings = variable_get('relevant_content', array());
|
| 144 |
|
| 145 |
// Default to "link" type
|
| 146 |
$type = 'link';
|
| 147 |
|
| 148 |
// If we have a delta (we should - only 3rd party legacy code should cause us not to) then get the settings and token settings.
|
| 149 |
// Also check tokens is enabled. Its optional
|
| 150 |
if (isset($variables['delta']) && isset($settings[$variables['delta']])) {
|
| 151 |
// Cleanup the token pattern
|
| 152 |
$token_pattern = trim($settings[$variables['delta']]['token_settings']);
|
| 153 |
|
| 154 |
// If the token pattern is not empty, switch to tokens mode
|
| 155 |
if (!empty($token_pattern)) {
|
| 156 |
$type = 'tokens';
|
| 157 |
}
|
| 158 |
}
|
| 159 |
|
| 160 |
$items = array();
|
| 161 |
foreach ($variables['nodes'] as $node) {
|
| 162 |
// If we're a link, default to a hyperlink - otherwise we should use tokens.
|
| 163 |
switch ($type) {
|
| 164 |
default :
|
| 165 |
case 'link' :
|
| 166 |
$items[] = l($node['title'], 'node/'. $node['nid']);
|
| 167 |
break;
|
| 168 |
|
| 169 |
case 'tokens' :
|
| 170 |
$objects = array('global' => NULL, 'node' => node_load($node['nid']));
|
| 171 |
$items[] = token_replace($token_pattern, $objects);
|
| 172 |
break;
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
$output = '';
|
| 177 |
if ($variables['header']) {
|
| 178 |
$output = check_markup($variables['header']);
|
| 179 |
}
|
| 180 |
|
| 181 |
$output .= theme('item_list', array('items' => $items));
|
| 182 |
return $output;
|
| 183 |
}
|
| 184 |
|
| 185 |
|
| 186 |
/**
|
| 187 |
* An internal theme function to render the tokens to help the user. NOTE: Why is this not a core theme function?!
|
| 188 |
*/
|
| 189 |
function theme_relevant_content_token_help() {
|
| 190 |
$token_info = token_info();
|
| 191 |
$output = '<p>'. t('Available tokens are:') .'</p>';
|
| 192 |
$output .= '<dl>';
|
| 193 |
foreach(array('node', 'site', 'date') as $key) {
|
| 194 |
$output .= "<dt><strong>{$token_info['types'][$key]['name']}</strong> - {$token_info['types'][$key]['description']}</dt>";
|
| 195 |
$token_pairs = array();
|
| 196 |
foreach ($token_info['tokens'][$key] as $token => $info) {
|
| 197 |
$token_pairs[] = "<code>[{$key}:{$token}]</code> - {$info['name']}";
|
| 198 |
}
|
| 199 |
$output .= '<dd>'. theme('item_list', array('items' => $token_pairs)) .'</dd>';
|
| 200 |
}
|
| 201 |
$output .= '</dl>';
|
| 202 |
|
| 203 |
return $output;
|
| 204 |
}
|