9

The website I'm working on has different sections and each section different types of entries. I would like the types of entries to be shown in the URL. I have followed this tutorial to do so.

In a site example.com where we have:

  • A Section called: My Section
  • With Entry Types: Example Entry Type, Another Entry type.

Utilizing the model from the tutorial, we could structure the URL for My Section as follows:

/my-section/{type}/{slug}

Which would give us URLs like these:

  • example.com/my-section/exampleEntryType/new-entry-1
  • example.com/my-section/exampleEntryType/new-entry-2
  • example.com/my-section/anotherEntryType/new-entry-3
  • example.com/my-section/anotherEntryType/new-entry-4

Notice that the entry types are the only part of the URL camelCased. This is because Entry Types can only be configured to have a Name, and a Handle; and handles can NOT have dashes. This interferes with keeping URLs consistent as the rest of the site uses dashes instead of spaces

The problem I am trying to solve is: how do we get the Entry Types to stay consistent with the rest of the site?

Does anyone have any suggestion on how to deal with this? Is this a problem that should be addressed in Craft? Should I be taking another approach for this?

I had considered using categories but the way the system is setup, entry types seem more appropriate.

3 Answers 3

10

You could just set your "Entry URL Format" to:

my-section/{type.name}/{slug}

Your entry type names couldn't be as clean when composing entries in the CP as you'd need to set them to something like "example-entry-type", but it would work.


Now for the really clean way:

Install the Low Regex for Craft plugin.

Set "Entry URL Format" to:

my-section/{type.handle|regex('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1-\\2" : "\\2")',(type.handle))}/{slug}

This will convert the entry type handle from camelCase to all lowercase with dashes as word delimiters. Set the entry type handles to be like "exampleEntryType" and they will become "example-entry-type" when in a slug.

3
  • Ahhh, I overlooked the usefulness of that plugin. I think I will go for that. It's a shame there's no native way of doing it but for now this is just as good. Thanks a lot Simon!
    – cballenar
    Commented Jul 7, 2014 at 0:07
  • No problem, glad it'll work for you! Commented Jul 7, 2014 at 2:04
  • 1
    I believe this is native now. Something like this may for for you. my-section/{ type.handle|replace('/(^|[a-z])([A-Z])/', '\\1-\\2')|lower }/{slug}. Commented Feb 26, 2015 at 16:17
3

You can change the slug for the entry type to be whatever you want it to be. The one generated by Craft with the -'s is a default, not a requirement. Also there is a config setting, slugWordSeparator to control the default word separator.

2
  • From what I'm seeing now, entry types (not each entry, but the group of entry types) only have a 'name' and a 'handle'. This can be seen in the following screenshot. dropbox.com/s/yirnzrc7dn7jfqn/…
    – cballenar
    Commented Jul 6, 2014 at 17:45
  • entry type's don't have slugs and the handle cannot have dashes, although the name can.
    – Ben Croker
    Commented Jul 7, 2014 at 10:59
3

You can use twig filters in url structures as mentioned by RhealPoirier in a comment above, no need for a plugin:

my-section/{type.handle|replace('/(^|[a-z])([A-Z])/', '\\1-\\2')|lower}/{slug}

or

my-section/{type.name|kebab}/{slug}

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.