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