By following the ElementAPI plugin documentation I was able to set up pagination of entries from the ingredients section on an entry page. Entries from the ingredients section have a relation field called relatedEntries.

Sections structures involved - set as an example:

  1. ingredients
  2. drinks

Desired output would be to show entries related to currently loaded entry from drinks section.

So when I'm at site.com/drink-entry-alfa I'd like to display all related entries from section ingredients to that current entry.

Using Twig I'd request current entry.id and use the .relatedTo() parameter to get related entries.

Here is the example from ElementAPI's documentation:

namespace Craft;

return [
  'endpoints' => [
    'ingredients.json' => [
      'criteria' => ['section' => 'ingredients'],
      'elementsPerPage' => 10,
      'transformer' => function(EntryModel $entry) {
        return [
          'title' => $entry->title
  • So you'd like to only show ingredients related to the current entry? Or you want to show all ingredients and if that ingredient is related to the current entry you want to get all entries related to that ingredient? Can you add an example of the structure using Drink A, B, C etc and Ingredient A, B, C? – Luke Pearce Aug 2 '16 at 14:23
  • Just to show ingredients related to the current entry. – Dominik Krulak Aug 2 '16 at 14:31
  • Also, @Luke Pearce you can see a little demonstration bellow the bold section in this question. – Dominik Krulak Aug 2 '16 at 14:38

If you want to show all ingredients related to the current entry you'd use the Single Entry Endpoint to pass the entry into the relatedTo param.

'ingredients/<entryId:\d+>.json' => function($entryId) {
  return [
    'elementType' => 'Entry',
    'criteria' => [
        'section' => 'ingredients',
        'relatedTo' => $entryId
    'transformer' => function(EntryModel $entry) {
      return [
        'title' => $entry->title,
        'url' => $entry->url
  • If I understand correctly here we've returned an array of all related entries titles per single entry from section ingredients. Now I'm thinking how this should point me in the right direction when I'd like to show related entries from the ingredients section of a current entry. – Dominik Krulak Aug 2 '16 at 13:38
  • I expect I failed to understand your original requirements. Can you update your original question with some more details about the site's structure and the entry you're talking about? Is the entry you're outputting these ingredients on also a related entry of an ingredient? – Luke Pearce Aug 2 '16 at 13:51
  • I'm on it right now and thanks for the grammar :) @Luke Pearce – Dominik Krulak Aug 2 '16 at 13:53
  • This question links me to this question – Dominik Krulak Aug 2 '16 at 14:40
  • Updated my original answer. This will return all ingredients related to the entry of whose ID you use in the endpoint. – Luke Pearce Aug 2 '16 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.