14
votes
Accepted
How do you use the Element API plugin to output Matrix blocks for an entry?
Totally possible. Just loop through the blocks and add whatever data you want from them to the response array:
'transformer' => function(EntryModel $entry) {
// Create an array of all the "...
9
votes
Accepted
Calling Related Entries using the Element API
You’d do this with a brand new element query using the relatedTo param.
Craft 2:
// This is the PHP equivalent of typing `craft.entries()` in a template:
$criteria = craft()->elements->...
8
votes
Accepted
How do I cache with the ElementAPI plugin?
The ElementAPI plugin itself doesn't seem to offer a caching solution, but there is a fork from a guy which has implemented a caching option cache and cacheTime:
https://github.com/theolampert/...
7
votes
Accepted
Element API - 'String' to 'kebab-case'
You can actually use the StringHelper::toKebabCase function for this:
$string = 'Hello there';
$result = StringHelper::toKebabCase($string);
// => 'hello-there'
7
votes
Accepted
Formatting postDate using the ElementAPI plugin
$entry->postDate will give you the postDate property of the EntryModel, which itself is a DateTime object.
The JSON transformer will do its best to convert that object to JSON by returning its ...
7
votes
Is there a way to split the element-api.php file?
Sure – the element-api.php config file is just plain ol' PHP, and the functions that make up the individual endpoints (e.g. 'news.json' => function () { ...) doesn't necessarily have to be declared ...
6
votes
Accepted
Element API plugin - Return message when no results are found
Currently, Element API is hard coded to throw a 404 exception for single element endpoints, if the element criteria returns no results. There's no way to prevent this exception, add "fallback" ...
6
votes
Accepted
Why is the summary and body returning an empty object using the Element API?
Try this:
...
'transformer' => function (EntryModel $entry) {
return [
'title' => $entry->title,
'url' => $entry->url,
'summary' => (string) $entry->...
5
votes
Accepted
Element API - remove response limit
You have to set the criteria model's limit parameter to null and disable pagination.
'criteria' => [
'group' => 'fullMembership',
'limit' => null,
],
'paginate' => false,
5
votes
Accepted
How can I use another plugin in PHP?
Your approach is technically correct – the problem is that craft()->imager->transformImage() requires two additional parameters; $transformDefaults and $configOverrides.
You can read more about ...
5
votes
How can I add custom headers to an ElementAPI response?
In case anyone finds this in a Google search, this is how you'd do it in Craft 3, if, say, you wanted to add an Expires header:
$response = Craft::$app->getResponse();
$response->headers->...
5
votes
Accepted
How do I use the Element API to search via entry title?
Following is an example of using the search feature in a GET request with parameter 'search'.
return [
'elementType' => 'Entry',
'criteria' => ['search' => (craft()->request->...
5
votes
Accepted
Get Assets in JSON with ElementAPI
Something like the following should work:
// elementapi.php
return [
'endpoints' => [
'assets.json' => [
'paginate' => false,
'elementType' => ...
5
votes
Accepted
Get parent entry from a section using the Element API
A couple of issues:
First, the EntryModel::getParent() method returns an EntryModel, not an ElementCriteriaModel, which means that calling getParent()->first() will throw an exception. In other ...
5
votes
How to use the Element API plugin only when logged in?
If you want to restrict the whole API, you can add some code to the top of your /config/elementapi.php file (right below namespace Craft;) that checks for a logged in user, and ends the request with ...
5
votes
Accepted
Exposing current Commerce2 Cart via the Element API
The cart is not baked into \Craft, but craft\commerce\Plugin.
Try the following:
$cart = craft\commerce\Plugin::getInstance()->getCarts()->getCart();
Uses the following function:
/**
* Get ...
4
votes
How do I cache with the ElementAPI plugin?
I was also very interested in this, so i forked the repo from theoplampert and added some extra features. You can find it here, https://github.com/jacobnijgh/ElementAPI. This fork allows you to not ...
4
votes
Accepted
How can I get to a category via the category slug using the ElementAPI
I think (well, I know) I am confused by where "regular" php is and where the ElementAPI takes over. This is returning me data, but I'm not sure if it's just dumb luck or I've actually got something ...
4
votes
Is there a way to return asset data along with entry data?
Yes - you can access it exactly the same way you do in the templates; you just need to use PHP syntax (e.g. -> for accessing sub-properties rather than .).
'transformer' => function(EntryModel $...
4
votes
Accepted
Smart Map - Sorting for closest location in PHP
You're very close!
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->myFieldHandle = array(
'target' => 90210,
'range' => 100
);
$criteria->order = '...
4
votes
Accepted
Element API locale
I had the same problem lately and solved it with a locale query parameter.
The code for this inside craft/config/elementapi.php looks like this:
return [
'endpoints' => function () {
'...
4
votes
Accepted
How can I add custom headers to an ElementAPI response?
You can do this by assigning your endpoint(s) to a function:
return [
'endpoints' => [
'my-endpoint' => function() {
HeaderHelper::setHeader(['Surrogate-Control: max-age=...
4
votes
Accessing global sets from Element API
Since Global Sets are elements just like entries, users, etc., you can create API endpoints that are all about them:
'globals/<handle:\w+>' => function($handle) {
return [
'...
4
votes
Accepted
List Orders with datePaid is within last 14 days using the Element API plugin
Here's an example elementapi.php that should get you going...this is returning shipping details but you can modify the transformer for whatever data you need of course...
return [
'endpoints' => [
...
4
votes
Accepted
How to render a Twig template in the element-api plugin (v2.5)
You didn’t tell where that Twig file is located, but I assume your issue has to do with being in the wrong “Template Mode”.
Here’s how you can switch mode:
Craft::$app->view->setTemplateMode()
...
4
votes
How to render a Twig template in the element-api plugin (v2.5)
You're using $view->resolveTemplate('_blog-entry') which is returning the absolute path to the template as a string, and then passing that to $view->render which is expecting a relative path, ...
4
votes
Saving asset programatically, eg via API
Not an answer directly to your question but a general function to upload an asset in PHP. In my case $file is an uploaded file in the $_FILES array. You can download a file per url and insert it's ...
4
votes
Accepted
Using Element API to output Super Table fields
Here is a solution I found. Define an empty array, then loop over it, providing whatever keys you want to return from the Super Table fields. Simply by looping over $block->columnTextBlocks (the ...
3
votes
Accepted
How do I access the category of an entry with Element API?
Same as you would access it from a Twig template. If you're positive there will always be one category selected, then this would work:
'transformer' => function(EntryModel $entry) {
return [
...
3
votes
How to create a static JSON files with Element API and cache it with Varnish
Not sure how you can do this, but you could manually print the response to a file yourself on a regular basis (as a cron job) using wget:
This:
wget -S http://craft.fira/fullmembers.json
…saves the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
plugin-elementapi × 229craft3 × 40
json × 21
elementcriteriamodel × 19
plugin-development × 15
php × 13
entries × 10
plugins × 8
relations × 8
matrix × 7
categories × 7
plugin-craftcommerce × 6
cache × 6
assets × 5
ajax × 5
query × 5
elementquery × 5
authentication × 5
localization × 4
search × 4
datetime × 4
plugin-craftcommerce2 × 4
javascript × 4
entries-field × 4
users × 3