Remember me? forgot password | register

SilverStripe Forum

7881 Posts in 2037 Topics by 1136 members

Jump to:

Site Builders

If this is your first visit, you will need to register before you can post. However, you can browse all messages below.

Community » SilverStripe Forum » Site Builders » Issue with multiple has_one of same class on page
Page: 1 go to end Reply
Author Topic: Issue with multiple has_one of same class on page 9504 views
  • asinesio
    asinesio's avatar
    6 posts

    Issue with multiple has_one of same class on page Link to this post

    So, I'm creating a site using SilverStripe, and I'm having some struggles trying to figure out the best way to accomplish a goal.

    The site I'm creating has a HomePage where there are three "content boxes" that have a header, an image, and some links below them.

    I am trying to allow my clients to manage the data that appears in each box.

    I have a ContentBox DataObject class that has a few data members. I also have a HomePage class that has three instances of ContentBox, e.g.:

    class HomePage extends Page {

    static $has_one = array(
          'LeftContentBox' => 'ContentBox',
          'RightContentBox' => 'ContentBox',
          'BottomContentBox' => 'ContentBox'
    );   

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

       
           // content boxes
          $fields->addFieldsToTab('Root.Content.LeftContentBox', LeftContentBox::getCMSFieldsToInclude());
          $fields->addFieldsToTab('Root.Content.RightContentBox', RightContentBox::getCMSFieldsToInclude());
          $fields->addFieldsToTab('Root.Content.BottomContentBox', BottomContentBox::getCMSFieldsToInclude());
    return $fields;
    }
    }

    ContentBox has its own array() of fields:

    class ContentBox extends DataObject {
    ...
    function getCMSFieldsToInclude() {

        $fields = array(
       new TextField('HeaderText', 'Header Text'),
    new TextField('HeaderLink', 'Header Link URL'),
           new ImageField('Image')
        );
        return $fields;
    }
    }

    When I try and edit a HomePage in the CMS, I get the following error:

    FATAL ERROR: collateDataFields() I noticed that a field called 'HeaderText' appears twice in your form: 'Form_EditForm'. One is a 'TextField' and the other is a 'TextField'
    At line 110 in /Applications/MAMP/htdocs/sapphire/forms/CompositeField.php

    ------

    So, does anyone have any suggestions as to how I should design this? Do I need to make three separate subclasses of ContentBox (left, bottom, right) that implement different named fields?

    Thanks in advance for any help you can provide.

    -Andy

    Last edited: 25 April 2008 at 4:43pm

  • Blackdog
    Blackdog's avatar
    Community Member
    99 posts

    Re: Issue with multiple has_one of same class on page Link to this post

    I can't see why you are using DataObject.

    Please explain what type of content you propose for each of those 3 "content boxes".

  • willr
    avatar
    Core Development Team
    930 posts
    3 SilverStripe Sites 3 SilverStripe Themes

    Re: Issue with multiple has_one of same class on page Link to this post

    If a page has 3 content boxes I usually just 'hardcode' all the fields on HomePage. Seems easier. Sure you do repeat yourself 3 times and it looks nasty but it works :P

  • asinesio
    asinesio's avatar
    6 posts

    Re: Issue with multiple has_one of same class on page Link to this post

    Blackdog, the reason I'm using it is because many different pages of my site have the same content box.

    A content box will have something like this:

    ---------------
    |"Header Text"|
    ---------------
    | Image |
    ---------------
    | > Link 1 |
    | > Link 2 |
    | > Link 3 |
    ---------------

    So, the reason I'm using DataObject is because I wanted to allow the user to manage the 1...N links in each content box.

    I suppose the easier way is just to give them another HTML field where they can enter an unordered list.

    willr, thanks for the advice. I'll take it to heart. I was just trying to DRY my code. :)

    If anyone else has any suggestions, I'm all ears.

    -Andy

  • Blackdog
    Blackdog's avatar
    Community Member
    99 posts

    Re: Issue with multiple has_one of same class on page Link to this post

    ok, serious. don't use Dataobjects for that, you will over complicate the situation.

    If the content boxes will just be replicated on another page, use the following.

    For example if you set the root of the content to be entered on the home page.

    <% control Page(home) %>
    $Title
    $Content
    $Some-other-field
    <% end_control %>

    That will pull the data from the home page to any other template you put that within. You can use it multiple times for your different content boxes.

    <% control Page(home) %>
    $Header
    <% end_control %>

    <% control Page(home) %>
    $Image
    <% end_control %>

    <% control Page(home) %>
    $Links
    <% end_control %>

    Last edited: 26 April 2008 at 10:59am

    9504 views
go to top Reply

Currently Online:

mocsa, fdqepd, acriis, Garrett, LeeUmm, Nemitz

Welcome to our latest member: acriis