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 "...
user avatar
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/...
user avatar
  • 220
8 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->...
user avatar
7 votes
Accepted

Passing multiple parameters to the Element API

You can fetch GET parameters using HttpRequestService::getQuery() or getRequiredQuery(): 'entries-by-category' => function() { $categoryIds = craft()->request->getRequiredQuery('...
user avatar
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 ...
user avatar
  • 3,400
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'
user avatar
  • 856
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 ...
user avatar
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" ...
user avatar
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->...
user avatar
  • 7,160
6 votes
Accepted

What criteria can the Element API plugin accept?

The $entry variable that is passed to your endpoint’s transformer function will be an EntryModel object - the same type of object you use when interacting with entries in your templates. So everything ...
user avatar
6 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 ...
user avatar
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,
user avatar
  • 35.8k
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->...
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->...
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' => ...
user avatar
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 ...
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 ...
user avatar
4 votes
Accepted

How can I interact with Users via the ElementAPI?

Change function (EntryModel $user) to function (UserModel $user) You were passing in a user to a function that’s expecting an EntryModel. That’s the reason for the error you are getting.
user avatar
  • 6,672
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 ...
user avatar
  • 282
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 ...
user avatar
  • 4,696
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 $...
user avatar
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 = '...
user avatar
  • 23.6k
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 () { '...
user avatar
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=...
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 [ '...
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' => [ ...
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() ...
user avatar
  • 35.8k
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, ...
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 ...
user avatar
4 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 ...
user avatar
  • 155

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