Posts Tagged ‘Extend Silverstripe’

Silverstripe CCK or Silverstripe for Drupal Users

Posted on 2009.08.03 by Tom

The first CMS I really got into that wasn’t some system I had built was Drupal. It is both a great and a terrible CMS to use, especially as my cherry poppin CMS. One thing that I liked about Drupal that makes the switch to other CMS a lot of work for me is the way Drupal uses Content Construction Kit (CCK) to allow you to create new “node” types and the forms that match. This seemed really easy to me where I could take a list much like this:

  • Pages
    • Already Created at Install
  • Sponsors
    • Sponsor Name
    • Sponsor URL
    • Sponsor Banner Image
    • Sponsor Priority
  • Classes
    • Class Category
    • Class Name
    • Class Description
    • Class Instructor
    • Class Capacity
    • Class Slots Filled

and begin to turn this into my node types by just making a few adjustments to the list, really not even adjustments just classifications. Now here is that list after classifying them

  • Pages
    • Already Created at Install
  • Sponsors
    • Sponsor Name – Textfield – Limit 128
    • Sponsor URL – Textfield – Limit 128
    • Sponsor Banner Image – Textfield – Limit 64
    • Sponsor Priority – Dropdown – 0-10
  • Classes
    • Class Name – Textfield – Limit 64
    • Class Description – Textarea – 5×20
    • Class Instructor – Textfield – Limit 64
    • Class Capacity – Integer – Limit 3
    • Class Slots Filled – Integer – Limit 3

Now for Drupal we have a pretty nice little list to just create in CCK by naming them applying these limits to the fields. This is pretty easy and if you ask your client to classify a certain area of their site like sponsors many times they will give you a list must like the first one. Using CCK this is all easy and once you are done you have a nice form, automatically built, that will only ask the administrator for the information that is needed and will create the node for them, it is straight forward, looks like all the other Drupal admin forms and handles validation for you.

So when I try other CMSs like modX, Concrete5, Joomla, etc… I always wonder as soon I install it how i go about doing the same thing within their system. Well the truth is that most CMS have something similar to Drupal’s CCK and to tell the truth again it is usually hard to find documentation on it because every calls it something different. There are content types, page types, and many many more names for the same thing. I prefer the term page type, because i think that it universally makes sense.

Creating this same thing in Silverstripe is actually really simple, it makes sense, and to me it is better than Drupal’s system because it is more flexible and since you are building basically a model and controller for your page type that extends the base Page model and controller then you have all the functionality of the Page but can also extend it as far as you want.

So lets get started with this same process just using Silverstripe! Lets take our blank list and classify it for Silverstripe.

  • Pages
    • Already Created at Install
  • Sponsors
    • Sponsor Name – Textfield – Varchar(128)
    • Sponsor URL – Textfield – Varchar(128)
    • Sponsor Banner Image – File Upload
    • Sponsor Priority – Dropdown from 0-10 – Integer(2)
  • Classes
    • Class Name – Textfield – Varchar(64)
    • Class Description – Textarea – Text
    • Class Instructor – Textfield – Varchar(64)
    • Class Capacity – Textfield – Integer(3)
    • Class Slots Filled – Textfield – Integer(3)

Notice that the classifications of these fields more closely models a database, this is because Silverstripe is smart and they know that you are going to build these extensions with Models, so they should model a data source more closely. Let Take our list and get started with creating our page types now!

Lets start by creating a new file for our site. Open up the installation of Silverstripe and open up the folder mysite/code Next we want to create a file to extend the Page Model and Controller. Following Silverstipe conventions we will append the word “page” at the end of the file name, since we are extending Page. Lets do Classes as our example for simplicity. So lets add a file to our mysite/code folder and name it ClassPage.php With Silverstripe extending things is a lot like working with an MVC framework such as CakePHP or Code Igniter so we chose to name that ClassPage.php after the singular version of what our Model will represent. If we had been doing the Sponsors instead we would of named it SponsorPage.php

Now that we have our file, lets open it and start editing our code! Once we open the file we are going to want to let the system know what we have by declaring our Model class first. So lets do the following:

		class ClassPage extends Page
		{
			static $db = array();
 
			function getCMSFields()
			{
				return $fields;
			}
		}

Basically we have just declared our Model and told the system the name. Next we need to add more code to this declaration and make it actually add our database fields and connect them to this model. We also need to write the getCMSFields function, as it is a Silverstripe standard that will be called for your Model once someone creates a new instance of this page type. So lets get that out of the way and create our class variable db, another Silverstripe standard. This will ensure that when someone runs a db update that our fields will be installed into the DB. So lets change that previous code to this:

		class ClassPage extends Page
		{
			static $db = array(
				'ClassInstructor' => Varchar(64),
				'ClassCapacity' => Int(3),
				'ClassSlotsFilled' => Int(3)
			);
 
			function getCMSFields()
			{
				$fields = parent::getCMSFields();
				$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassInstructor'));
				$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassCapacity'));
				$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassSlotsFilled'));
				return $fields;
			}
		}

Notice i left out the Title and Description fields, i feel that since they are kind of standard that leaving them out is ok to do, makes it easier to read and they are standard so they will be some kind of common tie among the different content types. So now we have our Content Type created, we only need to do one more thing to have a basic working extension here and that is to define the controller. This part might be confusing to people coming from other MVC frameworks as they are included in the same file, I didn’t like it at first but it really is ok and you will get used to it. Notice that in the getCMSFields() function that we are adding “Root.Content.ClassInfo” That is basically the tab at the top of the admin interface to let you know it is separate from the main content tab. So at the bottom of the model start a new class and leave it empty, since we are not needing to customize the existing Page_Controller.

	class ClassPage_Controller extends Page_Controller
	{
	}

So our entire file should look like this:

	class ClassPage extends Page
	{
		static $db = array(
			'ClassInstructor' => Varchar(64),
			'ClassCapacity' => Int(3),
			'ClassSlotsFilled' => Int(3)
		);
 
		function getCMSFields()
		{
			$fields = parent::getCMSFields();
			$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassInstructor'));
			$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassCapacity'));
			$fields->addFieldToTab('Root.Content.ClassInfo', new TextField('ClassSlotsFilled'));
			return $fields;
		}
	}
 
	class ClassPage_Controller extends Page_Controller
	{
	}

Now we can test it out, the first thing we need to do is go to our site and login to the admin areas. Then in a new tab append the following to the end of the URL /dev. Once you open that page you will see some text and the DB will be updated. Now we can go to our admin area and use our new page type!

There is still more to do but mainly in the TPL files and making sure that your info displays correctly or at least has a default for people to override in their own themes. We will save the theming for next time, it is nice and straight forward too but try this out and see what you think. If there are errors in any of this code let me know, I was mainly doing this as a reference so I didn’t build and run this but it should work just fine.

Bookmark and Share