Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Override a function from SiteTree with a module

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

Page: 1
Go to End
Author Topic: 2708 Views
  • John Silver
    Avatar
    Community Member
    10 Posts

    Override a function from SiteTree with a module Link to this post

    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?

  • Martijn
    Avatar
    Community Member
    271 Posts
  • John Silver
    Avatar
    Community Member
    10 Posts

    Re: Override a function from SiteTree with a module Link to this post

    Thanks for the answer, I will check it out.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Override a function from SiteTree with a module Link to this post

    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/

  • John Silver
    Avatar
    Community Member
    10 Posts

    Re: Override a function from SiteTree with a module Link to this post

    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)

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