Hardcoded Prev/Next, getting ExternalLink() error

Permalink
Hi, I hardcoded the previous and next page link block into one of my templates, based on this:http://concrete5tricks.com/blog/hardcode-previous-and-next-page-lin... ...

<?php
         //initialize the block
         $next_prev = BlockType::getByHandle('next_previous');
         //set your options
         $next_prev->controller->orderBy = 'display_asc';
         $next_prev->controller->loopSequence = true;
         $next_prev->controller->excludeSystemPages = true;
         //get the pages
         $prev_page = $next_prev->controller->getPreviousCollection();
         $next_page = $next_prev->controller->getNextCollection();
         //get url's to the pages
         $nh = Loader::helper('navigation');
         $prev_url = $nh->getLinkToCollection($prev_page);
         $next_url = $nh->getLinkToCollection($next_page);
         ?>


And I'm getting this error on my Page Types > Defaults page:

Fatal error: Call to a member function isExternalLink() on a non-object in C:\xampp\htdocs\site\concrete\core\helpers\navigation.php on line 36


Any idea what this is or how I can fix this issue?

Thanks for any help!

 
bbeng89 replied on at Permalink Reply
bbeng89
Maybe try verifying that $next_page and $prev_page are actually set before generating the links. Like this:
<?php if(is_object($prev_page)):?>
    <a href="<?php echo $nh->getLinkToCollection($prev_page); ?>">Previous Page</a>
<?php endif; ?>
<?php if(is_object($next_page)):?>
    <a href="<?php echo $nh->getLinkToCollection($next_page); ?>">Next Page</a>
<?php endif; ?>
mssteph replied on at Permalink Reply
Thanks for the quick response! Unfortunately, I get the same error.
bbeng89 replied on at Permalink Reply
bbeng89
Hmmm. Could you repost your code with the is_object() checks I suggested?
mssteph replied on at Permalink Reply
Sure! Here it is:

<?php
         //initialize the block
         $next_prev = BlockType::getByHandle('next_previous');
         //set your options
         $next_prev->controller->orderBy = 'display_asc';
         $next_prev->controller->loopSequence = true;
         $next_prev->controller->excludeSystemPages = true;
         //get the pages
         $prev_page = $next_prev->controller->getPreviousCollection();
         $next_page = $next_prev->controller->getNextCollection();
         //get url's to the pages
         $nh = Loader::helper('navigation');
         $prev_url = $nh->getLinkToCollection($prev_page);
         $next_url = $nh->getLinkToCollection($next_page);
         ?>
bbeng89 replied on at Permalink Best Answer Reply
bbeng89
You need to take out the lines:
$prev_url = $nh->getLinkToCollection($prev_page);
$next_url = $nh->getLinkToCollection($next_page);

Because we aren't using those anymore and it's still possibly sending a null to getLinkToCollection(). So your code should look like this:
<?php
         //initialize the block
         $next_prev = BlockType::getByHandle('next_previous');
         //set your options
         $next_prev->controller->orderBy = 'display_asc';
         $next_prev->controller->loopSequence = true;
         $next_prev->controller->excludeSystemPages = true;
         //get the pages
         $prev_page = $next_prev->controller->getPreviousCollection();
         $next_page = $next_prev->controller->getNextCollection();
         //get url's to the pages
         $nh = Loader::helper('navigation');
         ?>
         <?php if(is_object($prev_page)):?>
            <a href="<?php echo $nh->getLinkToCollection($prev_page); ?>">Previous Page</a>
mssteph replied on at Permalink Reply
IT WORKS! Thank you!!
bbeng89 replied on at Permalink Reply
bbeng89
Awesome! Glad I could help! I'll post a comment on that blog and let jordanlev know that section could cause people some problems.
mssteph replied on at Permalink Reply
Yeah, that would be helpful! Thanks again!
bbeng89 replied on at Permalink Reply
bbeng89
Sorry, when I originally posted my previous message I forgot to remove those two lines from the complete code. I updated it now so it should be good. Let me know if that works for you.