views_plugin_row.inc

  1. 3.x plugins/views_plugin_row.inc
  2. 2.x plugins/views_plugin_row.inc

Contains the base row style plugin.

File

plugins/views_plugin_row.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the base row style plugin.
  5. */
  6. /**
  7. * @defgroup views_row_plugins Views' row plugins
  8. * @{
  9. *
  10. * Row plugins control how Views outputs an individual record. They are
  11. * tightly coupled to style plugins, in that a style plugin is what calls
  12. * the row plugin.
  13. *
  14. * @see hook_views_plugins
  15. */
  16. /**
  17. * Default plugin to view a single row of a table. This is really just a wrapper around
  18. * a theme function.
  19. *
  20. * @ingroup views_row_plugins
  21. */
  22. class views_plugin_row extends views_plugin {
  23. /**
  24. * Initialize the row plugin.
  25. */
  26. function init(&$view, &$display, $options = NULL) {
  27. $this->view = &$view;
  28. $this->display = &$display;
  29. // Overlay incoming options on top of defaults
  30. $this->unpack_options($this->options, isset($options) ? $options : $display->handler->get_option('row_options'));
  31. }
  32. function uses_fields() {
  33. return !empty($this->definition['uses fields']);
  34. }
  35. function option_definition() {
  36. $options = parent::option_definition();
  37. if (isset($this->base_table)) {
  38. $options['relationship'] = array('default' => 'none');
  39. }
  40. return $options;
  41. }
  42. /**
  43. * Provide a form for setting options.
  44. */
  45. function options_form(&$form, &$form_state) {
  46. if (isset($this->base_table)) {
  47. $view = &$form_state['view'];
  48. // A whole bunch of code to figure out what relationships are valid for
  49. // this item.
  50. $relationships = $view->display_handler->get_option('relationships');
  51. $relationship_options = array();
  52. foreach ($relationships as $relationship) {
  53. $relationship_handler = views_get_handler($relationship['table'], $relationship['field'], 'relationship');
  54. // If this relationship is valid for this type, add it to the list.
  55. $data = views_fetch_data($relationship['table']);
  56. $base = $data[$relationship['field']]['relationship']['base'];
  57. if ($base == $this->base_table) {
  58. $relationship_handler->init($view, $relationship);
  59. $relationship_options[$relationship['id']] = $relationship_handler->label();
  60. }
  61. }
  62. if (!empty($relationship_options)) {
  63. $relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options);
  64. $rel = empty($this->options['relationship']) ? 'none' : $this->options['relationship'];
  65. if (empty($relationship_options[$rel])) {
  66. // Pick the first relationship.
  67. $rel = key($relationship_options);
  68. }
  69. $form['relationship'] = array(
  70. '#type' => 'select',
  71. '#title' => t('Relationship'),
  72. '#options' => $relationship_options,
  73. '#default_value' => $rel,
  74. );
  75. }
  76. else {
  77. $form['relationship'] = array(
  78. '#type' => 'value',
  79. '#value' => 'none',
  80. );
  81. }
  82. }
  83. }
  84. /**
  85. * Validate the options form.
  86. */
  87. function options_validate($form, &$form_state) { }
  88. /**
  89. * Perform any necessary changes to the form values prior to storage.
  90. * There is no need for this function to actually store the data.
  91. */
  92. function options_submit($form, &$form_state) { }
  93. function query() {
  94. if (isset($this->base_table) && isset($this->options['relationship']) && isset($this->view->relationship[$this->options['relationship']])) {
  95. $relationship = $this->view->relationship[$this->options['relationship']];
  96. $this->field_alias = $this->view->query->add_field($relationship->alias, $this->base_field);
  97. }
  98. else {
  99. $this->field_alias = $this->view->base_field;
  100. }
  101. }
  102. /**
  103. * Allow the style to do stuff before each row is rendered.
  104. *
  105. * @param $result
  106. * The full array of results from the query.
  107. */
  108. function pre_render($result) { }
  109. /**
  110. * Render a row object. This usually passes through to a theme template
  111. * of some form, but not always.
  112. */
  113. function render($row) {
  114. return theme($this->theme_functions(), $this->view, $this->options, $row, $this->field_alias);
  115. }
  116. }
  117. /**
  118. * @}
  119. */