You are here

function services_views_retrieve in Services Views 7

Same name and namespace in other branches
  1. 6 services_views.resource.inc \services_views_retrieve()

Callback for retrieving views resources.

Parameters

string $view_name: The views name.

string $display_id: (optional) The views display name.

array $args: (optional) A list of arguments to pass to the view.

int $offset: (optional) An offset integer for paging.

int $limit: (optional) A limit integer for paging.

string $return_type: (optional) Whether to return the raw data results (FALSE), the entire views object ('view') or themed results ('theme').

array $filters: (optional) A list of exposed filters to pass to the view.

Return value

array The views return.

1 string reference to 'services_views_retrieve'
services_views_services_resources in ./services_views.module
Implements hook_services_resources().

File

./services_views.resource.inc, line 30
Callbacks for services module resource hooks.

Code

function services_views_retrieve($view_name, $display_id = 'default', array $args = array(), $offset = 0, $limit = -1, $return_type = FALSE, array $filters = array()) {
  $result = array();
  $view = views_get_view($view_name);

  // Make sure we aren't working with a backwards compatible display_id.
  if (empty($view->display[$display_id]) || $view->display[$display_id]->display_plugin != 'services') {
    $display_id = 'services_clone_' . $display_id;
  }

  // Put all arguments and filters and then execute.
  $view
    ->set_arguments($args, FALSE);
  $view
    ->set_exposed_input($filters);
  $view
    ->set_offset($offset);

  // Support for Views 2.
  if (method_exists($view, 'set_use_pager')) {

    // If offset is set we can't have a user pager.
    if (empty($offset)) {
      $view
        ->set_use_pager(TRUE);
      if ($limit >= 0) {
        $view
          ->set_items_per_page($limit);
      }
    }
    else {

      // Disable the user pager.
      $view
        ->set_use_pager(FALSE);
    }
  }
  elseif ($limit >= 0) {
    $view
      ->set_items_per_page($limit);
  }

  // If we display values and not themed view output.
  if (empty($return_type)) {
    $view
      ->set_display($display_id);
    $view
      ->pre_execute();
    $view
      ->execute();
    $result = $view->result;
    $row_plugin = $view->display_handler
      ->get_option('row_plugin');

    // If row plugin is node, then we should load each node.
    if ($row_plugin == 'node') {
      $nodes = array();
      foreach ($view->result as $row) {
        $nodes[] = node_load($row->nid);
      }
      $result = $nodes;
    }
    elseif ($row_plugin == 'fields') {
      $items = array();

      // Pre-render and field values.
      foreach ($view->field as $field) {
        $field
          ->pre_render($view->result);
      }
      foreach ($view->result as $row_index => $row) {
        $item = new stdClass();
        $excluded_fields = array();
        foreach ($view->field as $field_id => $field) {
          if (!empty($field->options['label'])) {
            $field_label = strtolower($field->options['label']);
          }
          elseif ($field->field_alias) {
            $field_label = $field->field_alias;
          }
          else {
            $field_label = $field_id;
          }
          $field->view->row_index = $row_index;
          $item->{$field_label} = $field
            ->theme($row);
          if (!empty($field->options['exclude'])) {
            $excluded_fields[] = $field_label;
          }
        }
        foreach ($excluded_fields as $excluded_field) {
          unset($item->{$excluded_field});
        }
        $items[] = $item;
      }
      if ($items) {
        $result = $items;
      }
    }
  }
  elseif ($return_type == 'view') {
    $view
      ->set_display($display_id);
    $view
      ->execute();
    return $view;
  }
  else {

    // We want to keep the result an array.
    $result[] = $view
      ->preview($display_id);
  }
  return $result;
}