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

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


Reply


5 Posts   669 Views

Avatar
vwd

Community Member, 160 Posts

10 October 2011 at 3:57pm

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.

Avatar
MarcusDalgren

Community Member, 288 Posts

10 October 2011 at 8:11pm

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.

Avatar
ajshort

Community Member, 244 Posts

11 October 2011 at 12:20am

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

Avatar
vwd

Community Member, 160 Posts

11 October 2011 at 6:47pm

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.

Avatar
MarcusDalgren

Community Member, 288 Posts

11 October 2011 at 9:04pm

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.