Adding File Manager Images via page template

Permalink
I've got a site where I'd like to assign a background image based on the section of the site.

For a sitemap that looks like this:

HOME
--ABOUT
--PRESS
----PAGE 1
----PAGE 2
----PAGE 3
--CONTACT

I've got the page types adding a body id that matches the section name, so PRESS and PAGE 1 both have a <body id="press"> That code looks like this:
<body id="<?php 
   $f = preg_split('/\//', $c->getCollectionPath());
   echo $f[1]
?>">


What I was thinking was that I'd upload an image to the file manager, which I'd title "press." Then I could write some PHP in the template that says, in effect, "find an image in the file manager with a title that matches the body ID."

So I'm wondering if that's possible, and if this would be the best way to accomplish this. I thought about adding a Custom Attribute and manually selecting an image from the file manager, but I don't think you can have a custom attribute cascade down to children pages. Then the problem becomes, when you create new pages, they wouldn't have a file selected.

Any suggestions would be appreciated!
Thanks.

 
glockops replied on at Permalink Reply
glockops
Searching through the file manager will generate a tremendous amount of overhead.

With a little bit of custom coding, you can cascade attributes from parents. This is how I do it

In each page type template I have this:
<?php   
        // Get the section index
      global $si;
        $this->inc('elements/functions.php'); 
        // Prevent function from loading if Master Collection is being viewed
        if(!$c->isMasterCollection()) {
            $si = find_section_index($c);
        } else {
            $si = Page::getByID('1');
        }
    ?>

I'm interested in getting the nearest page that has the attribute "section index" - I use this for a section-specific custom autonav. The master collection if prevents problems when setting page defaults.

Here's the relevant contents of my elements/functions.php
<?php
// Find a parent page that has the "section_index" attribute set
// Updated 1/27/2012 to allow Alias to effectively "hide" their original location
// i.e. Alias pages will appear as if they are housed in the area they are aliased to.
function find_section_index($id) {
   // Create an ID array to hold each child/parent
   // Is this page an alias?
   if($id->getCollectionPointerID() > 0 ) {
      $id = Page::getByID($id->getCollectionPointerOriginalID());
      // Allows us to "unhide" an alias when needed
      if($id->getAttribute('override_default_alias_behavior')) {
         $id = Page::getByID($id->getCollectionPointerID());
      }
   } 
   // Loop through parents until SI or home is found

The first part is some custom things I needed for working with aliases, the bulk of what you need is the while loop. Section_index is just a boolean (checkbox) value.

$si now contains the page object that has an attribute of "section_index". Then in my hard-coded autonav I have the following:
<?php
// Automatic Sidebar Navigation
$nav_side = BlockType::getByHandle('autonav'); 
$nav_side->controller->displayPages = 'custom'; 
$nav_side->controller->displayPagesCID = $si->getCollectionID();
$nav_side->controller->orderBy = 'display_asc'; 
$nav_side->controller->displaySubPages = 'all';
$nav_side->controller->displaySubPageLevels = 'custom';  
$nav_side->controller->displaySubPageLevelsNum = 2;
$nav_side->controller->displayPagesIncludeSelf = 1; 
$nav_side->render('templates/sidenav');
?>


You could adopt a similar system to look for an attribute called "page background" if it was found, use it - if not find a parent that has it set (or go up the tree all the way to the homepage).

This also allows you to give a child of a section index it's own attribute.

Hope that helps!
Metaglyphics replied on at Permalink Reply
Thanks for this. I think this is definitely on the right track.

I've set up a custom attribute called header_image. It's an image/file attribute, with which I can select an image from my file manager.

Say I add this attribute to my PRESS page, for example. The pages below that (PAGE 1, PAGE 2) should look to see if they have their own header_image attribute. If they do, use it. If not, start looking up the chain to find the nearest parent page with that attribute.

The first part of your code seems to do something similar, though it's looking for an ID, rather than an image path.

Where I'm stuck right now is on how to get this code to return the path to the resulting image. Then I can write a style like:

#banner {background-image: url(<?php echo $si; ?>);


But I'm having some trouble finding that.
Metaglyphics replied on at Permalink Reply
Okay, I think I can simplify my requirement a little more. I don't think I need the conditional statement. I can just have a template that says "display the image from the 'page_header' custom attribute of the parent page."

So if my sitemap is:

HOME
--BLOG
----Blog Entry


the Blog Entry template would need logic that says "display the header_image from BLOG"

Does anybody know how to get that image path?

Thanks