Display related entries on entry page using ElementAPI 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:


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

&lt;?php
namespace Craft;

return [
  'endpoints' =&gt; [
    'ingredients.json' =&gt; [
      'criteria' =&gt; ['section' =&gt; 'ingredients'],
      'elementsPerPage' =&gt; 10,
      'transformer' =&gt; function(EntryModel $entry) {
        return [
          'title' =&gt; $entry-&gt;title
        ];
      },
    ],
  ],
];
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/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
  return [
    'elementType' =&gt; 'Entry',
    'criteria' =&gt; [
        'section' =&gt; 'ingredients',
        'relatedTo' =&gt; $entryId
    ],
    'transformer' =&gt; function(EntryModel $entry) {
      return [
        'title' =&gt; $entry-&gt;title,
        'url' =&gt; $entry-&gt;url
      ];
    },
  ];
},