Jump to:

1385 Posts in 493 Topics by 522 members

Themes

SilverStripe Forums » Themes » Getting the home page

Discuss SilverStripe Themes.

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

Page: 1 2
Go to End
Author Topic: 4846 Views
  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: Getting the home page Link to this post

    Don't apologize, you came back for a reply didn't you

    I was going to say that hiding the page name field would hide it from other pages as well - but that would be the case if you do it with css.

    But with $fields->removeFieldFromTab("Root.Content.Main","Title"); in your templateforhomePage.php you could do just that.

    And after I did a dev/build I indeed noticed that a new homepage was made, so it might even be a good idea to hide the Title-field from just the homepage. You can even give another page the name home, and it will be renamed to /home-2. Now I just have to find the page where all the settings and options for page.php are given, maybe there is a setting that I can also protect that page some more ... All the info we need is here and on the doku websites, sometimes the only problem is how to find it. I am constantly asking myself how that could be improved.

    Now I am off to bed! Cheers!

    EDIT: you also have to give $fields->removeFieldFromTab("Root.Content.Metadata","URL"); in order to protect against people changing a page name/url

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Getting the home page Link to this post

    Hey yuirgoul

    I don't like relying on the URLSegment either. For almost all the Sites I created, I used a special HomePage class that can only be created once. To ensure this, you'll have to override the canCreate method of the HomePage, something along the lines of:

    public function canCreate($member = null){
       $rights = parent::canCreate($member);
       if($rights == false)
          return false;
          
       $dobj = DataObject::get('HomePage');
       if(!$dobj)
          return true;
          
       return $dobj->Count() == 0;
    }

    On your Page_Controller you would have something like:

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

    ...to get the HomePage. I understand that there might be several HomePages when using Translatable. That can be solved by adding a where clause to the get_one statement. Like this:

    DataObject::get_one('HomePage', 'Locale = \'' . Translatable::get_current_locale() . '\'');

    This ensures that you get the HomePage of the current locale the user is in. Adding this to the canCreate and getHomePage methods should make this work even for multilingual sites.

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: Getting the home page Link to this post

    Thanx very much, this sounds like a jewel! I will play with it and it will probably greatly enhance my understanding of Silverstripe!

    Thanks again!

  • Mario...
    Avatar
    Community Member
    14 Posts

    Re: Getting the home page Link to this post

    Helped me out loads, thanks banal:

       public function PageByLang($url, $lang="nb-NO" /*your default lang*/) {
        $SQL_url = Convert::raw2sql($url);
        $SQL_lang = Convert::raw2sql($lang);

    $page = DataObject::get_one('FrontPage', 'Locale = \'' . Translatable::get_current_locale() . '\'');
       

        if ($page->Locale != Translatable::get_current_locale()) {
        $page = $page->getTranslation(Translatable::get_current_locale());
        }
        return $page;
       }

  • steve_nyhof
    Avatar
    Community Member
    224 Posts

    Re: Getting the home page Link to this post

    Hi Willr

    This is working for me. How can I add an <% if ??? %> to this to find out what page the control is calling?

    <% control Page(disclaimer) %>
    $Content
    <% end_control %>

    In my Page.php file I have..

    ---

    class Page_Controller extends ContentController {

    function popup() {
       return $this->renderWith('PagePopup');
       }

    ---

    I then call up a template called PagePopup.ss

    mysite.com/disclaimer/popup

    When I click on another page, like mysite.com/terms/popup - how can I render the template with the "terms" page $Content?

    4846 Views
Page: 1 2
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.