2

I'm not sure I've seen this exact question, but I'm curious how I can include custom and/or auto-generated parts of a slug. It's not the URI creation, but the slug itself.

For Instance:

{slug}-{id} would turn into lorem-ipsum-dolor-sit-amet-123456 on submit

and from there on the variable {{entry.url}} would be example.com/lorem-ipsum-dolor-sit-amet-123456.

I know ExpressionEngine had an option in the Channel preferences called URL Title Prefix, so this would go along those lines, but more auto-generated than a predefined set of words.

If it changes things, this is specifically for Commerce and Digital Products at the moment.

  • You can do so with Craft's Dynamic Entry Titles – AbbeyDesign Jun 15 '17 at 18:54
  • I don't necessarily want the title to be dynamic. The title should be able to be populated by the user. But upon submit, or even right away when the create entry page loads, the slug should be prepended or appended by the ID. @Brad I don't know how that kebab filter helps? – John Johnson Jun 15 '17 at 20:30
  • Dynamic Entry Titles are dynamic in the sense saving an entry would create a slug depending on any combination of fields you want. You can set your title field to be {entry.user-entered-slug}-{entry.id} or {entry.fieldA}-{entry.fieldB}-{entry.postDate}-some-text-{entry.id} etc etc. – AbbeyDesign Jun 16 '17 at 12:56
  • OK, I think I'm following. I could create a psuedo title field and behind the scenes it's populating the actual title field with the pertinent info I need. I'll try this... – John Johnson Jun 16 '17 at 13:26
1

I think for this you'd want to write a custom plugin that listened to the onSaveEntry event.

That plugin would check that the given entry is in the correct section/entry type you're interested in, pull any data from custom fields set on the entry update the slug for that entry accordingly.

I'd probably look at updating the slug directly in the database and avoid going back through craft()->entries->saveEntry() to avoid getting into an infinite save entry loop.

| improve this answer | |
  • Old answer, I know, but is this something I could do easily enough with a module? - trying my hand at learning how to do it. – Jay Jun 21 '19 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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