3

Just setting the limit property doesn’t do the trick alone, you need to deactivate pagination, too: 'criteria' => [ 'section' => 'trucks', 'limit' => 10, 'orderBy' => 'title asc', ], 'paginate' => false,


2

It's not the most economical way to do it but if you don't have hundreds and hundreds of entries, this should do it for you: <?php use craft\elements\Entry; function getChildren($entries){ foreach ($entries as $entry) { $items[] = [ 'title' => $entry->title, 'slug' => $entry->slug, 'level' =>...


2

Here's how you would do that... Pass your parameter to the class: 'transformer' => new MyTransformerClassName($user), Use a constructor to save it as a property: class MyTransformerClassName extends TransformerAbstract { private $user; public function __construct($user) { $this->user = $user; } public function ...


2

SEOmatic has some functions that it uses for its controller endpoints for returning metadata for SPAs and the like: SEOmatic headless SPA APIs We can leverage these for our own purposes in an Element API endpoint, if for some reason you can't ping the SEOmatic controller via AJAX or use GraphQL to access the data. use nystudio107\seomatic\helpers\...


2

Okay I found a way to do it if it can help someone: $description_fr = ($entry->summary) ? $entry->summary : ($entry->body) ? $entry->body : ''; $entry_en = Entry::find()->id($entry->id)->siteId(2)->one(); $description_en = ($entry_en['summary']) ? $entry_en['summary'] : ($entry_en['body']) ? $entry_en['body'] : ''; $entry_de = Entry:...


2

The currentSite property from the Sites service is what you're looking for. Here's how you would use it: 'api/search.json' => function() { $siteHandle = Craft::$app->getSites()->currentSite->handle; ... $criteria = [ 'site' => $siteHandle, ... ]; return [ ... ]; },


2

You could add a new param to the request called skip and use that on your homepage, and default it to zero to allow you to reuse the endpoint elsewhere. $skipEntries = Craft::$app->request->getParam('skip') ?? 0; $criteria = [ 'section' => $settings['section'], 'limit' => $settings['limit'], 'offset' => ($settings['limit'] * ...


2

If you always want to skip the first 6 entries, you can modify the Element API endpoint to add 6 to the offset number in the query criteria: $criteria = [ 'section' => $settings['section'], 'limit' => $settings['limit'], 'offset' => ($settings['limit'] * Craft::$app->request->getParam('offset')) + 6, 'order' => $settings['...


1

You need the url, not the file path. Can you store the url, or the http://localhost:8888 part of it in an environment variable? That gives you $url = getenv('CRAFT_URL') . $id . ".json"; If it is not already in an environment variable, then in a .env file? web/index.php that comes with Craft is a good example of loading a .env file, and then using ...


1

Perhaps you could use the Preparse Field plugin, so that you only need to deal with one field. That would make the query easier. https://plugins.craftcms.com/preparse-field


1

I've found a workaround for now. It's not ideal and I feel like it will probably be too intensive at scale but, for my uses, it will work. Rather than using select() to reduce the data being returned from the initial query, I'm simply returning everything: $related = Entry::find() ->relatedTo($entry) ->all(); I'm then looping over the returned ...


1

I think i have found the answer after digging in Element docs... looks like this works! in criteria 'draftCreator' => $user->id


1

Apparently, the answers are the following: Redactor doesn't know how to output slugs/URIs instead of full URLs Possibly a solution comes up with craft in the future My API setup is suboptimal: In fact, I did set the baseUrl including the language: https://frontend.com/de. The recommendation is to not do that at the moment but keep one "Site" for all ...


1

I found a response for Craft 2 that looks like it's similar to what I want: (Passing multiple parameters to the Element API) which uses the getQuery() or getRequiredQuery() functions, but I'm not sure those still exist in Craft 3? In Craft 3, you'd use: Craft::$app->getRequest()->getRequiredQueryParam('myArr') https://docs.craftcms.com/api/v3/craft-...


1

You could use the relatedTo parameter (docs) to query recipes related to the entry that has your Entries field on. Something like: return [ 'elementType' => Entry::class, 'criteria' => [ 'section' => 'recipes', 'relatedTo' => ['sourceElement' => $entryWithEntriesField] ], Where $entryWithEntriesField is the entry (or ...


1

Assuming an endpoint for one entry like: 'elementType' => Entry::class, 'criteria' => [ 'id' => $entryId ], You could: Fetch all the custom fields for that particular entry: function getFields($entry) { $fields = []; foreach ($entry->getFieldLayout()->getFields() as $field ) { $f['handle'] = $field->handle; $...


1

The results query could not be the same, you have to separate them. use craft\commerce\elements\Product; use craft\elements\Entry; return [ 'endpoints' => [ 'search/product.json' => function () { // settings $phrase = Craft::$app->request->getParam('query'); $criteria = [ 'search' ...


1

You are setting the whole $variants variable to last item in the array instead of creating the $variants array variable and pushing items into it. Should be this: 'transformer' => function (Product $product) { $variants = []; foreach ( $product->getVariants() as $variant ) { $variants[] = [ 'type' ...


1

I know this is answered but I just wanted to suggest a potentially better way. I would suggest trying the Preparse field plugin. This way you can create a new field that automatically generates the data you want to sort/query (in your case, the first session date). This will only happen when you save the entry so it will likely speed up your API considerably....


1

I don't know if I ever got this working exactly as I initially intended, but this is the final transformer code I ended up going with. Basically the same but with some usort calls on sessions and dates. I then had to manually do the pagination in the Twig template where courses were listed. Hope it helps! <?php namespace Craft; require craft()->...


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