2

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

We just released Element API 2.6.0 with added support for the offset criteria param, in combination with paginate => true. (Thanks to @missmatsuko for posting a GitHub issue about it: https://github.com/craftcms/element-api/issues/99)


2

I'm assuming that "welcome" is also your page slug. You could query the craft_elements_i18n table (pseudo code): $sql = "select id from craft_elements_i18n where slug = 'welcome' limit 1" $result = get($sql); $entry_id = $result['id']; Then feed that into your function. Replace 'welcome' with a read of the uri to make it dynamic.


2

The issue ended up being related to the yii debug toolbar failing to execute correctly. Disabling it in app.php fixed it.


2

Your second example has the correct elementType value (Entry::class with a corresponding Entry import at the top of the file). Alternatively you could do 'elementType' => 'craft\elements\Entry', The error you’re getting in that example is because you changed the endpoint to just news.json rather than api/news.json. So the request isn’t getting routed to ...


2

If you weren’t using Element API here, you could do this by adding a condition to the element query: $query->andWhere(['exists', (new \craft\db\Query()) ->from(['b' => \craft\db\Table::MATRIXBLOCKS]) ->innerJoin(\craft\db\Table::MATRIXBLOCKTYPES . ' bt', '[[bt.id]] = [[b.id]]') ->where('[[b.ownerId]] = [[elements.id]]') ->...


2

You can convert your Markdown text to HTML using yii\helpers\Markdown, which is what the |md Twig filter hooks into: 'transformer' => function(\craft\elements\Entry $entry) { return [ 'body' => \yii\helpers\Markdown::process((string)$entry->yourTextFieldHandle), ]; }, If you want the equivalent of |md('text', inlineOnly=true) (...


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:...


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

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

Have you tried grouping by category: 'hostings.json' => [ 'elementType' => Entry::class, 'criteria' => [ 'section' => 'hostings', 'groupBy' => 'hostingCompany[0].title' ], 'paginate' => true, 'transformer' => function(Entry $entry) { return [ '...


1

This seems more like it may be an error in your code somewhere, do you have dev mode turned on? I think you may want to output the title of the category: 'hostingCompany' => (string)$entry->hostingCompany[0]->title, See if that helps, if not I would update your question with your complete element-api.php


1

This tweet shows a way to adjust the elements per page dynamically with a query string param -> https://twitter.com/JakeDohm/status/1157305040278839296?s=20 so you could just request 6 or 9 items to get the same result.


1

You would need to join the array into a string, like so: // Begin defining the response data for this entry $data = [ 'title' => $entry->title, 'slug' => $entry->url, 'status' => $entry->status, 'textArea' => implode(' ',$bodyBlocks) ];


1

And just like that I stumble over this wonderful blog post https://digitalevangelist.net/blog/vuejs-craft-cms-single-page-app-logging-in-to-craft


1

change $products[] = to $products = in your for each loop, that should fix the extra square brackets. And if you want to get all entries and their titleVariation fields you should use 'elementType' => Entry::Class 'pricelist/<entryId:\d+>.json' => function($entryId) { return [ 'elementType' => Entry::Class, 'criteria' =>...


1

If your craft_searchindex table (assume your table prefix is "craft_") isn't too big (I think 100k rows is big), and your field in "relatedMerchant" is the 'title' field. You can do it in one step. $locationsRelatedToCategories = Entry::find() ->section('locations') ->limit(100000) ->relatedTo([ 'targetElement' => $categoryElement,...


1

If you want to get a specific product and it's variants by the productId you could simply do: 'api/product/<productId>' => function( $productId ) { return [ 'elementType' => Product::class, 'criteria' => [ 'id' => $productId ], 'pretty' => true, 'transformer' => function(...


1

This should work 'api/globals.json' => function() { $langHandle = Craft::$app->request->getQueryParam('lang', 'en'); return [ 'elementType' => GlobalSet::class, 'criteria' => ['site' => $langHandle], 'transformer' => function(GlobalSet $globalSet) { // start with -1 so the first one will be 0 ...


1

Couldn't you modify the criteria to exclude that entry ? id => "not X"


1

I wrote a transformer class that does exactly this: namespace your\awesome\namespace; use Craft; use craft\elements\Entry; use League\Fractal\TransformerAbstract; use craft\web\View; /** * Class TwigTransformer * Renders a TWIG entry for each item */ class TwigTransformer extends TransformerAbstract { private $twigTemplateFilePath; ...


1

I know this is a very old thread, but I stumbled onto it today because I was having a similar problem. I found that the source of my particular problem was due to the fact that I was also using Redirect plugin for Craft 3 on the site. The Redirect plugin appends a route to redirect everything to the 404 handler. I assume it was meant to be the last appended ...


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