Skip to content

Commit

Permalink
Add defaultSortInfo() method to define default sorts.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaeltraviss authored and amitaibu committed Dec 3, 2014
1 parent 9a8c0fc commit 7a80408
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ array(
);
```

You can also define default sort fields in your plugin, by overriding
`defaultSortInfo()` in your class definition.

This method should return an associative array, with each element having a key
that matches a field from `publicFieldsInfo()`, and a value of either 'ASC' or 'DESC'.

This default sort will be ignored if the request URL contains a sort query.

```php
class MyPlugin extends \RestfulEntityBaseTaxonomyTerm {
/**
* Overrides \RestfulEntityBase::defaultSortInfo().
*/
public function defaultSortInfo() {
// Sort by 'id' in descending order.
return array('id' => 'DESC');
}
}

```

### Filter
RESTful allows filtering of a list.

Expand Down Expand Up @@ -580,7 +601,7 @@ Since the global event is not tied to any resource the limit and period is speci
all roles.
- `restful_global_rate_period`: The period string compatible with
\DateInterval.
## Documenting your API
It is of most importance to document your API, this is why the RESTful module
provides a way to comprehensively document your resources and endpoints. This
Expand Down
2 changes: 1 addition & 1 deletion plugins/restful/RestfulBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function setRateLimitManager($rateLimitManager) {

/**
* Getter for rateLimitManager.
*
* @return \RestfulRateLimitManager
*/
public function getRateLimitManager() {
Expand Down
21 changes: 17 additions & 4 deletions plugins/restful/RestfulDataProviderDbQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ public function __construct(array $plugin, \RestfulAuthenticationManager $auth_m
$this->primary = empty($plugin['primary']) ? NULL : $this->primary = $plugin['primary'];
}

/**
* Defines default sort columns if none are provided via the request URL.
*
* @return array
* Array keyed by the database column name, and the order ('ASC' or 'DESC') as value.
*/
public function defaultSortInfo() {
$sorts = array();
if (!empty($this->getPublicFields[$this->getIdColumn()])) {
$sorts[$this->getIdColumn()] = 'ASC';
}
return $sorts;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -121,12 +135,11 @@ public function getQueryForList() {
*/
protected function queryForListSort(\SelectQuery $query) {
$public_fields = $this->getPublicFields();

// Get the sorting options from the request object.
$sorts = $this->parseRequestForListSort();
if (empty($sorts)) {
$query->orderBy($this->getIdColumn(), 'ASC');
return;
}

$sorts = $sorts ? $sorts : $this->defaultSortInfo();

foreach ($sorts as $sort => $direction) {
$query->orderBy($public_fields[$sort]['property'], $direction);
Expand Down
22 changes: 14 additions & 8 deletions plugins/restful/RestfulDataProviderEFQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function __construct(array $plugin, \RestfulAuthenticationManager $auth_m
$this->bundle = $plugin['bundle'];
}

/**
* Defines default sort fields if none are provided via the request URL.
*
* @return array
* Array keyed by the public field name, and the order ('ASC' or 'DESC') as value.
*/
public function defaultSortInfo() {
return array('id' => 'ASC');
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -94,15 +104,11 @@ public function getQueryForList() {
*/
protected function queryForListSort(\EntityFieldQuery $query) {
$public_fields = $this->getPublicFields();

// Get the sorting options from the request object.
$sorts = $this->parseRequestForListSort();
if (empty($sorts)) {
// Some endpoints like 'token_auth' don't have an id public field. In that
// case, skip the default sorting.
if (!empty($public_fields['id'])) {
// Sort by default using the entity ID.
$sorts['id'] = 'ASC';
}
}

$sorts = $sorts ? $sorts : $this->defaultSortInfo();

foreach ($sorts as $sort => $direction) {
// Determine if sorting is by field or property.
Expand Down
47 changes: 47 additions & 0 deletions tests/RestfulListTestCase.test
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,53 @@ class RestfulListTestCase extends RestfulCurlBaseTestCase {
);
$this->assertEqual($result, $expected_result, 'Sort by ID and by label.');

// Test default sorting from plugin definition; by label, then by reverse id.
$handler = restful_get_restful_handler('test_articles', 1, 1);
unset($request['sort']);
$result = $handler->get('', $request);
$expected_result = array(
array(
'id' => $node->nid,
'label' => 'abc',
),
array(
'id' => $nodes['abc'],
'label' => 'abc',
),
array(
'id' => $nodes['efg'],
'label' => 'efg',
),
array(
'id' => $nodes['xyz'],
'label' => 'xyz',
),
);
$this->assertEqual($result, $expected_result, 'Default sort by ID and by label.');

// Test that the default sort can be overridden.
$request['sort'] = 'id';
$result = $handler->get('', $request);
$expected_result = array(
array(
'id' => $nodes['abc'],
'label' => 'abc',
),
array(
'id' => $nodes['xyz'],
'label' => 'xyz',
),
array(
'id' => $nodes['efg'],
'label' => 'efg',
),
array(
'id' => $node->nid,
'label' => 'abc',
),
);
$this->assertEqual($result, $expected_result, 'Sort by ID, overriding default sort.');

// Illegal sort property.
$request['sort'] = 'wrong_key';
try {
Expand Down
3 changes: 1 addition & 2 deletions tests/RestfulRenderCacheTestCase.test
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class RestfulRenderCacheTestCase extends DrupalWebTestCase {
$settings = array('type' => 'article');
$account = $this->drupalCreateUser();


$num_articles = 3;
for ($index = 0; $index < $num_articles; $index++) {
$settings['title'] = $this->randomName();
$settings['title'] = 'Node title ' . $index;
$node = $this->drupalCreateNode($settings);
$nodes[$node->nid] = $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@
* Contains RestfulTestArticlesResource__1_1.
*/

class RestfulTestArticlesResource__1_1 extends RestfulExampleArticlesResource {}
class RestfulTestArticlesResource__1_1 extends RestfulExampleArticlesResource {
/**
* Overrides RestfulDataProviderEFQ::defaultSortInfo().
*/
public function defaultSortInfo() {
return array(
'label' => 'ASC',
'id' => 'DESC'
);
}
}

0 comments on commit 7a80408

Please sign in to comment.