You are here

function search_autocomplete_get in Search Autocomplete 7.3

Gets JSON data from a View rendered in the JSON data document style.

This is useful for when working with a JSON view in code.

Parameters

$name: The name of the view.

$display_id: The display of the view to use.

$args: The arguments to pass to the view.

$raw: If TRUE, the JSON data is returned as a string. Otherwise, an object representation is returned.

Return value

The JSON data in the form of an object or a string or NULL otherwise.

File

./search_autocomplete.view_autocomplete.inc, line 239
Search Autocomplete Enables autocomplete functionality on search fields.

Code

function search_autocomplete_get($name, $display_id = 'default', $args = array(), $raw = FALSE) {
  $view = views_get_view($name);
  if (!is_object($view)) {
    return NULL;
  }
  $preview = $view
    ->preview($display_id, $args);
  $start_pos = strpos($preview, '{');
  $finish_pos = strrpos($preview, '}');
  $length = $finish_pos - $start_pos + 1;
  $json = trim(substr($preview, $start_pos, $length));
  if ($raw) {
    return $json;
  }
  return json_decode($json);
}