Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [SOLVED] Managing has_one Relationship in Admin

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

Page: 1
Go to End
Author Topic: 778 Views
  • henrik_
    Avatar
    Community Member
    5 Posts

    [SOLVED] Managing has_one Relationship in Admin Link to this post

    Hey there,
    I'm new to Silverstripe.

    I want to show a Custom-Header on every Page, so I created a DataObject called Header.

    class Header extends DataObject{
       const BUTTONS = "Enum('NONE, DONATE, BECOME_MEMBER', 'NONE')";
       static $db = array(
          'Title' => 'Varchar(255)',
          'GeneralDescription' => 'Text',
          'BigButton' => self::BUTTONS,
          'SmallButton' => self::BUTTONS
       );
       static $has_one = array(
          'BackgroundImage' => 'Image',
       );
       static $has_many = array(
          'Pages' => 'Page'
       );   
    }

    Every Page now should have a connection to a header. It should be possible to choose either a existing Header-Entry or create a new one (simple drowdown isn't enough). This is how Page.php looks like:

    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
          'Header' => 'Header'
       );
       
       public function getCMSFields(){
          $fields = parent::getCMSFields();
          //what to add here?   
          return $fields;
       }
    }

    Can I use a GridField here? This would be the functionality I want to have. The problem is, that GridFields only work for Lists and not for single Models?

    Thanks!

  • Willr
    Avatar
    Forum Moderator
    5482 Posts
  • henrik_
    Avatar
    Community Member
    5 Posts

    Re: [SOLVED] Managing has_one Relationship in Admin Link to this post

    Thank you!
    But this just lets me choose existing header, doesn't it?
    More often the user will create a new header. Any idea how I can achieve this?

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: [SOLVED] Managing has_one Relationship in Admin Link to this post

    If you add a GridFieldAddNewButton component to the GridFieldConfig then you'll also be able to create new records. Alternatively, you could start with a config that already includes it. For example:

       public function getCMSFields() {
          $fields = parent::getCMSFields();
          $gf = GridFieldConfig_RecordEditor::create();
          $gf->addComponent(new GridFieldHasOneRelationHandler($this, 'Header'), 'GridFieldPaginator');
          $field = new GridField('Header', null, Header::get(), $gf);
          $fields->addFieldToTab('Root.Main', $field, 'Content');
          return $fields;
       }


    will let you manage the has_one relation as well as viewing, editing, adding and deleting Headers.

  • henrik_
    Avatar
    Community Member
    5 Posts

    Re: [SOLVED] Managing has_one Relationship in Admin Link to this post

    Awesome! Thank you very much!

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