Changing /taxonomyName/vocabulary to /vocabulary

Topics: Customizing Orchard
Dec 27, 2011 at 10:04 PM

Hello:

In Orchard 1.3.9 when you create a Taxonomy, it inserts itself into the root element for routing for all of its vocabulary terms.  For example, a taxonomy named "Section" could have a root vocabulary of "Lifestyle".  And within Lifestyle, it could have sub-vocabulary term of "Philosophy".  The url generated is:

/section/lifestyle/philosophy

How do I get rid of "/Section"?  The content part I am using is the default Blog Post type, if that helps.  I suspect this may have something to do with Url Alternatives.  But I am not finding much documentation on that.

You may ask why not call the Taxonomy "LifeStyle".  The reason is that you cannot browse/view a list of articles under the "Taxonomy" name.  You can only navigate via the Vocabulary terms.  At least, from what I can see (because that is exatly what I first tried).

The goal is to have articles show up under /lifestyle as well as /lifestyle/philosophy.  The "section" portion is just noise.

Thanks in advance!

Coordinator
Dec 27, 2011 at 11:17 PM

Add your own route. (it has nothing to do with alternates)

Dec 28, 2011 at 1:42 AM
bertrandleroy wrote:

Add your own route. (it has nothing to do with alternates)


Thank you.  And I see you are one of the authors of the Contrib.Taxonomies module.

I've tried looking for any documentation on Routes to no end, and lots of searches.  Just not seeing how to do this in the admin.

I've installed the BDN Route Debug module, but it seems to only be a debugging tool - as read-only.

Are you saying I have to modify the Contrib.Taxonomies module to "add a route" as you are suggesting?

Or, is there a way to do this in the admin that I am just not seeing in any Content Parts or Type.

Thanks and I appreciate your help.  New to Orchard.

Coordinator
Dec 28, 2011 at 1:47 AM

You can;t do it in the admin. Look at any routes.cs file in the project for examples

Do not modify taxonomies. Add your own module instead.

Dec 28, 2011 at 6:30 PM
bertrandleroy wrote:

You can;t do it in the admin. Look at any routes.cs file in the project for examples

Do not modify taxonomies. Add your own module instead.


Thanks again. 

I looked at the routes.cs in the Taxonomies module, and it seems it is already registering routes from the root of the url (just as I prefer above).  Specifically the code is:

new RouteDescriptor {
   Priority = 11,
   Route = new Route(
           "{*termPath}",
           new RouteValueDictionary {
                                       {"area", "Contrib.Taxonomies"},
                                       {"controller", "Home"},
                                       {"action", "Item"},
                                       {"termPath", ""}
                                   },
           new RouteValueDictionary {
                                       {"termPath", _termPathConstraint}
                                   },
           new RouteValueDictionary {
                                       {"area", "Contrib.Taxonomies"}
                                   },
           new MvcRouteHandler())
   }

This captures any url obviously, which would mean my adding of a route from the root would be moot at this point.

Digging into the code, this route passes the entire url (e.g. "/section/lifestyle/philosophy") that is looked up in a directionary within the HomeController.Item() action method.

This dictionary contains the complete set of Urls (slugs) for all taxonomies, including the Taxonomy "Section" name.  The list looks like:

"/section/lifestyle/philosophy"
"/section/lifestyle/hobbies"
"/section/lifestyle"

Notice that the root element of "/section/" is hardcoded into this path.

Therefore, any attempt by me to create a route, for say '/lifestyle/philosophy', would not be picked up.  Unless I create a controller that calls into this HomeController.Item(), but that seems quite messy.

It would seem the Taxonomy module limits the ability to do what I am wanting to do by hardcoding the complete paths from the IRoutabeAspect part of the TermPart.

So, how would I go about changing this root Paths list to remove the first portion?  It seem that is not possible without modifying the Contrib.Taxonomies module directly, which I am trying to avoid obviously.  :)

Thanks!

 

 

Coordinator
Dec 28, 2011 at 9:48 PM

If implementing your own controller action is the only way to make this work, then I don't see this as particularly messy: you are introducing different behavior, so it seems justified.

Dec 29, 2011 at 1:05 AM
Edited Dec 29, 2011 at 1:09 AM
bertrandleroy wrote:

If implementing your own controller action is the only way to make this work, then I don't see this as particularly messy: you are introducing different behavior, so it seems justified.


Thanks again.  What I meant by my own controller is my controller would have to call into your controller. That's the messy part, as I would have to instantiate your controller somehow (e.g. like how RenderAction() works):

public class CustomTaxonomyRouteController : Controller
{
  public ActionResult Item(string termPath, PagerParameters pagerParameters)
  {
    // get Contrib.Taxonomies.Controllers.HomeController
    var taxonomyHomeController = ...

    // prefix our current termPath so it matches a Taxonomy route
    var modifiedTermPath = String.Format("section/{0}", termPath);

    // pass the modified path into the Taxonomy controller for normal processing,
    // since now we pass it's internal lookup Dictionarys
    return taxonomyHomeController.Item(modifiedTermPath, pagerParameters);
  }
}
That's what I was trying to avoid.  Yes, i could make the section/{0} portion more 
dynamic from some admin interface. I shortened the code to make it easier.
Coordinator
Dec 29, 2011 at 1:19 AM

No, you should call into ITaxonomyService instead. You don't need the existing controller at all.