Display Custom Attribute - Nav Item Image in Autonav

Permalink
Hello, I am very new to concrete5. I feel have been doing reasonably well so far but I have hit a brick wall with trying to get images to display in my auto nav menu!

If someone could point me in the direction of an up to date tutorial or offer some assistance I would really appreciate it.

1. I have created a page attribute called 'nav_item_image'
2. I have uploaded a different image icon via properties in the site map for each page.
3. I coped view.php from the 'concrete/blocks/autonav' folder and moved it to 'mysite/blocks/autonav/templates/custom_menu_view.php'
4. I have assigned my new custom template to my auto nav block.

I need to work out how to call my custom attribute 'nav_item_image' in the PHP code.

I added the below but I know this isn't correct and doesn't work...

$image = $page->getAttribute('nav_item_image');


This is the block of code from 'mysite/blocks/autonav/templates/custom_menu_view.php'

echo '<ul class="nav">'; //opens the top-level menu
foreach ($navItems as $ni) {
   echo '<li class="' . $ni->classes . '">'; //opens a nav item 
    $image = $page->getAttribute('nav_item_image');
    echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><img src="' . $image . '" class="nav_icon" /><br />' . $ni->name . '</a>';
   if ($ni->hasSubmenu) {
      echo '<ul>'; //opens a dropdown sub-menu
   } else {
      echo '</li>'; //closes a nav item
      echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
   }
}
echo '</ul>'; //closes the top-level menu


I have tried the following help pages but I think some of this refers to older versions of c5?

-http://www.codeblog.ch/2009/12/image-navigation-items/...
-http://www.concrete5.org/community/forums/customizing_c5/auto-nav-w...
-http://www.concrete5.org/community/forums/themes/image-in-auto-nav-...
-http://www.concrete5.org/community/forums/customizing_c5/autonav-wi...

sutcliffe121
 
exchangecore replied on at Permalink Reply
exchangecore
I think what you're after would be something more like this:
$image = $ni->cObj->getAttribute('nav_item_image');


If that doesn't work you may have to go chase down each collection object. I think that the navigation object has a method to get that so you might also be able to do:
$image = $ni->getCollectionObject()->getAttribute('nav_item_image');
sutcliffe121 replied on at Permalink Reply
sutcliffe121
Thanks for that. I have added that to my file and I am getting the following error now when viewing the site where the image should be.

Catchable fatal error: Object of class File could not be converted to string in C:\Program Files (x86)\Ampps\www\logical_design\blocks\autonav\templates\left_menu.php on line 117
exchangecore replied on at Permalink Reply
exchangecore
This is because using this got you the "File" object, not the url.

Take a look at the documentation for File objects:http://www.concrete5.org/documentation/developers/files/files-and-f...

I think that if you wanted the URL for that image file you could do something like this:
$image->getUrl()


Alternatively you could probably just do something like this too:
$image = $ni->getCollectionObject()->getAttribute('nav_item_image')->getUrl();
sutcliffe121 replied on at Permalink Reply
sutcliffe121
$image = $ni->cObj->getAttribute('nav_item_image')->getUrl();


Above gives me: Fatal error: Call to a member function getUrl() on a non-object in C:\Program Files (x86)\Ampps\www\logical_design\blocks\autonav\templates\left_menu.php on line 115

Full section:

echo '<ul class="nav">'; //opens the top-level menu
foreach ($navItems as $ni) {
$image = $ni->cObj->getAttribute('nav_item_image')->getUrl();
    echo '<li class="' . $ni->classes . '">'; //opens a nav item
    echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><img src="' . $image . '" class="nav_icon" /><br />' . $ni->name . '</a>';
   if ($ni->hasSubmenu) {
      echo '<ul>'; //opens a dropdown sub-menu
   } else {
      echo '</li>'; //closes a nav item
      echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
   }
}
echo '</ul>'; //closes the top-level menu



I will have a read through the link you gave.
exchangecore replied on at Permalink Reply
exchangecore
Maybe you have to get a file revision... Give this a shot, if it doesn't work i'll take a look at it some more when I have a little bit more time.

$image = $ni->cObj->getAttribute('nav_item_image')->getVersion()->getRelativePath();
sutcliffe121 replied on at Permalink Reply
sutcliffe121
Ahh the non-object errors were because some of the new pages did not have nav item images added to them. There was nothing to handle pages with no nav item image.

Probably not the best way of doing it, but it works:

$image = $ni->cObj->getAttribute('nav_item_image');
   if ($image){$image_url = $image->getURL();}
   else {$image_url = 'noimage.jpg';}


echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '"><img src="' . $image_url . '" class="nav_icon" /><br />' . $ni->name . '</a>';


Thanks for your help.