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.

Blog Module /

Discuss the Blog Module.

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

Adding fields to BlogCategory and BlogTag


Go to End


3 Posts   1366 Views

Avatar
phpMagpie

Community Member, 6 Posts

9 December 2016 at 3:41am

Hi,

I'm trying to add extra fields to BlogCategory, but after adding them to the $db array and running dev/build no new fields are created.

If I add a new field to /blog/code/model/BlogPost.php::db and run dev/build then the field is created as expected.

Is there some special build that I need to run to get dev/build to recognise new config changes in /blog/code/model/BlogCategory.php.

The site uses Silverstripe 3.2.

Thanks, Paul

Avatar
phpMagpie

Community Member, 6 Posts

9 December 2016 at 4:13am

Edited: 09/12/2016 4:52am

Realised I should be doing this via /mysite/code/Extensions instead, so tried the following:

Created /mysite/code/Extensions/BlogCategoryExtension.php:

<?php
class BlogCategoryExtension extends DataExtension {

	private static $db = array(
		'Description' => 'Varchar(255)',
		'MetaTitle' => 'Varchar(255)',
		'MetaDescription' => 'Varchar(255)',
	);
	
	public function updateCMSFields(FieldList $fields) {
	  $fields->push(new TextField('Description'));
	  $fields->push(new TextField('MetaTitle'));
	  $fields->push(new TextField('MetaDescription'));
	}
	
}

I then added the following to /mysite/_config/config.yml:

...
BlogCategory:
  extensions:
    - BlogCategoryExtension
...

Then ran /dev/build.

No field was added to the database table and when I try and access the blog part of silverstripe's admin I just get a grey screen. If I remove the lines I added to config.yml and run /dev/build again all returns to normal.

Avatar
phpMagpie

Community Member, 6 Posts

9 December 2016 at 4:57am

I'm 99.9% certain this has something to do with BlogCategory being passed through Injector and renamed to BlogCategoryLinkCustomisation.

I needed to extend BlogTag in the same way so tested with the process detailed above and that worked fine.

So, I then added the following to config.yml:

...
Injector:
   BlogCategory:
     class: BlogCategoryLinkCustomisation
BlogCategoryLinkCustomisation:
  extensions:
    - BlogCategoryExtension
BlogTag:
  extensions:
    - BlogTagExtension
...

When I run /dev/build no fields are created for BlogCategory, but when I view the BlogCategory admin form I do have 3 extra fields ... which is some form of progress. Any advice on how I extend BlogCategory when it's been passed through Injector?