Jump to:

23364 Posts in 18142 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Shortcode function context question

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 220 Views
  • tazzydemon
    Avatar
    Community Member
    120 Posts

    Shortcode function context question Link to this post

    I am trying to make a shortcode to only output some text on the home page (and the shortcode is in custom site settings footer html code)

    I have just realised writing this that the shortcode context is customsitesettings and not the page_controller context where I had it, but this still leaves me with a small problem if finding the current page and whether its parentid is 0 (home page).

    So this was my junk page_controller code:

    public function About($args){
    if ($this->getParentID()== 0 && isset($args['id'])){
    $about = '<a data-reveal-id="'.$args['id'].'">About</a> |&nbsp;';
    return $about;
    }
    }

    How do I get the page object in the customsitesettings context? I'm feeling a bit thick here!

  • tazzydemon
    Avatar
    Community Member
    120 Posts

    Re: Shortcode function context question Link to this post

    Maybe its Director::get_current_page()->ParentID

    So my function is

    public function About($args){
    if (Director::get_current_page()->ParentID == 0 && isset($args['id'])){
    $about = '<a data-reveal-id="'.$args['id'].'">About</a> |&nbsp;';
    return $about;
    }
    }

    Oh yes and I was trying to avoid testing for url 'home' but i guess that is sacrosanct and many pages have ParentID=0. That was a mistake.

    So this works:

    public function About($args){
    if (Director::get_current_page()->URLSegment == 'home' && isset($args['id'])){
    $about = '<a data-reveal-id="'.$args['id'].'">About</a> |&nbsp;';
    return $about;
    }
    }

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