You are here

protected function views_aggregator_plugin_style_table::render_from_raw in Views Aggregator Plus 7

Render a field.module field from a raw value.

The field will be rendered with appropriate CSS classes, without label.

NB: This is messy code. The lengths we have to go through for this are ridiculous. Patches welcome!

The way it currently works is to set the desired $raw_value on the associated entity and then render it via:

  • set_items(), when a row_num is provided to write the value to
  • field_view_field(), otherwise

set_items() internally also calls field_view_field()

Parameters

object $field_handler: The views_handler_field_field object belonging to the View result field.

int $row_num: The view result row number to change. Pass NULL to simply render $raw_value outside the context of a View, without affecting any rows.

mixed $raw_value: Compound or simple value. If NULL the row value of the field is re-rendered using its current (raw) value.

Return value

string The rendered value or FALSE, if the type of field is not supported.

1 call to views_aggregator_plugin_style_table::render_from_raw()
views_aggregator_plugin_style_table::render_new_value in views/views_aggregator_plugin_style_table.inc
Returns the rendered value for a new (raw) value of a table cell.

File

views/views_aggregator_plugin_style_table.inc, line 813
views_aggregator_plugin_style_table.inc

Class

views_aggregator_plugin_style_table
Style plugin to render each item as a row in a table.

Code

protected function render_from_raw($field_handler, $row_num = NULL, $raw_value = NULL) {
  $field_name = $real_field_name = $field_handler->options['id'];
  $field_alias = $field_handler->field_alias;
  $row = isset($row_num) ? $field_handler->view->result[$row_num] : reset($field_handler->view->result);
  $affect_view = isset($row_num);

  // _field_data[] contains the entities we'll be rendering from/to.
  if (!$row || empty($row->_field_data[$field_alias])) {

    // This happens for ViewsPHP fields and for Math Expressions.
    return !$row || $affect_view ? FALSE : (isset($raw_value) ? $raw_value : '?');
  }

  // Note that when a 2nd copy of a field is used in the View, e.g.
  // field_price_1, we refer back to the base field name, i.e. field_price.
  $last_underscore = strrpos($real_field_name, '_');
  if ((int) drupal_substr($real_field_name, $last_underscore + 1)) {
    $field_name = drupal_substr($real_field_name, 0, $last_underscore);
  }
  $_field_data = $row->_field_data[$field_alias];
  if ($this
    ->has_no_suitable_renderer($_field_data, $field_name)) {

    // E.g. when the $field_handler refers to a node property (rather than
    // a field) that does not have a renderer.
    return $affect_view ? $_field_data['entity']->{$field_name} = $raw_value : $raw_value;
  }

  // Clone entity if we don't want to affect the current View results or if we
  // have multiple displays.
  $entity = $affect_view ? $_field_data['entity'] : clone $_field_data['entity'];
  $entity_type = $_field_data['entity_type'];
  $lang = is_a($field_handler, 'views_handler_field_field') ? $field_handler
    ->field_language($entity_type, $entity) : $entity->language;
  if (isset($raw_value)) {

    // Only supporting values of 1 item, at index 0.
    if (is_array($raw_value)) {
      $entity->{$field_name}[$lang][0] = $raw_value;
    }
    elseif (isset($entity->{$field_name})) {
      if (empty($entity->{$field_name})) {
        $current_value = NULL;
        $key = 'value';
      }
      else {
        $current_value = reset($entity->{$field_name}[$lang][0]);
        $key = key($entity->{$field_name}[$lang][0]);

        // Cannot override 'tid' with non-numeric value. But like 'value',
        // 'tid' may be set in case of min, max, most frequent etc.
        // 'amount' is to allow the setting of Drupal Commerce prices.
        if ($affect_view && $key != 'value' && $key != 'amount' && !($key == 'tid' && is_numeric($raw_value))) {
          return FALSE;
        }
      }
      $entity->{$field_name}[$lang][0][$key] = $raw_value;
    }
    if ($affect_view) {

      // Next employ set_items() to re-render the $entity updated above.
      // set_items() calls field_view_field() to render the value, applying
      // rounding etc.
      // It returns an array with raw and rendered components.
      $raw_plus_rendered = $field_handler
        ->set_items($row, $row_num);

      // Now set the current value back on the entity in case we have multiple
      // displays, all drawing from the same entity.
      if (isset($key)) {
        $entity->{$field_name}[$lang][0][$key] = $current_value;
      }

      // The final step is to theme the rendered values. This includes
      // token replacement and template theming.
      // theme() calls $field_handler->advanced_render($row);
      $row->{'field_' . $real_field_name} = $raw_plus_rendered;
      return $field_handler
        ->theme($row);
    }
  }

  // If we can't affect the View result or $raw_value isn't set, we use the
  // Field API. The Field label is not rendered.
  $display = array(
    'type' => $field_handler->options['type'],
    'settings' => $field_handler->options['settings'],
    'label' => 'hidden',
  );
  $render_array = field_view_field($entity_type, $entity, $field_name, $display, $lang);
  return drupal_render($render_array);
}