From 1357f5d7dd27af4aab0b7b7d3243cce0ebdeea7a Mon Sep 17 00:00:00 2001 From: ekes <ekes@iskra.net> Date: Sun, 1 Apr 2018 19:59:56 +0200 Subject: [PATCH] For #200. Allow variation on result content on API version. --- .../services_entity/resource_controller.inc | 52 ++++++--- .../radar/radar_services/radar_services.info | 1 + .../radar_services/radar_services.module | 22 +++- .../radar_services.resources.inc | 29 ++++- .../radar_services.services.inc | 100 ++++++++++++++++++ 5 files changed, 182 insertions(+), 22 deletions(-) diff --git a/modules/radar/radar_services/plugins/services_entity/resource_controller.inc b/modules/radar/radar_services/plugins/services_entity/resource_controller.inc index 21279369..2c00f617 100644 --- a/modules/radar/radar_services/plugins/services_entity/resource_controller.inc +++ b/modules/radar/radar_services/plugins/services_entity/resource_controller.inc @@ -12,11 +12,27 @@ */ class RadarServicesEntityResourceController extends ServicesEntityResourceControllerClean { - /** - * Extends ServicesResourceControllerInterface::access(). - * - * Default services access does not load by UUID. - */ + protected $apiVersion = ''; + + /** + * Set API version. + */ + public function setApiVersion($version) { + $this->apiVersion = $version; + } + + /** + * Get API version. + */ + public function getApiVersion() { + return $this->apiVersion; + } + + /** + * Extends ServicesResourceControllerInterface::access(). + * + * Default services access does not load by UUID. + */ public function access($op, $args) { if ($op == 'index') { // Access is handled per-entity by index(). @@ -177,18 +193,20 @@ class RadarServicesEntityResourceController extends ServicesEntityResourceContro // For referenced entities only return the URI. if ($id = $property->getIdentifier()) { $data[$name] = $this->get_resource_reference($property->type(), $id); - // 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. - $entity_info = entity_get_info($property->type()); - $entities = entity_uuid_load($property->type(), [$id]); - $entity_label = empty($entity_info['entity keys']['label']) ? 'label' : $entity_info['entity keys']['label']; - if (!empty($entities)) { - $entity = reset($entities); - $data[$name][$entity_label] = entity_label($property->type(), $entity); - } - else { - $data[$name][$entity_label] = ''; + 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. + $entity_info = entity_get_info($property->type()); + $entities = entity_uuid_load($property->type(), [$id]); + $entity_label = empty($entity_info['entity keys']['label']) ? 'label' : $entity_info['entity keys']['label']; + if (!empty($entities)) { + $entity = reset($entities); + $data[$name][$entity_label] = entity_label($property->type(), $entity); + } + else { + $data[$name][$entity_label] = ''; + } } } } diff --git a/modules/radar/radar_services/radar_services.info b/modules/radar/radar_services/radar_services.info index 6b11ae18..28f50337 100644 --- a/modules/radar/radar_services/radar_services.info +++ b/modules/radar/radar_services/radar_services.info @@ -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 diff --git a/modules/radar/radar_services/radar_services.module b/modules/radar/radar_services/radar_services.module index 7bfa4b34..62437091 100644 --- a/modules/radar/radar_services/radar_services.module +++ b/modules/radar/radar_services/radar_services.module @@ -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; } diff --git a/modules/radar/radar_services/radar_services.resources.inc b/modules/radar/radar_services/radar_services.resources.inc index d7091d10..193154e7 100644 --- a/modules/radar/radar_services/radar_services.resources.inc +++ b/modules/radar/radar_services/radar_services.resources.inc @@ -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 { diff --git a/modules/radar/radar_services/radar_services.services.inc b/modules/radar/radar_services/radar_services.services.inc index a1fc3108..466d05b2 100644 --- a/modules/radar/radar_services/radar_services.services.inc +++ b/modules/radar/radar_services/radar_services.services.inc @@ -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; } -- GitLab