Can someone analyze this if / else hardcode autonav?

Permalink
Just following hints I've been able to piece together on the forums, but clearly the else part is never being recognized. Whatever I plug in to the if statement is the only thing that gets placed on the page.

<?php
        $page = Page::getCollectionParentID(); //gets name of Parent
        if ($page = 'current-campaigns'):         
            $nav = BlockType::getByHandle('autonav');
            $nav->controller->orderBy = 'display_asc';
            $nav->controller->displayPages = 'custom';
            $nav->controller->displayPagesCID = 'support-and-join';
            $nav->controller->displaySubPages = 'relevant';
            $nav->controller->displaySubPageLevels = 'all';
            $nav->render('view');
     else: 
            $nav = BlockType::getByHandle('autonav');
            $nav->controller->orderBy = 'display_asc';
            $nav->controller->displayPages = 'current';
            $nav->controller->displaySubPages = 'relevant';

 
adajad replied on at Permalink Reply
adajad
I know it shouldn't matter, but have you tried it like this?

<?php
        $page = Page::getCollectionParentID(); //gets name of Parent
        if ($page = 'current-campaigns'){         
            $nav = BlockType::getByHandle('autonav');
            $nav->controller->orderBy = 'display_asc';
            $nav->controller->displayPages = 'custom';
            $nav->controller->displayPagesCID = 'support-and-join';
            $nav->controller->displaySubPages = 'relevant';
            $nav->controller->displaySubPageLevels = 'all';
            $nav->render('view');
        }else{ 
            $nav = BlockType::getByHandle('autonav');
            $nav->controller->orderBy = 'display_asc';
            $nav->controller->displayPages = 'current';
            $nav->controller->displaySubPages = 'relevant';


And also, doesn't Page::getCollectionParentID(); return an integer and not a string?
mnakalay replied on at Permalink Reply
mnakalay
Yes indeed getCollectionParentId() returns and Id (an integer) and not a string. It's the id of the parent of the current page. now if you really want to have a string you should do:

$parentId = Page::getCollectionParentId(); //gets ID of Parent
$page = Page::getByID($parentId, $version = 'RECENT'); //gets NAME of Parent

then it should work with the string 'current-campaigns'

something else that you could do, but it has nothing to do with solving the problem, just to make the code easier to maintain I think; is write it like this:
<?php
$parentId = Page::getCollectionParentId();//gets ID of Parent
$page = Page::getByID($parentId, $version = 'RECENT'); //gets NAME of Parent
$nav = BlockType::getByHandle('autonav');
$nav->controller->orderBy = 'display_asc';
    if ($page = 'current-campaigns'):         
        $nav->controller->displayPages = 'custom';
        $nav->controller->displayPagesCID = 'support-and-join';
    else: 
        $nav->controller->displayPages = 'current';
    endif; 
$nav->controller->displaySubPages = 'relevant';
$nav->controller->displaySubPageLevels = 'all';
$nav->render('view');?>


What it does is take the lines of code that are the same in both sides of the if statement and take them out, keeping in the if statement only those lines that depend on it. It avoids redundancy (DRY or Don't Repeat Yourself Principle) I didn't try it though so no promises but really it should not pose any problem.

Good luck
zoinks replied on at Permalink Reply
Thanks, mnakalay.

I did try to use the # of the page first, but for simplicity, I didn't mention it.

I'll try this code and see if it works. Just a quick question: CID isn't asking for an integer here rather than a string?
$nav->controller->displayPagesCID = 'support-and-join';
mnakalay replied on at Permalink Reply
mnakalay
oups I didn't se that. You're definitely right.

If you look in the autonav controller, around line 292, you have the function generateNav() which includes the following:
switch($this->displayPages) {
            case 'current':
               $cParentID = $this->cParentID;
               if ($cParentID < 1) {
                  $cParentID = 1;
               }
               break;
            case 'top':
               // top level actually has ID 1 as its parent, since the home page is effectively alone at the top
               $cParentID = 1;
               break;
            case 'above':
               $cParentID = $this->getParentParentID();
               break;
            case 'below':

as you can se $cParentID takes an integer for value. So for this:
case 'custom':
               $cParentID = $this->displayPagesCID;
               break;

to work, $this->displayPagesCID; needs to be an integer as well