Hi everybody,
I'm using a template to add and remove secondary categories from a
story, and I've hit a snag.
Here's how it's supposed to work:
1. User chooses secondary categories from a code select, which offers a
list based on the primary category of the story. For example, in the
primary category "/baking/", the code select shows:
Yeast (Which points to /z-secondary/baking/yeast/)
Flour (Which points to /z-secondary/baking/flour/)
The names of the secondaries all start with z to keep them out of the
way in the UI.
2. When the story is previewed or published, if any of the secondary
categories chosen in the code select are not already associated with the
story, they are added.
3. Next, I want to remove any secondary categories that are present in
the story, but not picked in the code select. This seems to kind of sort
of work, but not really.
For example, if I choose "Flour" and deselect "Yeast," Bricolage throws
this error on preview:
Category '/z-secondary/baking/yeast/' not associated with story
'Delicious Flour'
This error is from line 1272 of Story.pm, in the get_uri method.
So it looks like the category is getting deleted, but it's not
registering everywhere it needs to before get_uri gets called.
Does anybody know if there's a callback I should be calling after the
category deletion?
Here's the template code:
my @desired_cat_uris = $code_select->get_values;
my @current_cat_uris = map ($_->get_uri,
$story->get_secondary_categories);
# Add the desired categories
foreach my $cat_to_possibly_add (@desired_cat_uris) {
if (my $cat_obj = Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_add})) {
$story->add_categories([$cat_obj]) unless grep ({$_ eq
$cat_to_possibly_add} @current_cat_uris);
$story->save;
}
}
# Remove all secondary categories
foreach my $cat_to_possibly_remove (@current_cat_uris) {
if (substr($cat_to_possibly_remove,1,1) eq 'z-secondary') {
$the_story->delete_categories([Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_remove})]) unless grep ({$_ eq $cat_to_possibly_remove}
@desired_cat_uris);
$story->save;
}
}
Thanks so much,
Bret
--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com
I'm using a template to add and remove secondary categories from a
story, and I've hit a snag.
Here's how it's supposed to work:
1. User chooses secondary categories from a code select, which offers a
list based on the primary category of the story. For example, in the
primary category "/baking/", the code select shows:
Yeast (Which points to /z-secondary/baking/yeast/)
Flour (Which points to /z-secondary/baking/flour/)
The names of the secondaries all start with z to keep them out of the
way in the UI.
2. When the story is previewed or published, if any of the secondary
categories chosen in the code select are not already associated with the
story, they are added.
3. Next, I want to remove any secondary categories that are present in
the story, but not picked in the code select. This seems to kind of sort
of work, but not really.
For example, if I choose "Flour" and deselect "Yeast," Bricolage throws
this error on preview:
Category '/z-secondary/baking/yeast/' not associated with story
'Delicious Flour'
This error is from line 1272 of Story.pm, in the get_uri method.
So it looks like the category is getting deleted, but it's not
registering everywhere it needs to before get_uri gets called.
Does anybody know if there's a callback I should be calling after the
category deletion?
Here's the template code:
my @desired_cat_uris = $code_select->get_values;
my @current_cat_uris = map ($_->get_uri,
$story->get_secondary_categories);
# Add the desired categories
foreach my $cat_to_possibly_add (@desired_cat_uris) {
if (my $cat_obj = Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_add})) {
$story->add_categories([$cat_obj]) unless grep ({$_ eq
$cat_to_possibly_add} @current_cat_uris);
$story->save;
}
}
# Remove all secondary categories
foreach my $cat_to_possibly_remove (@current_cat_uris) {
if (substr($cat_to_possibly_remove,1,1) eq 'z-secondary') {
$the_story->delete_categories([Bric::Biz::Category->lookup({'uri' =>
$cat_to_possibly_remove})]) unless grep ({$_ eq $cat_to_possibly_remove}
@desired_cat_uris);
$story->save;
}
}
Thanks so much,
Bret
--
Bret Dawson
Producer
Pectopah Productions Inc.
(416) 895-7635
bret@pectopah.com
www.pectopah.com