Commit c34a9354 authored by Dries's avatar Dries

Issue #1861838 by tim.plunkett, dawehner: Clean up the various Views field rendering methods.

parent 0d55d1a6
......@@ -71,7 +71,8 @@ function pre_render(&$values) {
function render($values) {
$links = array();
foreach ($this->options['fields'] as $field) {
if (empty($this->view->style_plugin->rendered_fields[$this->view->row_index][$field])) {
$rendered_field = $this->view->style_plugin->get_field($this->view->row_index, $field);
if (empty($rendered_field)) {
continue;
}
$title = $this->view->field[$field]->last_render_text;
......
......@@ -80,6 +80,16 @@ abstract class StylePluginBase extends PluginBase {
*/
protected $usesFields = FALSE;
/**
* Stores the rendered field values, keyed by the row index and field name.
*
* @see \Drupal\views\Plugin\views\style\StylePluginBase::render_fields()
* @see \Drupal\views\Plugin\views\style\StylePluginBase::get_field()
*
* @var array|null
*/
protected $rendered_fields;
/**
* Overrides \Drupal\views\Plugin\views\PluginBase::init().
*
......@@ -587,12 +597,12 @@ function render_grouping($records, $groupings = array(), $group_rendered = NULL)
}
/**
* Render all of the fields for a given style and store them on the object.
* Renders all of the fields for a given style and store them on the object.
*
* @param $result
* @param array $result
* The result array from $view->result
*/
function render_fields($result) {
protected function render_fields(array $result) {
if (!$this->usesFields()) {
return;
}
......@@ -616,19 +626,20 @@ function render_fields($result) {
}
unset($this->view->row_index);
}
return $this->rendered_fields;
}
/**
* Get a rendered field.
* Gets a rendered field.
*
* @param $index
* @param int $index
* The index count of the row.
* @param $field
* The id of the field.
* @param string $field
* The ID of the field.
*
* @return string|null
* The output of the field, or NULL if it was empty.
*/
function get_field($index, $field) {
public function get_field($index, $field) {
if (!isset($this->rendered_fields)) {
$this->render_fields($this->view->result);
}
......
......@@ -54,11 +54,11 @@ function testSimple() {
));
$view->preview();
$counter = $view->style_plugin->rendered_fields[0]['counter'];
$counter = $view->style_plugin->get_field(0, 'counter');
$this->assertEqual($counter, 1, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 1, '@counter' => $counter)));
$counter = $view->style_plugin->rendered_fields[1]['counter'];
$counter = $view->style_plugin->get_field(1, 'counter');
$this->assertEqual($counter, 2, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 2, '@counter' => $counter)));
$counter = $view->style_plugin->rendered_fields[2]['counter'];
$counter = $view->style_plugin->get_field(2, 'counter');
$this->assertEqual($counter, 3, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => 3, '@counter' => $counter)));
$view->destroy();
......@@ -81,13 +81,13 @@ function testSimple() {
));
$view->preview();
$counter = $view->style_plugin->rendered_fields[0]['counter'];
$counter = $view->style_plugin->get_field(0, 'counter');
$expected_number = 0 + $rand_start;
$this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
$counter = $view->style_plugin->rendered_fields[1]['counter'];
$counter = $view->style_plugin->get_field(1, 'counter');
$expected_number = 1 + $rand_start;
$this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
$counter = $view->style_plugin->rendered_fields[2]['counter'];
$counter = $view->style_plugin->get_field(2, 'counter');
$expected_number = 2 + $rand_start;
$this->assertEqual($counter, $expected_number, format_string('Make sure the expected number (@expected) patches with the rendered number (@counter)', array('@expected' => $expected_number, '@counter' => $counter)));
}
......
......@@ -1335,27 +1335,6 @@ public function render($display_id = NULL) {
return $this->display_handler->output;
}
/**
* Render a specific field via the field ID and the row #
*
* Note: You might want to use views_plugin_style::render_fields as it
* caches the output for you.
*
* @param string $field
* The id of the field to be rendered.
*
* @param int $row
* The row number in the $view->result which is used for the rendering.
*
* @return string
* The rendered output of the field.
*/
public function renderField($field, $row) {
if (isset($this->field[$field]) && isset($this->result[$row])) {
return $this->field[$field]->advanced_render($this->result[$row]);
}
}
/**
* Execute the given display, with the given arguments.
* To be called externally by whatever mechanism invokes the view,
......
......@@ -480,10 +480,6 @@ function template_preprocess_views_view_table(&$vars) {
$query += $view->exposed_raw_input;
}
// Fields must be rendered in order as of Views 2.3, so we will pre-render
// everything.
$renders = $handler->render_fields($result);
foreach ($columns as $field => $column) {
// Create a second variable so we can easily find what fields we have and what the
// CSS classes should be.
......@@ -575,7 +571,7 @@ function template_preprocess_views_view_table(&$vars) {
}
if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
$field_output = $renders[$num][$field];
$field_output = $handler->get_field($num, $field);
$element_type = $fields[$field]->element_type(TRUE, TRUE);
if ($element_type) {
$field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment