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. * @ingroup views_field_handlers
  12. */
  13. class views_handler_field_node extends views_handler_field {
  14. function init(&$view, &$options) {
  15. parent::init($view, $options);
  16. // Don't add the additional fields to groupby
  17. if (!empty($this->options['link_to_node'])) {
  18. $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
  19. if (module_exists('translation')) {
  20. $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
  21. }
  22. }
  23. }
  24. function option_definition() {
  25. $options = parent::option_definition();
  26. $options['link_to_node'] = array('default' => isset($this->definition['link_to_node default']) ? $this->definition['link_to_node default'] : FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. /**
  30. * Provide link to node option
  31. */
  32. function options_form(&$form, &$form_state) {
  33. $form['link_to_node'] = array(
  34. '#title' => t('Link this field to the original piece of content'),
  35. '#description' => t("Enable to override this field's links."),
  36. '#type' => 'checkbox',
  37. '#default_value' => !empty($this->options['link_to_node']),
  38. );
  39. parent::options_form($form, $form_state);
  40. }
  41. /**
  42. * Render whatever the data is as a link to the node.
  43. *
  44. * Data should be made XSS safe prior to calling this function.
  45. */
  46. function render_link($data, $values) {
  47. if (!empty($this->options['link_to_node']) && !empty($this->additional_fields['nid'])) {
  48. if ($data !== NULL && $data !== '') {
  49. $this->options['alter']['make_link'] = TRUE;
  50. $this->options['alter']['path'] = "node/" . $this->get_value($values, 'nid');
  51. if (isset($this->aliases['language'])) {
  52. $languages = language_list();
  53. $language = $this->get_value($values, 'language');
  54. if (isset($languages[$language])) {
  55. $this->options['alter']['language'] = $languages[$language];
  56. }
  57. else {
  58. unset($this->options['alter']['language']);
  59. }
  60. }
  61. }
  62. else {
  63. $this->options['alter']['make_link'] = FALSE;
  64. }
  65. }
  66. return $data;
  67. }
  68. function render($values) {
  69. $value = $this->get_value($values);
  70. return $this->render_link($this->sanitize_value($value), $values);
  71. }
  72. }