Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Simple site-wide banner management .. Help, please?

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

Page: 1
Go to End
Author Topic: 1579 Views
  • Imploosio
    Avatar
    Community Member
    5 Posts

    Simple site-wide banner management .. Help, please? Link to this post

    New to SilverStripe, I'm trying to wrap my head around the whole data object and relationship thing, and what should probably be a pretty common feature - managing ad banners on my site. Yet I fail miserably trying to understand what's going on.

    I have three fixed banner areas on my site, left and right column and a wider content banner. I've created a new tab 'Banners' to the admin page where I can name my banner, set a link and upload a picture. I've tried using ComplexTableField, HasOneComplexTableField and DataObjectManager to deal with the problem but I think I fail at understanding how the relationships between pages and data objects work.

    Here's what I have so far,

    Page.php

    public static $has_one = array(
    'BannerLeft' => 'BannerLeft',
    'BannerRight' => 'BannerRight',
    'BannerWide' => 'BannerWide'
    );

    public function getCMSFields() {
    $fields = parent::getCMSFields();

    $banner_left = new ComplexTableField(
    $this,
    'BannerLeft',
    'BannerLeft',
    array(
    'Image' => 'Banner image',
    'URL' => 'URL'
    ),
    'getCMSFields_forPopup'
    );
    $banner_left->showPagination = false;

    $banner_right = new ComplexTableField(
    $this,
    'BannerRight',
    'BannerRight',
    array(
    'Image' => 'Banner image',
    'URL' => 'URL'
    ),
    'getCMSFields_forPopup'
    );
    $banner_right->showPagination = false;

    $banner_wide = new ComplexTableField(
    $this,
    'BannerWide',
    'BannerWide',
    array(
    'Image' => 'Banner image',
    'URL' => 'URL'
    ),
    'getCMSFields_forPopup'
    );
    $banner_wide->showPagination = false;

    $fields->addFieldToTab('Root.Banners', $banner_left);
    $fields->addFieldToTab('Root.Banners', $banner_right);
    $fields->addFieldToTab('Root.Banners', $banner_wide);
    return $fields;
    }

    For each banner data object (ie. BannerLeft.php)

    class BannerLeft extends DataObject {
    static $db = array(
    "Name" => "Text",
    "URL" => "Text"
    );

    static $has_one = array(
    "Image" => "Image"
    );

    static $has_many = array(
    "Page" => "Page"
    );

    public function getCMSFields_forPopup() {
    return new FieldSet(
    new TextField('Name'),
    new TextField('URL'),
    new FileIFrameField('Image')
    );
    }
    }

    In Page.ss I've tried to print out stuff with

    <% control BannerLeft %>
    <a href="$URL" title="$Name">$Image.SetSize(150,410)</a>
    <% end_control %>

    It produces the right kind of things in the admin views. I have a tab called 'Banners' in the top row where I can upload my banners and they seem to be shared throughout the site on every page, but unless I set the the field type to HasOneComplexTableField and click the radio button checked individually for each and every page, I don't get any visible results to front-end. Idea is to have just one banner at any given time in any of the banner areas. ComplexTableField (or DataObjectManager) should work out fine, right?

    Also, would it be possible to just use a single Banner data object for all the areas, instead of having 3 clones for areas?

    Any help and words of wisdom would be greatly appreciated!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    You would be better adding the banner fields and complex table field on a single page (such as your HomePage if you have one) then in your template you would get the banner from the homepage. This would save you from having the relationship on every. single. page.

    <% control Page(home) %>
    <% control BannerLeft %>
    <a href="$URL" title="$Name">$Image.SetSize(150,410)</a>
    <% end_control %>
    <% end_control %>

  • Imploosio
    Avatar
    Community Member
    5 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    Oh, awesome. For some reason ComplexTableField returns nothing to templates, but HasOneComplexTableField works fine once I've 'checked' the radio buttons once. Anyhow, now that all the banners are in the same place and I don't have to manage them per page this is certainly something I can deal with. Thanks a lot Will!

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    Woudn't that be something for SiteConfig, where more sitewide things live?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    martimiz - yes SiteConfig would be good for that but quite a few people have noted issues with ComplexTableFields and SiteConfig. I'm sure it would be able to be setup, just haven't attempted it myself.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    Willr - I see, I spoke too soon... It seems the CTF doesn't acknowledge SiteConfig's ID. There's a discussion here that mostly concerns the DOM:
    http://silverstripe.org/dataobjectmanager-module-forum/show/12048

    Here you need to (tested, looks like it works):
    - make sure the (banner)object has a 'has_one' = array('SiteConfig' => 'SiteConfig');
    - in the SiteConfig decorator create a field like: $banners = new DataObjectManager($this->owner, ...
    - add a custom sourceID: $banners->setSourceID($this->owner->ID);

    A patch like this for the CTF would be nice. I think there's something about that in the thread as well...

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    Thanks good to know those steps. Perhaps the documentation needed to be appending with that / a tutorial on ssbits would be good that people could reference. Since CTF is deprecated for 3.0 a patch for CTF may not make it into the post 2.4 releases.

  • Plato
    Avatar
    Community Member
    26 Posts

    Re: Simple site-wide banner management .. Help, please? Link to this post

    The issue lies with CTF relying on the parent form to have a field named ID.
    The setSource functions available on a vanilla CTF did not work for me, and as such I found the solution lies in pushing a hidden field on to the siteconfig through it's decorator.

    $fields->push(new HiddenField('ID', '', $this->owner->ID));

    http://sspaste.com/paste/show/4f4ebfa33f39a

    Setting the ID to the owner->ID was a previous fix I'd discovered, however it only worked for ImageDOM's.
    However: neither of these work-arounds have been tested with Translatable / subsites.

    Bumping old thread for benefit of future searchers. Enjoy.

    1579 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.