Page 1 of 1
Products Email Field
Posted: Wed Oct 15, 2014 4:15 pm
by Barrowboy
Hi
Is it possible to create a field in products module that would recognise an email address?
The only way I have managed to do it at the moment is in a text field by adding the full ref.
<a href="mailto:
somebody@me.com">Joe Bloggs</a>
This works but you need some coding experience to keep adding.
What I was looking for is just to add the email address and the system adds the rest.
Any ideas you guys?
Thanks
Re: Products Email Field
Posted: Wed Oct 15, 2014 4:50 pm
by JohnnyB
You might use some regex matching on the field inside of the template. Then, if it is known to be an email address, wrap it in your mailto link.
You can use the regex with smarty / php or via javascript on the client side.
Re: Products Email Field
Posted: Wed Oct 15, 2014 5:29 pm
by paulbaker
Or try {mailto address="
asdf@asdf.com" encode="javascript"} which uses JS to try to hide email from spam harvesters.
Re: Products Email Field
Posted: Wed Oct 15, 2014 6:40 pm
by Barrowboy
Hi Power poster
I like this idea but were and how would it be used in relation to the field used i.e. Text Input.
Does it just go in the template?
Cheers.
Re: Products Email Field
Posted: Wed Oct 15, 2014 7:05 pm
by paulbaker
Not sure, try it and see

you may find the { code } is not parsed and so it's not a solution for you.
Re: Products Email Field
Posted: Wed Oct 15, 2014 8:28 pm
by velden
What about adding two normal text input fields and inside template build the link yourself? That would be the normal cmsms/template/smarty way I guess:
Assuming you add two fields: 'emailname' and 'emailaddress':
Code: Select all
<a href="mailto:{$entry->fields.emailaddress->value}">{$entry->fields.emailname->value}</a>
Re: Products Email Field
Posted: Wed Oct 15, 2014 8:33 pm
by calguy1000
{if is_email($entry->fields.emailaddress->value)}
<a href="mailto:{$entry->fields.emailaddress->value}">email the guy</a>
{else}
... something else ...
{/if}
Re: Products Email Field
Posted: Wed Oct 15, 2014 8:35 pm
by JohnnyB
Not tested.
You would look in the frontend template for the custom field you want to make an email address.
{assign var='email_regex' value='/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/'}
*** I might have the regex syntax wrong for the value='' but it is a common regex to find an email address....
{if $My.Custom.Products.Field.Value|preg_match:email_regex}
{mailto address=$My.Custom.Products.Field.Value encode=javascript}
{/if}
That is the basic logic. You'll need to supply your custom field value in there and like I said it is not tested and I may have some stuff wrong in there. But, that would be the idea to test if the field contains an email address and if so, make the value into a clickable email address on the page...
Re: Products Email Field
Posted: Wed Oct 15, 2014 8:39 pm
by JohnnyB
Oh Snap!
I didn't know about the is_email function.
Much easier than trying regex stuff.
Re: Products Email Field
Posted: Thu Oct 16, 2014 3:46 pm
by Barrowboy
Hi All
Thanks for all your help.
Not sure what was meant by not describing my problem thought I had done that in my first message.
Anyway I choose the idea of having two text inputs and used this in my template.
<span class="email">Email:-</span> {if is_email($entry->fields.emailaddress->value)}
<a href="mailto:{$entry->fields.emailaddress->value}"> {$entry->fields.emailname->value}</a>
{/if}
Works a treat. Having a second text input allows me to have a different name from the email i.e. Manager, Editor, Secretary etc.
Thanks again till the next time.
Re: Products Email Field
Posted: Thu Oct 16, 2014 4:23 pm
by JohnnyB
Not sure what was meant by not describing my problem thought I had done that in my first message.
.... That's just his default profile signature...
