You are here

function message_views_pre_render in Message 6

Implementation of hook_views_pre_render().

In order to reduce the database hits, we take advantage of the fact that we know the message instances IDs before they are rendered. We load them all, and they are saved in a static cache. So later when they are rendered, the message instances are already cached.

See also

message_instance_load_multiple().

message_show_message().

File

./message.module, line 93
API functions to manipulate messages.

Code

function message_views_pre_render(&$view) {

  // Define if this is a view that involves message instance, and thus should be
  // processed.
  $process = FALSE;
  $ids = array();

  // Look for a field that belong to the {message_instance} table.
  foreach ($view->field as $field) {
    if ($field->definition['handler'] == 'message_handler_field_message_render') {
      $field_alias = $field->field_alias;
      $process = TRUE;
      break;
    }
  }
  if ($process && !empty($view->result)) {
    foreach ($view->result as &$row) {
      $ids[] = $row->{$field_alias};
    }
  }

  // Load the message instances, so they will be statically cached.
  if ($ids && ($message_instances = message_instance_load_multiple($ids))) {
    $remove = array();
    foreach ($message_instances as $key => $value) {

      // Check if messages should be hidden.
      if (!empty($value->hide)) {
        $remove[$key] = $key;
      }
    }

    // Remove hidden results from the view.
    foreach ($view->result as $key => $value) {

      // TODO: Get the properly the alias of iid.
      if (in_array($value->iid, $remove)) {
        unset($view->result[$key]);

        // Unset the key from the remove list, to make next checks a bit faster.
        unset($remove[$key]);
      }
      if (empty($remove)) {
        break;
      }
    }
  }
}