views_plugin_row.inc 4.5 KB

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