You are here

function views_matrix_plugin_style_matrix::render_fields in Views Matrix 7

Same name and namespace in other branches
  1. 8 includes/views_matrix_plugin_style_matrix.inc \views_matrix_plugin_style_matrix::render_fields()

Overrides views_plugin_style::render_fields().

Avoids rendering the column and row header fields more than once for each value to improve performance.

Overrides views_plugin_style::render_fields

File

includes/views_matrix_plugin_style_matrix.inc, line 361
Class definition for views matrix plugin.

Class

views_matrix_plugin_style_matrix
@class Views Plugin Class

Code

function render_fields($result) {
  if (!$this
    ->uses_fields()) {
    return NULL;
  }
  if (!isset($this->rendered_fields)) {
    $this->rendered_fields = array();

    // Get the column and row header field IDs and create an array of cached
    // render values for them.
    $x_key = $this->options['xconfig']['field'];
    $y_key = $this->options['yconfig']['field'];
    $cached_rendered_values = array();

    // If all fields have a field::access FALSE there might be no fields, so
    // there would be no reason to execute this code.
    if (!empty($this->view->field)) {
      foreach ($result as $count => $row) {
        $this->view->row_index = $count;

        /** @var views_handler_field $handler */
        foreach ($this->view->field as $id => $handler) {

          // In case this is one of the header fields, check and fill the
          // cached rendered values.
          if ($id === $x_key || $id === $y_key) {

            // Get the field value for this result, which serves as the cache
            // key for the rendered value.
            $value = $handler
              ->get_value($row);

            // In case of Field API fields, the value might be an array. To
            // still be able to use it as a cache key, we serialize it.
            if (!is_scalar($value)) {
              $value = serialize($value);
            }

            // If we have no cached rendered value yet, compute it.
            if (!isset($cached_rendered_values[$id][$value])) {
              $cached_rendered_values[$id][$value] = $handler
                ->theme($row);
            }
            $rendered = $cached_rendered_values[$id][$value];
          }
          else {
            $rendered = $handler
              ->theme($row);
          }
          $this->rendered_fields[$count][$id] = $rendered;
        }
        $this->row_tokens[$count] = $handler
          ->get_render_tokens(array());
      }
    }
    unset($this->view->row_index);
  }
  return $this->rendered_fields;
}