Autonav + custom page attribute

Permalink
I was wondering how to do the followng:

- I have some pages that have a parent
- Those pages have a "url" custom attribute
- I want to use autonav to display the subpages' "url" attributes.

Basically I have product pages where the manufacturer is advertised and I want to display his URL (which exists as a custom attribute) in a side menu.

I can imagine it being easy but I'm too tired to figure it out. If you help me, PM me your paypal address so I can buy you a beer (and also tell me how much a decent beer costs in your city).

VPenkov
 
mesuva replied on at Permalink Reply
mesuva
I think for what you are describing you might want to look at using the Page List block (with a Custom Template), and not the Auto-Nav block. Although they do a similar thing, the Auto-Nav is more focused on being able to display nested page structures, while the Page List focuses on lists of pages along with descriptions and other attributes.

So have a look at creating a Custom Template for the Page List block.
In short: copy view.php from /concrete/blocks/page_list to /blocks/page_list/templates/custom.php. Then to use it, in edit mode, click on a page list block and select Custom Template from the popup menu and select 'Custom' from the drop down. The name of your file in the templates folder, in this case custom.php, is what determines the name of the custom template in the dropdown.

In the page list template, you'll see a for loop that stores a page in $cobj for each iteration. Somewhere in that loop you could simply go:

$website = $cobj->getAttribute('url');   // where url is the name of your custom 
if($website) {
   $website = str_replace('http://', '', $website); // this is to handle both addresses with and without the http
   echo '<a target="_blank" href="http://'.$website.'">' . $website . '</a>';
}


As an example, here's where I've used this approach to list details from sub-pages:
http://www.saba.org.au/directory/website-and-internet-services/...
This page just has a page list block with a custom template that outputs the attributes from each page. It will automatically update when a subpage is added.

The other thing you might be wanting to do is simply outputting an attribute on a particular page type (so hardcoded in a template). If that is the case, you just use:

$c = Page::getCurrentPage();
$website = $c->getAttribute('url');


The example of this using the same site is here:
http://www.saba.org.au/directory/website-and-internet-services/mesu...
This is where I have an actual page type 'business' in my theme that that outputs the attributes automatically.

Hope that is the kind of info you are looking for.
VPenkov replied on at Permalink Reply
VPenkov
Thanks but not quite, I already got to there.
I was wondering if AutoNav or PageList could get subpages (recursively) and if there's an "url" custom attribute on a certain page, display it in a menu.

The recursive part is what troubles me.
mesuva replied on at Permalink Best Answer Reply
mesuva
In that case, the Auto-Nav is the right block to use I believe, it can display the whole sitemap as a nested un-ordered list, or you can select parts of the sitemap, different levels, etc. The block can do that out-of-the-box.

The default view.php for the Auto-Nav already uses attributes for some behaviour - look at line 56, where is has:
if ($_c->getCollectionAttributeValue('replace_link_with_first_in_nav'))


I'm thinking you should be able to retrieve your url attribute the same way and then control the output using it.

I do find the autonav a bit confusing myself, I've only edited a few times for things simpler things like adding specific classes to menu items.
VPenkov replied on at Permalink Reply
VPenkov
I found this:https://raw.github.com/jordanlev/c5_clean_block_templates/master/aut...

It was developed by jordanlev who seems to be my favorite community member for a while now. With his autonav view.php it was quite easy for me to achieve my goal.

Check it out, it should be included as the default template IMO.
jordanlev replied on at Permalink Reply
jordanlev
Thanks! I do plan on submitting it to the core system after 5.5. is released (right now I have a cleaned-up page list template in there, I just want to do one at a time).

Glad you figured this out. Since you're comfortable working with the template code, I think that's the best solution. But for future reference, I had an idea for a simpler solution -- just add "External Link" pages under each company in the sitemap (the title of the page can be the url as well, if you want that to be displayed in the menu).

-Jordan