diff --git a/README.md b/README.md index 160cb66..68bb66d 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ The query string param name that should be used to identify which page is being Note that it cannot be set to `'p'` because that’s the parameter Craft uses to check the requested path. -### `resourceKey` +#### `resourceKey` The key that the elements should be nested under in the response data. By default this will be `'data'`. @@ -201,6 +201,46 @@ Possible values are: - `'jsonFeed'` – formats data based on [JSON Feed V1](https://jsonfeed.org/version/1) (see the [JSON Feed](#json-feed) example below). - A custom serializer instance. +#### `includes` + +The [include names](http://fractal.thephpleague.com/transformers/#including-data) that should be included for the current request, if any. + +```php +'includes' => craft()->request->getQuery('include'), +``` + +Note that this setting requires a custom transformer class that’s prepped to handle includes: + +```php +class MyTransformerClassName extends TransformerAbstract +{ + protected $availableIncludes = ['author']; + + public function includeAuthor(EntryModel $entry) + { + return $this->item($entry->author, function(UserModel($author) { + return [ + 'id' => $author->id, + 'name' => $author->name, + ]; + }); + } + + // ... +} +``` + +#### `excludes` + + +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. + +```php +'excludes' => 'author', +``` + +Like [`includes`](#includes), this setting requires a custom transformer class. + #### `jsonOptions` 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. diff --git a/elementapi/controllers/ElementApiController.php b/elementapi/controllers/ElementApiController.php index 7d166a1..c0a6fa9 100644 --- a/elementapi/controllers/ElementApiController.php +++ b/elementapi/controllers/ElementApiController.php @@ -114,6 +114,14 @@ public function actionGetElements($configFactory = null, array $config = null) } $fractal->setSerializer($serializer); + // Set the includes + $includes = isset($config['includes']) ? $config['includes'] : []; + $fractal->parseIncludes($includes); + + // Set the excludes + $excludes = isset($config['excludes']) ? $config['excludes'] : []; + $fractal->parseExcludes($excludes); + // Define the transformer if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) {