1
return [
    'endpoints' => [

        'journal/<entryId:\d+>.json' => function($entryId) {
            return [
                'elementType' => 'Entry',
                'criteria' => ['id' => $entryId, 'section'=>'journal'],
                'first' => true,
                'transformer' => function(EntryModel $entry) {
                    $nextEntry = $entry->getNextSibling();
                    // also tried getNext()

                    $upNext = [];
                    $upNext["title"] = $nextEntry->title;
                    // errs out here ...
                }
            ];
        }
    ]
];

I feel like I'm missing something obvious, but how do I get the next sibling of an entry using Element API? Would it be easier/better to pass both the current entry ID and the next entry ID (which I have available) into the criteria to make it easier to grab?

Thanks!

1
  • Just to check, 'journal' is a Structure section and you're only looking to return one entry, correct?
    – Brad Bell
    Commented Aug 27, 2015 at 17:03

1 Answer 1

3

Depends on what you mean by “next”.

If the entry is within a Structure section and you want whatever entry is positioned after it, you’d use getNextSibling().

$nextSibling = $entry->getNextSibling();

Otherwise you want getNext(). But you need to keep in mind that which entry is next can vary in infinite ways, so you really need to supply that $criteria argument here.

The point of the $criteria argument is to define a collection of entries in a specific order, in which $entry is one of them, and Craft uses that to determine which entries come immediately before and after it.

Your example code shows that you are returning Journal entries, so I’ll assume that you want $nextEntry to be the journal entry that was written after $entry – so the entries should be sorted in chronological order. If that’s the case, here’s the code you should use:

$nextEntry = $entry->getNext([
    'section' => 'journal',
    'order' => 'postDate asc'
]);

As far as what you can pass into that argument, all of the craft.entries parameters are fair game here, with the exact same value syntax. Just keep in mind that it’s PHP code and not Twig, so instead of something like:

.postDate('<= 2013-06-04')

it would be:

'postDate' => '<= 2013-06-04',

You can also pass in an actual ElementCriteriaModel object, with its values set ahead of time:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'journal';
$criteria->postDate = '<= 2013-06-04';
$criteria->order = 'postDate asc';

$nextEntry = $entry->getNext($criteria);
2
  • Works great, thank you! Quick followup, where is it documented what options I have for the $criteria argument? e.g. section journal, order postDate, postdate <= otherDate? I'm having trouble finding it in the Craft documentation. Commented Aug 27, 2015 at 18:15
  • @JonathanWalters Just updated my answer with more info about that. Commented Aug 27, 2015 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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