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

[solved] Difficulty adding new field to Root.Content.Metadata in admin area


Go to End


8 Posts   4117 Views

Avatar
WebSiteGuy

Community Member, 8 Posts

5 December 2010 at 5:51am

I would like to add a new radio button set to Root.Content.Metadata tab.

This field set will be used to allow the user to select whether to "hide" the page from search engines. Basically, depending on the users selection, either no meta tag will be added, a meta robots tag with a content value of noindex will be added, or a meta robots tag with a content value of noindex,nofollow will be added.

So ultimately my radio button set would look like:

[X] Treat this page as a normal page (no meta tag is inserted, this should also be the default selected value for the radio button set).
[ ] Let search engines see this page, but not list it in their search results (this would insert a noindex robots metatag)
[ ] Completely hide this page from search engines? (This would insert a nofollow, no index robots metatag).

I would like this to appear above the "Title' meta tag field currently in the Metadata tab.

From what I know of SS, I would modify the sitetree class in mysite/code/Page.php, so here is what I have attempted. Please note that I wasn't sure of the correct syntax for adding the radio button set, so in this example I am using a text field to simplify learning how to add fields. If anyone can give me an example with the radio button set with a default value, it would be greatly appreciated.

At the end of the mysite/code/Page.php I've added:

//custom-code: BOF New Class for MetaRobots tag
class Page_MetaRobotsTag extends SiteTree {

   public static $db = array(
   'MetaRobotsTag' => 'Text'
   );

   public static $has_one = array(
   );
   /** Add code here **/
   function getCMSFields() {
      $fields = parent::getCMSFields();
	  $fields->addFieldToTab('Root.Content.Metadata', new TextField('MetaRobotsTag'), 'Title');
      return $fields;
   }
}
//custom-code: EOF New Class for MetaRobots tag

I have /dev/build?flush=1 to rebuild the db and clear my cache, but the new field will not appear in my admin areas Root.Content.Metadata tab, can anyone tell me if I missed something in my code or what else I may be doing wrong.

I assume once I can get this to show I will then be able to make the selection and hav ethe DB store it and my next step will be to somehow change the way metatags are created to include my stored value. I see that in /sapphire/core/model/Sitetree.php around line 1228 (commented with * Return the title, description, keywords and language metatags.) I can add my code to be generated with the other metatags. Is there a better place or method to do this. I assume I generally should not be altering the core SiteTree file.

Thank you in advance for any help or advice you can give me.

Avatar
Artyom

Community Member, 22 Posts

5 December 2010 at 8:59am

You are initializing things wrong

$fields->addFieldToTab('Root.Content.Metadata', new TextField('MetaRobotsTag'), 'Title');

should be

$fields->addFieldToTab('Root.Content.Metadata', new TextField('Meta Robots Tag'), 'MetaRobotsTag');

Avatar
WebSiteGuy

Community Member, 8 Posts

5 December 2010 at 3:07pm

Thanks Artyom, still not working. Here is my complete page.php code:

<?php
class Page extends SiteTree {
	public static $db = array(
	);

	public static $has_one = array(
		);
}
class Page_Controller extends ContentController {

	/**
	 * An array of actions that can be accessed via a request. Each array element should be an action name, and the
	 * permissions or conditions required to allow the user to access it.
	 *
	 * <code>
	 * array (
	 *     'action', // anyone can access this action
	 *     'action' => true, // same as above
	 *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
	 *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
	 * );
	 * </code>
	 *
	 * @var array
	 */
	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		// Note: you should use SS template require tags inside your templates 
		// instead of putting Requirements calls here.  However these are 
		// included so that our older themes still work
		Requirements::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}
}
//custom-code: BOF New Class for MetaRobots tag
class Page_MetaRobotsTag extends SiteTree {
   public static $db = array(
   'MetaRobotsTag' => 'Text'
   );

   public static $has_one = array(
   );
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
	  $fields->addFieldToTab('Root.Content.Main', new HeaderField($title = 'Search Engine Visibility',$headingLevel = 3));
	  $fields->addFieldToTab('Root.Content.Metadata', new TextField('MetaRobotsTag'), 'MetaRobotsTag');
      return $fields;
   }
}
//custom-code: EOF New Class for MetaRobots tag	

Avatar
Zauberfisch

Community Member, 30 Posts

5 December 2010 at 3:21pm

Edited: 05/12/2010 3:23pm

adding a field to all pages is actually pretty easy.

you don't need a new class for that, and it would not work with creating a new class extending SiteTree anyway

all you need to do is:

go to mysite/code/Page.php

and make it look like this:


<?php
class Page extends SiteTree {
   public static $db = array(
   );

   public function getCMSFields() {
      $fields = parent:getCMSFields();
      $fields->addFieldToTab('Root.Content.Metadata', new TextField('MetaRobotsTag', $title = 'Search Engine Visibility'), $above = 'MetaTitle'); 
      return $fields;
   }
}

class Page_Controller extends ContentController {
   public static $allowed_actions = array (
   );
} 

as you might note i removed all thing that are not needed, if you like to keep them, do it, it doesn't matter

@artyom: both of you did it wrong, its suppose to be
$fields->addFieldToTab('Root.Content.Metadata', ....., 'MetaTitle');
because the last argument in addFieldToTab is where it should be placed, and he wants it abouve MetaTitle

Avatar
Artyom

Community Member, 22 Posts

5 December 2010 at 3:26pm

My mistake sorry. You're right.. The constructor of the FormField accociates it with with object member, and then the last param is where to insert it in the form.

Thanks for catching that. It's been a while

Avatar
Artyom

Community Member, 22 Posts

5 December 2010 at 3:32pm

The issue seems to be that you asked for it to be after "Title" and not "MetaTitle" so it disregarded your code. There is no field refering to "Title" on the Metadata tab. It's called "MetaTitle" (but appears with the prompt "Title" )

Avatar
WebSiteGuy

Community Member, 8 Posts

5 December 2010 at 3:36pm

Correct, changing the reference to Metatitle instead of Title and incorporating Zauberfisch's suggested code worked after I did a /?flush=all .

The final code instead anyone needs it was:

<?php
class Page extends SiteTree {
   public static $db = array(
   );

   public function getCMSFields() {
      $fields = parent::getCMSFields();
	  $fields->addFieldToTab('Root.Content.Metadata', new TextField('MetaRobotsTag', $title = 'Search Engine Visibility'), $above = 'MetaTitle'); 
      return $fields;
   }
}
class Page_Controller extends ContentController {

	/**
	 * An array of actions that can be accessed via a request. Each array element should be an action name, and the
	 * permissions or conditions required to allow the user to access it.
	 *
	 * <code>
	 * array (
	 *     'action', // anyone can access this action
	 *     'action' => true, // same as above
	 *     'action' => 'ADMIN', // you must have ADMIN permissions to access this action
	 *     'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
	 * );
	 * </code>
	 *
	 * @var array
	 */
	public static $allowed_actions = array (
	);

	public function init() {
		parent::init();

		// Note: you should use SS template require tags inside your templates 
		// instead of putting Requirements calls here.  However these are 
		// included so that our older themes still work
		Requirements::themedCSS('layout'); 
		Requirements::themedCSS('typography'); 
		Requirements::themedCSS('form'); 
	}
}

Avatar
Zauberfisch

Community Member, 30 Posts

5 December 2010 at 3:37pm

Edited: 05/12/2010 3:40pm

btw, here are docs for the radio button thing: http://doc.silverstripe.org/optionsetfield

god damn, i'm sorry for beeing retarded, i forgot to add the field to $db

of course you need to do that if you want the data to be save, go to Page.php

static $db = array(
   'MetaRobotsTag' => 'Text'
);