Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Shortcode function context question


Reply

2 Posts   342 Views

Avatar
tazzydemon

22 November 2013 at 11:49am Community Member, 122 Posts

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!

Avatar
tazzydemon

22 November 2013 at 12:03pm (Last edited: 22 November 2013 12:10pm), Community Member, 122 Posts

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;
}
}