1

I have a classifieds site where I would like to list items at a URL structure like this:

classifieds/for-rent/apartments/title-of-my-ad.

That structure is broken down like this (views-page)/(content-type-argument)/(taxonomy-term-arg)/(node-title)

What I want to happen is this: If someone removes then all nodes will be listed that are apartments, which were created with the 'for rent' content type; If someone remove apartments (the tax arg) then all 'for rent' items, regardless of taxonomy, are listed.

See what I am getting at?

My problem is that the content type argument has to be written with an underscore instead of a hyphen before it will work. I want to use a hyphen. It's not as ugly, plus Google specifically recommends hyphens in the url instead of an underscore, and I want to keep google happy.

so, given the above example,

classifieds/for_rent/etc/etc <= works fine.

classifieds/for-rent/etc/etc <= does not work.

How do I use the hypenated form as the argument? Is there a way to do this in views? Is there a way to write something in the .htaccess file so that when an argument is received with a hyphen the url is passed with an underscore behind the scenes?

Thanks!

2
  • How many content types do you have?
    – Jimajamma
    Commented Jun 20, 2012 at 14:37
  • 6 content types. each represents a category of classifieds. For sale, For rent, Personals, etc. The reason these are separate is because of the unique fields needed for each type. For example, bedrooms and bath are needed for the real estate section where mileage, car make and model are needed for the autos content type.
    – blue928
    Commented Jun 22, 2012 at 5:13

1 Answer 1

1

If you have only six types, the quickest solution may be to create 6 page displays for your view, eg, one might be:

classifieds/for-rent/%/%

and then override the filter on this display and add node type for_rent. Then create another page view, say:

classifieds/for-sale/%/%

and filter that page display on node of type for_sale, another:

classifieds/personals/%/%

etc for personals.

ADDITION:

You could create a menu item and do this yourself, eg:

function YOURMODULE_menu() {

  $items=array();

  // any other menu items here

  $items['classifieds/%/%/%'] = array(
    'title' => 'YOUR TITLE',
    'page callback' => 'YOURMODULE_classifieds',
    'page arguments' => array(1,2,3),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function YOURMODULE_classifieds($type, $tax, $title) {

  $type=str_replace('-', '_', $type);

  return views_embed_view(NAME_OF_YOUR_VIEW, NAME_OF_DISPLAY, $type, $tax, $title);

}

Now, this was just typed in off the top of my head so there may be subtle issues here, but the basic point is to create a menu item that responds to:

classifieds/SOMETHINGWITHADASHINIT/bar/baz

that gets the dash turned into an underscore for Drupal's internal name of the type. So, you could "just" create a block display in your view and remove all of View's paths and call it manually like the above. That, or ask that Views have a "transform underscores into dashes" checkbox like the "transform spaces to dashes" it has for taxonomy terms (which it might already have in D7 :)

2
  • Yea, that is exactly the solution I have now, and it works great. Managing these while in development is getting a little tedious, so was hoping to find a way to switch that. Wanted to cut down on the overhead a bit. Plus there is room for more content types in the future as categories grow. Thanks for a great answer though. I was thinking that the .htaccess file would be the way to go. Pathauto rewrites the url so that there is a hyphen in the content type. I was wondering if I could switch that out to an underscore before the link is sent to PHP from apache for parsing. Thoughts?
    – blue928
    Commented Jun 23, 2012 at 8:56
  • While I have been called out too many times to count when I pronounce things with respect to D7, but I would be highly surprised if pathauto is involved here at all, rather, the MACHINE name of your content type probably does have _'s in its name instead of -'s, so what you really need is for Views to have a checkbox much like "translate spaces to dashes" like it does with taxonomy terms for example, but in this regard, "transform underscores to dashes." Since it doesn't, at least here in Views 3 D6 land, let me add another option above :)
    – Jimajamma
    Commented Jun 23, 2012 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.