Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Showing HasManyComplexTableField data on every page - weird problem

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

Page: 1
Go to End
Author Topic: 637 Views
  • bones
    Avatar
    Community Member
    104 Posts

    Showing HasManyComplexTableField data on every page - weird problem Link to this post

    Hi

    On my "HomePage.php", I'm using the following:

    $tablefield = new HasManyComplexTableField(
             $this,
             'Banners',
           'Banner',
           array(
                'BannerImage' => 'Banner Image',
                'BannerText' => 'Banner Text'
           ),
           'getCMSFields_forPopup'
          );
          $tablefield->setAddTitle( 'Banner' );
          $fields->addFieldToTab( 'Root.Content.Banner', $tablefield );


    Together with "Banner.php":

    <?php

    class Banner extends DataObject {
       
       static $db = array(
          'BannerText' => 'Varchar(100)'
       );
       static $has_one = array(
          'Page' => 'Page',
          'BannerImage' => 'Image'
       );
       
       function getCMSFields_forPopup() {
          return new FieldSet(
             new TextareaField('BannerText'),
             new ImageField('BannerImage')
          );
       }
    }
    ?>


    So far so good- everything works. However, I want to display these same items on every page throughout the website. So in "Page.php", I added the following:

       public function BannerBox(){
          return DataObject::get("Banner");
       }


    Initially, this seemed OK too, displaying the items throughout the site. Then I realised that the items were being displayed regardless of whether they were enabled (ie regardless of whether the tickbox in the popup was ticked or not). Am I using the correct method to call the data onto each page, or is this a bug?

    Many thanks

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Showing HasManyComplexTableField data on every page - weird problem Link to this post

    Maybe I misunderstand you, but I see no reference to any 'enable' boolean or checkbox in your Banner Object? Anyway, if there was one, you could check on it in your DataObject::get, and that would be that...

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Showing HasManyComplexTableField data on every page - weird problem Link to this post

    Sorry, I've made an error in my information. The tickbox isn't in the popup, it's next to the entry (see attached file).

    The weird thing is that using

    public function BannerBox(){
    return DataObject::get("Banner");
    }


    completely ignores the tickbox. If I omit this code, the tickbox works correctly (but the info is confined to just the homepage.

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Showing HasManyComplexTableField data on every page - weird problem Link to this post

    OK, I that that one links the banners that are checked to your homepage, but it doesn't really disable their further use within the website. If you want to do it like this, you could maybe do something like this (simplyfied):

    $homepage = DataObject::get_one('Sitetree', "ClassName = 'HomePage'");
    return $homepage->Banners();

  • bones
    Avatar
    Community Member
    104 Posts

    Re: Showing HasManyComplexTableField data on every page - weird problem Link to this post

    Fantastic! Works perfectly. Thank you so much

    637 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.