Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Global Variable For Background Image


Reply

5 Posts   1996 Views

Avatar
steve_nyhof

26 December 2009 at 3:29pm Community Member, 224 Posts

I have read some topics on global values or variables. Something about adding a home page.

Here is what i am trying to do...

I have 20 different background images in separate folders (at the moment) that my clients can select from. Currently I have them dragging the one being used out of a "Backgrounds" folder, then dragging another with the same name from another folder into the Backgrounds folder.

I have a Header Images tab in my editor to upload images for a specific page, but i am looking to upload a background image file to show for the whole site.

Does anyone have some ideas, and examples please?

Thank you,
Steve

Avatar
tobych

27 December 2009 at 3:45pm (Last edited: 27 December 2009 3:46pm), Community Member, 97 Posts

Steve,

I'm confused. Is this a question about SilverStripe, about CSS, or about your particular editor?

Assuming for now that's it's about SilverStripe, if you want clients to be able to choose a background image in the CMS, one way is to add a field to your HomePage page type. Then add code in your Page_Controller to pull in an appropriate CSS file. Each CSS file would have perhaps just the one line specifying the background image by accessing this field in the template. You could instead preprocess a single CSS file based on the value.

I recently added some functionality similar to this for a client. The code looked something like this:

class HomePage extends Page {
   
public static $db = array(
            'ThemeStyle' => "Enum('default, beige_red, blue', 'default')"
            );
...

function getCMSFields() {
$fields = parent::getCMSFields();
$themeStyleField = new DropdownField('ThemeStyle', 'Theme Style', singleton('HomePage')->dbObject('ThemeStyle')->enumValues()
);
$fields->addFieldToTab('Root.Content.SiteConfiguration', $themeStyleField);
return $fields;
}

...

class Page_Controller extends ContentController {
   
   public function init() {
      parent::init();
      Requirements::themedCSS("layout");
      Requirements::themedCSS("typography");
      Requirements::themedCSS("form");
      Requirements::css($this->themeStyleSheet());
   }

   function themeStyleSheet() {
    $themeStyle = $this->getHomePage()->ThemeStyle;
    return "themes/mytheme/css/styles/$themeStyle.css";
   }   

   function getHomePage() {
    return DataObject::get_one('HomePage');
   }
   
}

Avatar
steve_nyhof

27 December 2009 at 5:17pm Community Member, 224 Posts

Thank you for this.

I am working through a similar thing with a global variable set on it's own global tab, and requires the Home Page. The problem I am facing is that I am unable to get the Home Page to show up in the Bahaviors tab.

I will work through your code and see if I am missing something.

Avatar
tobych

28 December 2009 at 4:56pm Community Member, 97 Posts

Feel free to show us your code. Or try the IRC channel.

Toby

Avatar
steve_nyhof

29 December 2009 at 2:43am Community Member, 224 Posts

Hi Toby,
See this post...
http://silverstripe.org/content-editor-discussions/show/262452?start=8#post276050

I am on a time frame to get this working in just few days.

I may go back to the start as SilverRay says on a test setup and see where I am going wrong.