Skip to main content
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 ...
Mats Mikkel Rummelhoff's user avatar
5 votes
Accepted

Get Assets in JSON with ElementAPI

Something like the following should work: // elementapi.php return [ 'endpoints' => [ 'assets.json' => [ 'paginate' => false, 'elementType' => ...
Mats Mikkel Rummelhoff's user avatar
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->...
Mark Watson's user avatar
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->...
andrew.welch's user avatar
  • 11.6k
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 ...
Mats Mikkel Rummelhoff's user avatar
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 ...
Mats Mikkel Rummelhoff's user avatar
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 ...
Jørgen's user avatar
  • 165
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 () { '...
periodical's user avatar
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 [ '...
Brandon Kelly's user avatar
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' => [ ...
Jeremy Daalder's user avatar
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() ...
carlcs's user avatar
  • 36.3k
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, ...
tremby's user avatar
  • 165
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 ...
Robin Schambach's user avatar
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 ...
andrewmenich's user avatar
3 votes
Accepted

Element API Endpoint returns data but has 404 header

Removing this from my site's nginx conf fixed it: location ~* \.(?:manifest|appcache|html?|xml|json)$ { expires -1; } However, what I really want to do is cache stuff, without breaking this ...
Adam Menczykowski's user avatar
3 votes
Accepted

How do I query data using the Element API Plugin based on a custom field?

You’re close. But the relatedTo param can’t just be set to some username. It has to either be an element ID or an actual element model (more specifically, a user ID or UserModel in this case). 'users/...
Brandon Kelly's user avatar
3 votes
Accepted

ElementAPI handling CategoryModel and returning a heirachical object

Element API doesn’t have any special support for returning nested arrays of element data, but you can hack it by limiting the initial batch of categories to just those at the top level, and within the ...
Brandon Kelly's user avatar
3 votes

How can I pull my Username into the transformer of the ElementAPI plugin?

I was able to pass the $username data to the transformer. By doing this: 'transformer' => function(EntryModel $entry) use ($username)
Sandy D.'s user avatar
  • 383
3 votes

ElementAPI - Can I use javascript/ajax to load json

You better use GET rather than POST : $(function () { var data = { 'CSRF': $('input[name="CRAFT_CSRF_TOKEN"]').val(), }; $.ajax({ 'type': 'get', 'contentType': '...
benoit_mire's user avatar
3 votes

ElementAPI - Can I use javascript/ajax to load json

Totally do-able. Once you have the ElementAPI plugin downloaded and installed, you'll want to make sure you have created a file called elementapi.php and it is in your /craft/config/ directory. For ...
Damon's user avatar
  • 4,706
3 votes
Accepted

Retrieve entry data from an Entries field in a Matrix Block

You’re off to a great start here, in the way that you are first assembling the $menuBlocks array, and then finally returning the main array with the title and menu keys. For the section items, you ...
Brandon Kelly's user avatar
3 votes
Accepted

Query Entry Using multiple custom Fields Element API

getPost() will search the request’s POST data, but you are setting the parameter values as query string params, so you should be using getQuery(). 'studentNumber' => craft()->request->...
Brandon Kelly's user avatar
3 votes

Access Element API JSON in another environment

Brandon answered this in a github issue https://github.com/pixelandtonic/ElementAPI/issues/4 You can do that by setting your endpoint config to an anonymous function: 'my/endpoint/pattern' => ...
Spoeken's user avatar
  • 327
3 votes
Accepted

In the Element API plugin, how can I get only tags that are related to a particular section?

The relatedTo parameter accepts either a single element ID or model, or an array of element IDs or models. It doesn't accept asset source handles, section handles, or a particular element type ...
Mats Mikkel Rummelhoff's user avatar
3 votes

Get repeated events with the Solspace Calendar and Element API plugins

Unfortunately this won't be possible with Solspace Calendar since we are extending ElementCriteria with Calendar_EventCriteriaModel, which has a layer on top of it allowing us to build recurring ...
Solspace's user avatar
  • 1,410
3 votes
Accepted

Complex Conditionals with ElementCriteriaModel / Element API

After doing a bit more digging, I think the only real option here is run a preliminary query to pull out the ids of the elements I want and then pass that into the ElementCriteriaModel. It's not ideal,...
Michael Witwicki's user avatar
3 votes
Accepted

ElementAPI: Error's out if entry doesn't have assets

Why don't you just check if there is at least one photo? Something along the lines of 'transformer' => function(EntryModel $entry) { return [ 'ID' => $entry->id, 'Year' =&...
Andris Sevcenko's user avatar
3 votes
Accepted

Get 'purchasable' from ElementAPI

Try return [ 'endpoints' => [ 'products.json' => [ 'elementType' => 'Commerce_Product', 'criteria' => [ // 'category' => 'products', // randomly trying ...
Luke Holder's user avatar
  • 6,837
3 votes

How to skip the first X entries using ElementAPI

It seems that offset doesn't work when pagination is set to true (which is its default value). If you don't need pagination for your results, you can add your offset in criteria and set pagination ...
missmatsuko's user avatar
3 votes

ElementAPI: How to order and/or limit results?

Just setting the limit property doesn’t do the trick alone, you need to deactivate pagination, too: 'criteria' => [ 'section' => 'trucks', 'limit' => 10, 'orderBy' => '...
medoingthings's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible