Skip to content

Commit caaae16

Browse files
qrazibrandonkelly
authored andcommitted
Add includes and excludes support to endpoint configuration (#41)
Resolves #40
1 parent 5082022 commit caaae16

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,46 @@ Possible values are:
201201
- `'jsonFeed'` – formats data based on [JSON Feed V1](https://jsonfeed.org/version/1) (see the [JSON Feed](#json-feed) example below).
202202
- A custom serializer instance.
203203

204+
#### `includes`
205+
206+
The [include names](http://fractal.thephpleague.com/transformers/#including-data) that should be included for the current request, if any.
207+
208+
```php
209+
'includes' => craft()->request->getQuery('include'),
210+
```
211+
212+
Note that this setting requires a custom transformer class that’s prepped to handle includes:
213+
214+
```php
215+
class MyTransformerClassName extends TransformerAbstract
216+
{
217+
protected $availableIncludes = ['author'];
218+
219+
public function includeAuthor(EntryModel $entry)
220+
{
221+
return $this->item($entry->author, function(UserModel($author) {
222+
return [
223+
'id' => $author->id,
224+
'name' => $author->name,
225+
];
226+
});
227+
}
228+
229+
// ...
230+
}
231+
```
232+
233+
#### `excludes`
234+
235+
236+
The [include names](http://fractal.thephpleague.com/transformers/#including-data) that should be excluded for the current request, which would otherwise have been included (e.g. if they were listed as a default include), if any.
237+
238+
```php
239+
'excludes' => 'author',
240+
```
241+
242+
Like [`includes`](#includes), this setting requires a custom transformer class.
243+
204244
#### `jsonOptions`
205245

206246
The value of the `$options` argument that will be passed to [`json_encode()`](http://php.net/manual/en/function.json-encode.php) when preparing the response. By default no options will be passed.

elementapi/controllers/ElementApiController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ public function actionGetElements($configFactory = null, array $config = null)
114114
}
115115
$fractal->setSerializer($serializer);
116116

117+
// Set the includes
118+
$includes = isset($config['includes']) ? $config['includes'] : [];
119+
$fractal->parseIncludes($includes);
120+
121+
// Set the excludes
122+
$excludes = isset($config['excludes']) ? $config['excludes'] : [];
123+
$fractal->parseExcludes($excludes);
124+
117125
// Define the transformer
118126
if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract)
119127
{

0 commit comments

Comments
 (0)