Jump to:

23370 Posts in 18145 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php?

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: 568 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php? Link to this post

    Hi,

    I'm trying to get my head around the function extend()

    What is the purpose of line 1255 in SiteTree.php:

    public function MetaTags($includeTitle = true) {
          $tags = "";
          if($includeTitle === true || $includeTitle == 'true') {
             $tags .= "<title>" . Convert::raw2xml(($this->MetaTitle)
                ? $this->MetaTitle
                : $this->Title) . "</title>\n";
          }

          $tags .= "<meta name=\"generator\" content=\"SilverStripe - http://silverstripe.org\" />\n";

          $charset = ContentNegotiator::get_encoding();
          $tags .= "<meta http-equiv=\"Content-type\" content=\"text/html; charset=$charset\" />\n";
          if($this->MetaKeywords) {
             $tags .= "<meta name=\"keywords\" content=\"" . Convert::raw2att($this->MetaKeywords) . "\" />\n";
          }
          if($this->MetaDescription) {
             $tags .= "<meta name=\"description\" content=\"" . Convert::raw2att($this->MetaDescription) . "\" />\n";
          }
          if($this->ExtraMeta) {
             $tags .= $this->ExtraMeta . "\n";
          }

          $this->extend('MetaTags', $tags); // Results of this function are ignored... So what is the purpose of this line?

          return $tags;
       }

    Thanks very much.

    VWD.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php? Link to this post

    Well like the comment in the code says that line isn't actually being used.
    Since $tags is a string the line would have to have been:

    $tags = $this->extend('MetaTags', $tags);

    Generally though, anywhere you find $this->extend() is a chance for you to hook in with a decorator or extension and make changes or run your own code.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php? Link to this post

    The arguments are passed by reference so that you can modify the variable value. http://php.net/manual/en/language.references.php

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php? Link to this post

    So correct me if I'm wrong... if I create an extension/decorator for SiteTree and implement the MetaTags method, then the line of code there (line 1255 of SiteTree.php) will ensure that any time SiteTree->MetaTags(...) is called, my extension/decorator's MetaTag(...) method is also called?

    So would that sum up the purpose of Object::extend(...)? It just ensures that the specified function will be called by all associated extensions/decorators if it is implemented by them?

    Thanks guys. Just trying to see what it's there for.

    Many thanks.
    VWD.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: What does "$this->extend('MetaTags', $tags);" do in line 1255 of SiteTree.php? Link to this post

    Yes that's pretty much correct. Just remember to make the variable a reference in your function definition.
    So

    function MetaTags($tags) {...

    wouldn't actually do anything but

    function MetaTags(&$tags) {...

    would. As you can see the variable itself is not passed by reference in the code (and that's deprecated since 5.3) but if you make it a reference in your function definition it should all work out.

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