RantFever 4

I pontificate but not in the pejorative sense of the word.

Blog Categories to Rant Categories

Posted 6 April 2013
Written by Abinadi Ayerdis
Category PyroCMS

When designing this site, I wanted the front page to have some tiles on it that linked to major sections. I quickly saw that Pyro's blog categories really leant themselves to this idea.

PyroCMS's Categories

I wanted to associate an image and a description with each category tile. But to make that happen is problematic. PyroCMS's default_blog_categories table has three fields: id, slug, and title. So how to display the categories on the front page. Luckily, PyroCMS comes with a widget to display the categories and the capability to assign the widget to an area. The area can then be added wherever you want in a post or in a template. Fine. I can do that. 

The only problem with the categories widget is that I am forced into an unordered list with the category names in order by title. No place to put an image and no description. So I quickly realized I would need to do two things: alter the table structure and associated admin pages as well as create a new widget to output the categories with an associated image and description.

For the record, there are probably lots of ways to make this happen, this is just how thought to get it done. With web development, there is always more than one way to skin a cat.

default_blog_categories table

The first thing I did was change the structure of the table. I added two columns: image_path and description. I made them both null. I suppose that isn't strictly necessary, but I wanted to leave the option of not putting anything in those fields. Now my database is ready to accept the fields I want. I would note that the image_path was a varchar(255) and description a textarea. The image_path could contain anything, really. A full path would be preferable, I suppose, because then it would be very portable. I didn't do that, though perhaps I should have. I just put the name of the file there and left it to the code to fill in the right path.

Hacking the admin pages

Generally speaking, it isn't a good idea to change core files. That makes upgrading later next to impossible. Or, rather, it makes lots of stuff break when you upgrade. So by changing the core, I am essentially locking myself into this version of PyroCMS. That is okay for me now, as the next version will be the Laravel version, and I'll want to get the professional version of that instead of the community edition I have now. 

I went straight to system/cms/modules/blog/views/admin/categories. This is where the two views live that house the form and the listing of categories within the control panel. I added two inputs to the form:

<li class="even">
            <label for="title"><?php echo lang('global:title');?> <span>*</span></label>
            <div class="input"><?php echo  form_input('title', $category->title) ?></div>
            <label for="slug"><?php echo lang('global:slug') ?> <span>*</span></label>
            <div class="input"><?php echo  form_input('slug', $category->slug) ?></div>
            <label for="image_path"><?php echo lang('cat:image_path') ?> </label>
            <div class="input"><?php echo form_input('image_path', $category->image_path) ?> </div>
            <label for="description"><?php echo lang('cat:description') ?> </label>
            <div class="input"><?php echo form_textarea('description', $category->description) ?> </div>
            <?php echo  form_hidden('id', $category->id) ?>

You can see that I followed the same form as the PyroCMS developers, so that the two new fields look like they have always been there. ;)

The next step is to check the controller and module to make sure the fields get saved and updated. I added rules for the fields in the Admin_Categories controller. And that was all I needed to change in that controller, because the controller sends the whole post array to the module, the fields are sent along with the rest.

The module blog_categories_m.php only needed a couple of changes to make it save the image_path and description. I added the following to both the insert and the update methods and presto!

 'image_path' => $input['image_path'],
 'description' => $input['description']

Now it all works. I can see the new fields in the categories form and they update to the database on save/update. Good job.

The Rant Categories widget

I put the rant_categories widget in the shared_addons folder. I could have put it in the core directory, but I figured that I had messed with the core enough. A widget only needs two files to work. I copied the existing categories widget and altered the display so that the image_path and the description would be output. No sweat. I added some styles, and I have the category tiles I had dreamed of. 

Now all I need to do is write some posts!


There are no comments

Posting comments after three months has been disabled.