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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Override a function from SiteTree with a module


Go to End


5 Posts   4746 Views

Avatar
John Silver

Community Member, 10 Posts

13 August 2009 at 4:15am

I'd like to override the function MetaTags from \sapphire\core\model\SiteTree.php. Of course I could just extend SiteTree with a new Page.php, but I want to make my new MetaTags available as a module.

I tried the following:
- created a new folder inside the silverstripe
- created a new "_config.php" with the following content:

DataObject::add_extension('SiteTree', 'SiteTreeExtensions');

-created a file "code/SiteTreeExtensions.php" with the following content:
class SiteTreeExtensions extends DataObjectDecorator {
	public function MetaTags($includeTitle = true) {
		$tags = "XXX";

		return $tags;
	}
}

When check the output, I still get the text from the "old" MetaTag-function.

Is it simply not possible to override functions, or am I doing it wrong?

Avatar
Martijn

Community Member, 271 Posts

15 August 2009 at 3:22pm

Maybe you can rip some code from the MetaManager:

http://silverstripe.org/all-other-modules/show/265170?showPost=265210

Avatar
John Silver

Community Member, 10 Posts

18 August 2009 at 8:02pm

Thanks for the answer, I will check it out.

Avatar
dhensby

Community Member, 253 Posts

18 August 2009 at 11:25pm

This might not at all be what you are trying to do, but it might be useful to you:

http://ssbits.com/autofill-metadescription-and-metakeywords-on-page-save/

Avatar
John Silver

Community Member, 10 Posts

19 August 2009 at 10:17pm

Pigeon: Thanks, that was not exactly what I was looking for, but the site is great and has lots of other interesting stuff.

Martijn: Great Extension, but it's also not quite what I'm looking for. I want to overwrite the "MetaTags" function so I can call "$MetaTags(0)" in my template and get the output generated by my module.

For now I solved it by overwriting the function in the controller, but I think that's not very clean. The original function is located in SiteTree, which is the model.

_config.php

	DataObject::add_extension('SiteTree', 'SiteTreeExtension');
	DataObject::add_extension('Page_Controller', 'SiteTreeExtension_Controller');

SiteTreeExtension.php

class SiteTreeExtension extends DataObjectDecorator {
	
}

class SiteTreeExtension_Controller extends Extension {

	public function MetaTags($includeTitle = true) {
		
		// Vars
		$version = SapphireInfo::Version();
		$charset = ContentNegotiator::get_encoding();
		$currentLang = i18n::convert_rfc1766(Translatable::get_current_locale());
		$lastChangedDate = date("Y-m-d\TH:i:sP", strtotime($this->owner->LastEdited));
		
		// Set Array
		$metaData = array(
			array("generator", "SilverStripe $version - http://www.silverstripe.com"),
			array("Content-type" , "text/html; charset=$charset"),
			array("keywords" , Convert::raw2att($this->owner->MetaKeywords)),
			array("description" , Convert::raw2att($this->owner->MetaDescription)),
			array("Content-Language" , $currentLang),
			array("date" , $lastChangedDate)
		);
		$tags = "";
		foreach($metaData as $metaLine) {
			$tags .= $metaLine[1] != "" ? "<meta name=\"$metaLine[0]\" content=\"$metaLine[1]\" />\n" : "";
		}
		if ($this->ExtraMeta) {
			$tags .= $this->ExtraMeta."\n";
		}
		$this->extend('updateMetaTags', $tags);
		
		$this->extend('MetaTags', $tags);
		return $tags;
	}
	

}

(Obviously far from being finished)