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

Adding custom fields to Search using onBeforeWrite - but what about Tagfield?


Go to End


884 Views

Avatar
neilcreagh

Community Member, 136 Posts

19 November 2014 at 1:18am

Hi, I'm setting up a bookshop and adding the custom fields "Subtitle" and "Authors" to the default search by putting them into the MetaDescription using the onBeforeWrite method. This works great on Text fields...

public function onBeforeWrite() { 
   parent::onBeforeWrite();
   $Text1 = $this->getField('Subtitle'); 
   $Text2 = $this->getField('Authors'); 
   $this->setField('MetaDescription',"{$Text1} by {$Text2}");
	}

But the problem is that my "Authors" field is actually a many_many 'TagField' :

static $many_many = array(
	'Authors' => 'Author',

So the onBeforeWrite code above works great for the Subtitle but doesn't work for the Authors. Any idea how I could get this to work?

Because the 'TagField' is actually an extension of a standard Text field and because it's onBEFOREWrite is there a way I could just grab whatever text is in that field - I don't need to know if it's a existing author or match it to a data object or anything. ?