Forum
Fieldtype for URLs?
I'm making a list of links in Perch Runway. I'd really like to be able to do some checks that the user has pasted in a URL correctly. Does it start with https://? Does it contain spaces? Maybe even does it return a 404 (though I'm less worried about that). Is this a good case for a Fieldtype?
Or perhaps a similar thing could be achieved with tags in my template? For example, how would I test for "does this start with https:// or https://"? (If not, I could add this myself).
Is there a way to replace something in templates? So I could remove any spaces at the start or end of the field, for example (or does that happen automatically anyway?
For the https:// you could use
replace
https://docs.grabaperch.com/docs/templates/attributes/replace/
You can add multiple by separating them with commas
Thanks, Dexter - I can see that could be helpful. It doesn't quite do what I need though. I don't want to remove the https:// or https:// - I just want to add it if it's not there. Can see that the replace would be useful sometimes though.
You could structure your template with it though if you always wanted to add it.
True - thanks. I can probably work something around this.
I would just build the field type - they're pretty straightforward.
OK, thanks Drew - yes, having looked at a Fieldtype, that does look do-able. Where would I find the standard text field type as a starting point? Or am I better to use the Pagelist as a starting point?
The standard text field is the default type. Everything else basically extends it - so you only need to do what's different.
Thanks, Drew - for those that are interested, here's what I have - seems to work pretty well to tidy up a link.
Here's my updated code for anyone interested - fixed an issue where an empty field would have https:// added. Also now copes with mailto: links.
Note from everyday experience:
Change
into
-> don't do strtolower, as there ARE case sensitive, uppercase URLs out there