We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Suggestions, Runway

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 http://? 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 http:// 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?

Paul Bell

Paul Bell 0 points

  • 4 years ago

For the http:// you could use replace

https://docs.grabaperch.com/docs/templates/attributes/replace/

replace="http://|"

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 http:// 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.

<a href="http://<perch:content type="text" label="Link" id="link" replace="http://|" />">link</a>

True - thanks. I can probably work something around this.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

<?php
  // Basically a text field that automatically adds http:// to URLs that don't have it.
  class PerchFieldType_link extends PerchAPI_FieldType
  {
    public function get_raw($post=false, $Item=false) {
        $id = $this->Tag->id();

        if ($post===false) {
            $post = $_POST;
        }

        $url = '';

        if (isset($post[$id])) {
            $this->raw_item = trim($post[$id]);
            $url = $this->raw_item;

            // Things we want to remove from the URL - spaces, mis-typed stuff. Anything else the user might mistakenly enter.
            $search = array('http//','https//', ' ', '$');

            $url = strtolower(str_replace($search, '', $url));

            // http:// or https:// missing? Let's add it in here.            
            if(substr($url,0,7) !== 'http://' && substr($url,0,8) !== 'https://')
            {
                $url = 'http://' . $url;                
            }
        }

        return $url;
    }
  }
?>

Here's my updated code for anyone interested - fixed an issue where an empty field would have http:// added. Also now copes with mailto: links.

<?php
  // Basically a text field that automatically adds http:// to URLs that don't have it.
  class PerchFieldType_link extends PerchAPI_FieldType
  {
    public function get_raw($post=false, $Item=false) {
        $id = $this->Tag->id();

        if ($post===false) {
            $post = $_POST;
        }

        $url = '';

        if (isset($post[$id]) && $post[$id] !== '') {
            $this->raw_item = trim($post[$id]);
            $url = $this->raw_item;

            if($url == 'http://' || $url == 'https://' || $url == 'mailto:')
            {
                $url = '';
            }

            // Things we want to remove from the URL - spaces, mis-typed stuff. Anything else the user might mistakenly enter.
            $search = array('http//','https//', ' ', '$');

            $url = strtolower(str_replace($search, '', $url));

            // http:// or https:// missing? Let's add it in here.            
            if(substr($url,0,7) !== 'http://' && substr($url,0,8) !== 'https://' && substr($url,0,7) !== 'mailto:')
            {
                $url = 'http://' . $url;                
            }
        }

        return $url;
    }
  }
?>

Note from everyday experience:

Change

$url = strtolower(str_replace($search, '', $url));

into

$url = str_replace($search, '', $url);

-> don't do strtolower, as there ARE case sensitive, uppercase URLs out there