Staggered Category List

UPDATE: No more CSS, have MT auto indent.

category_heirarchy.png

The "Primary Category" drop down menu just lists the categories sorted alphabetically and doesn't obey the category heirarchies you may have setup. This is especially annoying if you have several similarly named sub categories under different parent categories. So here's a hack that will change the way that field works, the categories will now obey the category heirarchy as shown in the screenshot (click for a larger view) It's probably best to note that I'm not the most perl savvy and this code may not be the most efficient/quickest/best way to do it but it works for me.

You'll need CMS.pm, open it up. Replace everything between

## Load categories and process into loop for category pull-down.

## Now load user's preferences and customization for new/edit ## entry page.

(found approximately on line number 965) with this

## Load categories and process into loop for category pull-down.
require MT::Category;
my $iter = MT::Category->load_iter({ blog_id => $blog_id });
my $cols = MT::Category->column_names;
my @data;
my $cat_id = $param{category_id};
my @cats = MT::Category->_flattened_category_hierarchy ($blog_id);
my $depth = 0;
my $i = 1;
while (my $obj = $iter->()) {
while (my $obj = shift @cats) {
my $row = { };
if (!ref ($obj)) {
if ($obj eq 'BEGIN_SUBCATS') {
$depth++;
} elsif ($obj eq 'END_SUBCATS') {
$depth--;
}
# $row->{$obj} = 1;
# push @data, $row;
next;
}
for my $col (@$cols) {
$row->{'category_' . $col} = $obj->$col();
}
$row->{category_pixel_depth} = $depth;
$row->{category_label} = ("    " x $depth) . $row->{category_label};
$row->{category_is_selected} = 1
if $cat_id && $cat_id == $obj->id;
push @data, $row;
}
}
#@data = sort { $a->{category_label} cmp $b->{category_label} }
# @data;
my $top = { category_id => '',
category_label => $app->translate('Select') };
$top->{category_is_selected} = 1 unless $cat_id;
unshift @data, $top;
$param{category_loop} = \@data;

## Now load user's preferences and customization for new/edit
## entry page.

Save CMS.pm and upload it. If you followed the previous version of this tutorial that handled this with CSS, copy over the line in red above. Thanks to Scott for the solution.

23 Comments

Dan Wolfgang said:
on Jan 5, 2005 8:04 PM | Reply

Very cool! Would it also be possible to hack this so that things sort alphabetically, regardless of case? (As-is, items are sorted by uppercase then lowercase.)

Scott Houchin said:
on Jan 5, 2005 10:59 PM | Reply

I tried this, but found that it didn't work in IE 6 on Windows. It worked on Firefox so I'm assumeing that Windows doesn't support the margins in the option tag.

I got around this by inserting non-breaking spaces in the option tag contents instead of styling it:

$row->{categorypixeldepth} = $depth; $row->{categorylabel} = ("    " x $depth) . $row->{categorylabel};

(that's four non-breaking spaces in that string)

With this solution, I didn't need to modify edit_entry.tmpl

Daniel said:
on Jan 6, 2005 1:49 PM | Reply

Hi Arvind, this looks like a pretty good improvement to me. I am using loads of categories and subcats and it would be much easiner find the right one if the weren't just sorted alphabetically but after parent categories. So I tried your method, but it won't sort the list right. I have one item intended which is following its parent by chance. I am running MT 3.14

Arvind Satyanarayan said:
on Jan 6, 2005 2:02 PM | Reply

That's really odd, the code above is just a copy of code used elsewhere in MT that does the category sorting on the list categories page. Can you send me an email using the Ask Arvind link at the top of the page and I"ll have a look at it and see what's wrong

Aine said:
on Jan 9, 2005 4:06 PM | Reply

In v.3.14 the code is changed (and the lines above are no longer as shown) and this hack is no longer needed. :)

Arvind Satyanarayan said:
on Jan 9, 2005 4:34 PM | Reply

What do you mean Aine. I haven't upgraded to MT 3.14 as yet but I didn't know they had implemented the staggered category list on the entry edit screen.

NathanNewman said:
on Jan 13, 2005 11:10 PM | Reply

Do you have the code to modify the "assign multiple categories" menu to list those categories in staggered fashion?

Jonas Bergenudd said:
on Jan 21, 2005 1:14 AM | Reply

Great hack!

My Perl-fu is not as good as yours so I was wondering if you could look into doing the same for the multiple category popup window?

It would be great to have the same sorting there.

regards, Jonas

Beth said:
on Jan 24, 2005 6:25 AM | Reply

They haven't sorted the primary categories in MT 3.14.....

Mark Carey said:
on Jan 24, 2005 4:56 PM | Reply

Great hack, Arvind.

I haven't implemented this yet, but will a future version of this hack create a staggered category list view in the "Assign multiple categories" pop-up tool as well? (hint, hint) :)

Kevin Donahue said:
on Feb 16, 2005 9:14 PM | Reply

This is AWESOME and works great in MT 3.14. My question is how to take it one step further & stagger the ADDITIONAL CATEGORIES window?

Arvind Satyanarayan said:
on Feb 20, 2005 7:29 PM | Reply

I'll look into adding this functionality to the additional categories popup window as soon as I get a free minute ;)

NathanNewman said:
on Mar 12, 2005 4:14 PM | Reply

Arvind-- Just a bump for adding the staggered list for the multiple categories window. I love this hack and, with a new project I'm putting together, desperately need the additional functionality on the multiple categories window. I'm planning to use a lot of categories with multiple categories assigned to each post, so this would be extremely useful.

Thanks for your great work!

Arvind Satyanarayan said:
on Mar 12, 2005 4:26 PM | Reply

I'll begin working on that hack as soon as I can. I need to think about how to handle it because the way I do it in the drop down list won't work. At the moment I'm thinking something similar to

Parent > Sub > Sub

Anna said:
on May 8, 2005 3:31 AM | Reply

This is great! Also would be useful to make the same staggered list show up on the "Add A Category" popup window. Specifically, the dropdown where you have to select the "Parent Category".

Anna said:
on May 8, 2005 3:33 AM | Reply

Also, one more thought. Is there a way to alphabetize the categories & subcategories in the staggered list?

Anna said:
on May 12, 2005 10:44 PM | Reply

Just checking back to see if there is a way to alphabetize the categories & subcategories in the staggered list? Any help would be great.

Thanks, Anna

jtflr ydgspx said:
on Sep 7, 2008 11:12 AM | Reply

hdcnq vmcsn eroyzwa jirmvk zhdsirk jrhmbw mzkextyof

menopause avodart said:
on Sep 10, 2008 5:25 PM | Reply

mlqks hlcwket

pack said:
on Sep 19, 2008 5:02 AM | Reply

cvwdj

emsam said:
on Sep 28, 2008 12:37 AM | Reply

fkae jhedvxy

Leave a comment

Preview