node.views.inc 10.7 KB
Newer Older
1
<?php
2
// $Id$
3 4
/**
 * @file
5 6 7 8 9 10 11 12 13 14 15 16
 * Provide views data and handlers for node.module
 */

/**
 * @defgroup views_node_module node.module handlers
 *
 * Includes the tables 'node', 'node_revisions' and 'history'.
 * @{
 */

/**
 * Implementation of hook_views_data()
17
 */
18 19
function node_views_data() {
  // Basic table information.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  // Define the base group of this table. Fields that don't
  // have a group defined will go into this field by default.
  $data['node']['table']['group']  = t('Node');

  // Advertise this table as a possible base table
  $data['node']['table']['base'] = array(
    'field' => 'nid',
    'title' => t('Node'),
  );

  // For other base tables, explain how we join
  $data['node']['table']['join'] = array(
    'users' => array(
      'handler' => 'views_join', // this is actually optional
      'arguments' => array('node', 'users', 'uid', 'uid'),
     ),
    'node_revisions' => array(
38
      'handler' => 'views_join', // this is actually optional
39 40 41
      'arguments' => array('node', 'node_revisions', 'nid', 'nid'),
     ),
  );
42

43
  // ----------------------------------------------------------------
44 45 46
  // Fields

  // title
47
  $data['node']['title'] = array(
48
    'title' => t('Title'), // The item it appears as on the UI,
49 50 51 52 53 54 55 56
    'help' => t('The title of the node.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'field' => 'title', // the real field
      'group' => t('Node'), // The group it appears in on the UI,
      'handler' => 'views_handler_field_node',
      'arguments' => array(TRUE), // arguments to the handler
     ),
57 58
  );

59
  // nid
60 61 62
  $data['node']['nid'] = array(
    'title' => t('Nid'),
    'help' => t('The node ID of the node'), // The help that appears on the UI,
63
    // Information for displaying the nid
64 65 66 67
    'field' => array(
      'handler' => 'views_handler_field_node',
      'arguments' => array(TRUE),
    ),
68
    // Information for accepting a nid as an argument
69
    'argument' => array(
70
      'handler' => 'views_handler_argument_nid',
71 72
      'arguments' => array('title'),
    ),
73
    // Information for accepting a nid as a filter
74 75 76
    'filter' => array(
      'handler' => 'views_handler_filter',
    ),
77 78 79 80
    // Information for sorting on a nid.
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
81 82 83
  );

  // created field
84 85 86 87 88 89 90 91 92 93 94
  $data['node']['created'] = array(
    'title' => t('Post date'), // The item it appears as on the UI,
    'help' => t('The date the node was posted.'), // The help that appears on the UI,
    'field' => array(
      'handler' => 'views_handler_field_date',
      'arguments' => array(TRUE),
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
95 96

  // changed field
97
  $data['node']['changed'] = array(
98
    'title' => t('Updated date'), // The item it appears as on the UI,
99 100 101 102 103
    'help' => t('The date the node was last updated.'), // The help that appears on the UI,
    'field' => array(
      'handler' => 'views_handler_field_date',
      'arguments' => array(TRUE),
    ),
104
  );
105 106 107

  // Node type
  $data['node']['type'] = array(
108 109
    'title' => t('Type'), // The item it appears as on the UI,
    'help' => t('The type of a node (for example, "blog entry", "forum post", "story", etc)'), // The help that appears on the UI,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    'field' => array(
      'handler' => 'views_handler_field_node_type',
      'arguments' => array(TRUE),
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_node_type',
    ),
  );

  // published status
  $data['node']['status'] = array(
    'title' => t('Published'), // The item it appears as on the UI,
    'help' => t('The published status of the node.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_boolean',
      'arguments' => array(TRUE), // arguments to the handler
     ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
131 132
  );

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  // promote status
  $data['node']['promote'] = array(
    'title' => t('Promoted to front page'), // The item it appears as on the UI,
    'help' => t('The front page of the node.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_boolean',
      'arguments' => array(TRUE), // arguments to the handler
     ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // sticky
  $data['node']['sticky'] = array(
    'title' => t('Sticky'), // The item it appears as on the UI,
    'help' => t('Whether or not the node is sticky.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_boolean',
      'arguments' => array(TRUE), // arguments to the handler
     ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // ----------------------------------------------------------------------
  // Node revisions table

  // Define the base group of this table. Fields that don't
  // have a group defined will go into this field by default.
166
  $data['node_revisions']['table']['group']  = t('Node');
167

168
  // Advertise this table as a possible base table
169
  $data['node_revisions']['table']['base'] = array(
170 171 172 173 174
    'field' => 'vid',
    'title' => t('Node revisions'),
  );

  // For other base tables, explain how we join
175
  $data['node_revisions']['table']['join'] = array(
176
    'node' => array(
177
      'handler' => 'views_join', // this is actually optional
178 179 180
      'arguments' => array('node_revisions', 'node', 'vid', 'vid'),
     ),
    'user' => array(
181
      'handler' => 'views_join', // this is actually optional
182 183 184 185 186 187 188
      'arguments' => array('node_revisions', 'node', 'vid', 'vid'),
     ),
  );

  // Body field
  $data['node_revisions']['body'] = array(
    'title' => t('Body'), // The item it appears as on the UI,
189
    'help' => t('The actual, full data in the body field; this may not be valid data on all node types.'), // The help that appears on the UI,
190 191 192
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_markup',
193
      'arguments' => array('format'), // arguments to the handler
194 195
     ),
  );
196 197 198 199 200 201 202 203 204 205 206 207

  // Teaser field
  $data['node_revisions']['teaser'] = array(
    'title' => t('Teaser'), // The item it appears as on the UI,
    'help' => t('The stored teaser field. This may not be valid or useful data on all node types.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'handler' => 'views_handler_field_markup',
      'arguments' => array('format'), // arguments to the handler
     ),
  );

208 209 210 211 212
  return $data;
}

/**
 * Field handler to provide simple renderer that allows linking to a node.
213 214
 *
 * @ingroup views_field_handlers
215 216 217
 */
class views_handler_field_node extends views_handler_field {
  /**
218
   * Override init function to provide generic option to link to node.
219
   */
220 221
  function init(&$view, &$data) {
    parent::init($view, $data);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    if (isset($data->options['link_to_node']) && $view->base_table != 'node') {
      $this->additional_fields[] = $nid;
      $this->nid_field = 'node_nid';
    }
    else {
      $this->nid_field = 'nid';
    }
  }

  /**
   * Provide link to node option
   */
  function options_form(&$form) {
    $form['link_to_node'] = array(
      '#title' => t('Link this field to its node'),
      '#type' => 'checkbox',
      '#default_value' => $this->data->options['link_to_node'],
    );
  }

  function render_link($data, $values) {
    if (!empty($this->data->options['link_to_node'])) {
      return l($data, "node/" . $values->{$this->node_field}, array('html' => TRUE));
    }
    else {
      return $data;
    }
  }

  function render($values) {
    return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  }
}

/**
 * Field handler to translate a node type into its readable form.
258 259
 *
 * @ingroup views_field_handlers
260 261 262 263 264 265 266
 */
class views_handler_field_node_type extends views_handler_field_node {
  function render($values) {
    $value = node_get_types('name', $values->{$this->field_alias});
    return $this->render_link(check_plain($value), $values);
  }
}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

/**
 * Argument handler to accept a node type.
 *
 * @ingroup views_argument_handlers
 */
class views_handler_argument_node_type extends views_handler_argument {
  function construct() {
    parent::construct('type');
  }

  /**
   * Override the behavior of summary_name(). Get the user friendly version
   * of the node type.
   */
  function summary_name($data) {
    return $this->node_type($data->{$this->name_alias});
  }

  /**
   * Override the behavior of title(). Get the user friendly version of the
   * node type.
   */
  function title() {
    return $this->node_type($this->argument);
  }

  function node_type($type) {
    $output = node_get_types('name', $type);
    if (empty($output)) {
      $output = t('Unknown node type');
    }
    return check_plain($output);
  }
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/**
 * Argument handler to accept a node id.
 *
 * @ingroup views_argument_handlers
 */
class views_handler_argument_node_nid extends views_handler_argument {
  // No constructor is necessary.

  /**
   * Override the behavior of title(). Get the title of the node.
   */
  function title() {
    $title = db_fetch_result(db_query(db_rewrite_sql("SELECT n.title FROM {node} n WHERE n.nid = %d"), $this->argument));
    if (empty($title)) {
      return t('No title');
    }

    return check_plain();
  }
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

/**
 * Implementation of hook_views_plugins
 */
function node_views_plugins() {
  return array(
    'module' => 'views', // This just tells our themes are elsewhere.
    'row' => array(
      'node' => array(
        'title' => t('Node'),
        'help' => t('Display the node with standard node view.'),
        'handler' => 'views_row_plugin_node_view',
        'theme' => 'views_view_row_node',
        'base' => array('node'), // only works with 'node' as base.
      ),
    ),
  );
}

/**
 * Plugin which performs a node_view on the resulting object.
 *
 * @ingroup views_row_plugins
 */
class views_row_plugin_node_view extends views_object {
  /**
   * Override the behavior of the render() function.
   */
  function render(&$view, $row) {
    return theme(array('views_view_row_node__' . $view->name, 'views_view_row_node'), $view, $row);
  }
}

/**
 * Template helper for theme_views_view_row_node
 */
function template_preprocess_views_view_row_node(&$vars) {
  $vars['node'] = ''; // make sure var is defined.
  $nid = $vars['row']->nid;
  if (!is_numeric($nid)) {
    return;
  }

  $node = node_load($nid);

  if (empty($node)) {
    return;
  }

  // @todo add settings so that this can choose teaser, links, etc.
  $vars['node'] = node_view($node, TRUE);
}

377 378 379
/**
 * @}
 */