views_handler_field_node.inc

  1. 3.x modules/node/views_handler_field_node.inc
  2. 2.x modules/node/views_handler_field_node.inc

Contains the basic 'node' field handler.

File

modules/node/views_handler_field_node.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the basic 'node' field handler.
  5. */
  6. /**
  7. * Field handler to provide simple renderer that allows linking to a node.
  8. * Definition terms:
  9. * - link_to_node default: Should this field have the checkbox "link to node" enabled by default.
  10. */
  11. class views_handler_field_node extends views_handler_field {
  12. /**
  13. * Constructor to provide additional field to add.
  14. */
  15. function construct() {
  16. parent::construct();
  17. $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
  18. if (module_exists('translation')) {
  19. $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
  20. }
  21. }
  22. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['link_to_node'] = array('default' => isset($this->definition['link_to_node default']) ? $this->definition['link_to_node default'] : FALSE);
  25. return $options;
  26. }
  27. /**
  28. * Provide link to node option
  29. */
  30. function options_form(&$form, &$form_state) {
  31. parent::options_form($form, $form_state);
  32. $form['link_to_node'] = array(
  33. '#title' => t('Link this field to its node'),
  34. '#description' => t('This will override any other link you have set.'),
  35. '#type' => 'checkbox',
  36. '#default_value' => !empty($this->options['link_to_node']),
  37. );
  38. }
  39. /**
  40. * Render whatever the data is as a link to the node.
  41. *
  42. * Data should be made XSS safe prior to calling this function.
  43. */
  44. function render_link($data, $values) {
  45. if (!empty($this->options['link_to_node'])) {
  46. if ($data !== NULL && $data !== '') {
  47. $this->options['alter']['make_link'] = TRUE;
  48. $this->options['alter']['path'] = "node/" . $values->{$this->aliases['nid']};
  49. if (isset($this->aliases['language'])) {
  50. $languages = language_list();
  51. if (isset($languages[$values->{$this->aliases['language']}])) {
  52. $this->options['alter']['language'] = $languages[$values->{$this->aliases['language']}];
  53. }
  54. else {
  55. unset($this->options['alter']['language']);
  56. }
  57. }
  58. }
  59. else {
  60. $this->options['alter']['make_link'] = FALSE;
  61. }
  62. }
  63. return $data;
  64. }
  65. function render($values) {
  66. return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  67. }
  68. }