You are here

public function state_flow_entity_handler_field_state_flow_states_history_list::pre_render in State Machine 7.3

Override pre_render() reduce number of queries.

Loads all relevant state_flow_history_entities in one query. They get rendered in chunks in the render() method.

Overrides views_handler_field::pre_render

File

modules/state_flow_entity/includes/views/state_flow_entity_handler_field_state_flow_states_history_list.inc, line 30
A field handler to take a record form {state_flow_states} and load all of the relevant history entities.

Class

state_flow_entity_handler_field_state_flow_states_history_list
@file A field handler to take a record form {state_flow_states} and load all of the relevant history entities.

Code

public function pre_render(&$values) {

  // Call the parent pre_render method on general principle.
  parent::pre_render($values);

  // An array of revision ids. Corresponding state_flow_history_entities will
  // be loaded based on these values.
  // Change to $relevant_revision_ids
  $relevant_revision_ids = array();
  foreach ($values as $result_set) {
    $revision_id = $this
      ->get_value($result_set, 'revision_id');
    $entity_type = $this
      ->get_value($result_set, 'entity_type');
    $relevant_revision_ids[$entity_type][$revision_id] = $revision_id;
  }

  // Use EntityFieldQuery to get the hids of the relevant
  // state_flow_history_entities based on the parent entity_type and
  // revision_ids.
  foreach ($relevant_revision_ids as $parent_entity_type => $keys) {
    $efq = new EntityFieldQuery();
    $efq
      ->entityCondition('entity_type', 'state_flow_history_entity');
    $efq
      ->propertyCondition('entity_type', $parent_entity_type);
    $efq
      ->propertyCondition('revision_id', $keys, 'IN');
    $efq
      ->propertyOrderBy('hid', 'DESC');

    // Execute query and collect results.
    $result = $efq
      ->execute();

    // An array for all the relevant state_flow_history_entity hids.
    $hids = array();
    foreach ($result['state_flow_history_entity'] as $result_object) {
      $hids[$result_object->hid] = $result_object->hid;
    }

    // Load all the relevant history entities.
    $state_flow_history_entities = entity_load('state_flow_history_entity', $hids);

    // Results get rendered in groups by parent revision_id so group the
    // state_flow_history_entities accordingly.
    $state_flow_history_entities_by_revision_id = array();
    foreach ($state_flow_history_entities as $hid => $state_flow_history_entity) {
      $state_flow_history_entities_by_revision_id[$state_flow_history_entity->revision_id][$hid] = $state_flow_history_entity;
    }

    // Store the array on this object so it is available in the render method.
    $this->state_flow_history_entities_by_revision_id[$parent_entity_type] = $state_flow_history_entities_by_revision_id;
  }
}