Skip to content
Snippets Groups Projects
Commit 1357f5d7 authored by ekes's avatar ekes
Browse files

For #200. Allow variation on result content on API version.

parent 59f64d9a
No related branches found
No related tags found
1 merge request!2Resolve "Add label/name all entities in the API results"
......@@ -12,6 +12,22 @@
*/
class RadarServicesEntityResourceController extends ServicesEntityResourceControllerClean {
protected $apiVersion = '';
/**
* Set API version.
*/
public function setApiVersion($version) {
$this->apiVersion = $version;
}
/**
* Get API version.
*/
public function getApiVersion() {
return $this->apiVersion;
}
/**
* Extends ServicesResourceControllerInterface::access().
*
......@@ -177,6 +193,7 @@ class RadarServicesEntityResourceController extends ServicesEntityResourceContro
// For referenced entities only return the URI.
if ($id = $property->getIdentifier()) {
$data[$name] = $this->get_resource_reference($property->type(), $id);
if (version_compare($this->getApiVersion(), '1.2', '>=')) {
// So in common with some of our other errors the entity won't load
// here as it's a UUID. Replacing the entity controller would
// probably do it? For now fix the issue at hand.
......@@ -192,6 +209,7 @@ class RadarServicesEntityResourceController extends ServicesEntityResourceContro
}
}
}
}
elseif ($property instanceof EntityValueWrapper) {
$data[$name] = $property->value();
}
......
......@@ -15,6 +15,7 @@ features[ctools][] = strongarm:strongarm:1
features[features_api][] = api:2
features[services_endpoint][] = api_1_0
features[services_endpoint][] = api_1_1
features[services_endpoint][] = api_1_2
features[user_permission][] = services_search_api search from any index
features[variable][] = services_entity_resource_class
features[variable][] = uuid_services_support_all_entity_types
......
......@@ -29,6 +29,25 @@ function radar_services_services_resources() {
'description' => t('Language code for language preference when translation available.'),
'source' => array('param' => 'language'),
);
// Only extending the searvices_search_api module implementation further.
// However this can't be done in the alter hook for whatever reason.
$default = services_search_api_services_resources();
$resources['search_api_1_2'] = $default['search_api'];
$resources['search_api_1_2']['retrieve']['callback'] = '_radar_services_search_api_resource_retrieve_1_2';
$resources['search_api_1_2']['retrieve']['file'] = array(
'type' => 'inc',
'module' => 'radar_services',
'name' => 'radar_services.resources',
);
$resources['search_api_1_2']['retrieve']['args'][] = array(
'name' => 'language',
'optional' => TRUE,
'default value' => LANGUAGE_NONE,
'type' => 'string',
'description' => t('Language code for language preference when translation available.'),
'source' => array('param' => 'language'),
);
return $resources;
}
......@@ -82,7 +101,8 @@ function radar_services_services_entity_resource_info() {
*/
function radar_services_services_request_preprocess_alter($controller, &$args, $options) {
if ($controller['callback'] != '_radar_services_search_api_resource_retrieve'
&& $controller['callback'] != '_radar_services_search_api_resource_retrieve_1_1') {
&& $controller['callback'] != '_radar_services_search_api_resource_retrieve_1_1'
&& $controller['callback'] != '_radar_services_search_api_resource_retrieve_1_2') {
return;
}
......
......@@ -4,6 +4,24 @@
* Extends services_search_api.resources.inc
*/
/**
* Callback function for the index service call v1.1.
*
* Adds labels to referenced entities.
*
* @see _services_search_api_resource_retrieve()
*/
function _radar_services_search_api_resource_retrieve_1_2($index, $keys = '', $filter, $sort, $limit, $offset, $fields, $language) {
$query = _radar_services_search_api_execute_query($index, $keys, $filter, $sort, $limit, $offset, $fields, $language);
$result = $query->execute();
$output = array(
'result' => _radar_services_search_api_process_results($result, $query, $fields, $language, '1.2'),
'count' => $result['result count'],
'facets' => _radar_services_search_api_process_facets($result, $query, $fields, $language),
);
return $output;
}
/**
* Callback function for the index service call v1.1.
*
......@@ -15,7 +33,7 @@ function _radar_services_search_api_resource_retrieve_1_1($index, $keys = '', $f
$query = _radar_services_search_api_execute_query($index, $keys, $filter, $sort, $limit, $offset, $fields, $language);
$result = $query->execute();
$output = array(
'result' => _radar_services_search_api_process_results($result, $query, $fields, $language),
'result' => _radar_services_search_api_process_results($result, $query, $fields, $language, '1.1'),
'count' => $result['result count'],
'facets' => _radar_services_search_api_process_facets($result, $query, $fields, $language),
);
......@@ -32,7 +50,7 @@ function _radar_services_search_api_resource_retrieve_1_1($index, $keys = '', $f
function _radar_services_search_api_resource_retrieve($index, $keys = '', $filter, $sort, $limit, $offset, $fields, $language) {
$query = _radar_services_search_api_execute_query($index, $keys, $filter, $sort, $limit, $offset, $fields, $language);
$result = $query->execute();
return _radar_services_search_api_process_results($result, $query, $fields, $language);
return _radar_services_search_api_process_results($result, $query, $fields, $language, '1.0');
}
/**
......@@ -75,7 +93,7 @@ function _radar_services_search_api_execute_query($index, $keys, $filter, $sort,
*
* @see _services_search_api_process_results()
*/
function _radar_services_search_api_process_results(&$result, $query, $fields, $language) {
function _radar_services_search_api_process_results(&$result, $query, $fields, $language, $version) {
if (empty($result['results']) || !is_array($result['results'])) {
return FALSE;
}
......@@ -90,7 +108,10 @@ function _radar_services_search_api_process_results(&$result, $query, $fields, $
drupal_alter('services_search_api_postprocess', $entities, $entity_type);
$resourceclass = variable_get('services_entity_resource_class', 'RadarServicesEntityResourceController');
$resource = new $resourceclass;
$resource = new $resourceclass();
if (method_exists($resource, 'setApiVersion')) {
$resource->setApiVersion($version);
}
$fields = implode(',', $fields);
foreach ($entities as $entity_id => $entity) {
try {
......
......@@ -210,5 +210,105 @@ function radar_services_default_services_endpoint() {
$endpoint->debug = 0;
$export['api_1_1'] = $endpoint;
$endpoint = new stdClass();
$endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */
$endpoint->api_version = 3;
$endpoint->name = 'api_1_2';
$endpoint->server = 'rest_server';
$endpoint->path = 'api/1.2';
$endpoint->authentication = array(
'services' => 'services',
);
$endpoint->server_settings = array();
$endpoint->resources = array(
'entity_file' => array(
'alias' => 'file',
'operations' => array(
'retrieve' => array(
'enabled' => '1',
),
'update' => array(
'enabled' => '1',
),
'create' => array(
'enabled' => '1',
),
),
),
'group_events' => array(
'operations' => array(
'index' => array(
'enabled' => '1',
),
),
),
'entity_location' => array(
'alias' => 'location',
'operations' => array(
'create' => array(
'enabled' => '1',
),
'retrieve' => array(
'enabled' => '1',
),
'update' => array(
'enabled' => '1',
),
),
),
'entity_node' => array(
'alias' => 'node',
'operations' => array(
'create' => array(
'enabled' => '1',
),
'retrieve' => array(
'enabled' => '1',
),
'update' => array(
'enabled' => '1',
),
),
),
'search_api_1_2' => array(
'alias' => 'search',
'operations' => array(
'retrieve' => array(
'enabled' => '1',
),
),
),
'entity_taxonomy_term' => array(
'alias' => 'taxonomy_term',
'operations' => array(
'create' => array(
'enabled' => '1',
),
'retrieve' => array(
'enabled' => '1',
),
'update' => array(
'enabled' => '1',
),
),
),
'entity_user' => array(
'alias' => 'user',
'actions' => array(
'login' => array(
'enabled' => '1',
),
'logout' => array(
'enabled' => '1',
),
'token' => array(
'enabled' => '1',
),
),
),
);
$endpoint->debug = 0;
$export['api_1_2'] = $endpoint;
return $export;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment