Articles Anywhere

How to transform custom phone field into URL?

1 month 1 week ago #90092

TVBZ's Avatar TVBZ

Hi,

I am using a custom field to add some phone number to a contact article.
When rendering this field in FO, I want to make this into a link: href="tel:xxx".

I tried using Sourcerer and some php. But I can't get it to work.
This is what needs to happen:

1. Remove spaces
2. Remove first 0
3. Add prefix to result ("tel:" + countrycode)

The code below is not removing the spaces:

{article}
{source 0}
  <?php
    $prefix = 'tel:+32';
    $phone = '[phone]';
    $phone = str_replace(' ', '' , $phone);
    $phone = preg_replace('/\s+/', '', $phone);
    $phone = $prefix . substr($phone, 1);
   ?>
{/source}
<a href="tel:{source 0}<?php echo $phone; ?>{/source}">[phone]</a>
{/article}

There are 2 problems with this code:
1. As you can see, I tried both str_replace() and preg_replace(). In the end, the spaces are still there.
2. When using substr(), the $phone variable is no longer rendered. Now it renders [phone]. When removing the subsrt(), the [phone] field renders as expected.

Can anyone help me to get the desired result? I am a php noob. 😡

Many thanks

1 month 1 week ago #90095

TVBZ's Avatar TVBZ

I tried converting the custom field string into array to work with. Also tried to first make a copy from custom field variable output before working with it, .. no luck.

The custom field value renders fine as a string. But as soon as I try to do anything with it, it renders back to the custom field tag "[fieldname]", instead of the custom field value.

Let's say, as an example, the value in the phone field is 0956:

$phone = [phone];
echo $phone;
Output = 0956, as expected.

Now I try to remove first char "0":
$phone = substr($phone, 1);
echo $phone;
Output = phone] .. Why? I don't understand 😐


So now I am thinking I can't remove spaces because there are no spaces in the custom field tag. Because the spaces are in the rendered value of the custom field tag. Makes sence?

1 month 1 week ago #90096

TVBZ's Avatar TVBZ

Ok.. Allmost there. The issue was fixed by sorting the sourcerer system plugin after the articles anywhere system plugin.
But now I have some new issue rendering the result.

This code works fine:

{article}
{source 0}
  <?php
    $prefix = 'tel:+32';
    $phone = '[phone]';
    $phone = str_replace(' ', '' , $phone);
    $phone = $prefix . substr($phone, 1);
   ?>
{/source}
<div>{source 0}<?php echo $phone ?>{/source}</div>
{/article}

It renders what I could use inside a href="..." tag.
But for some reason, I can't use it inside a href tag.

While it renders perfectly inside a div, replacing the div with this link doesn't work:
<a href="{source 0}<?php echo $phone ?>{/source}">Call now</a>

This results in <a href="/{source 0}<?php echo $phone ?>%7B/source%7D"></a>

1 month 1 week ago #90097

TVBZ's Avatar TVBZ

Okay.. I figured it out! 🙂
It seems sourcerer has some trouble rendering inside a href="" tag.

The solution was to create the complete html tag in php.
I made it as a reusable php snippet. Because I have both phone and mobile phone fields.
This way, the function itself is only needed 1 time.


WORKING SOLUTION:

First: We need a reusable function:

{source 0}
  <?php
    function getPhoneUrl($phone) {
      $prefix = 'tel:+32';
      $target = str_replace(' ', '' , $phone);
      $target = $prefix . substr($target, 1);
      $html = "<a title='Call " . $phone . "' href='" . $target . "'>" . $phone . "</a>";
      return $html;
    }
   ?>
{/source}

Next, we render the html <a> tag by calling the function and using the ArticlesAnywhere custom field as a parameter.
We can do this as many times, for as many fields as we wish. Here is my example for phone and mobile phone:
{if phone}
   {source 0}<?php echo getPhoneUrl('[phone]'); ?>{/source}
{/if}
{if mobilephone}
  {source 0}<?php echo getPhoneUrl('[mobilephone]'); ?>{/source}
{/if}

IMPORTANT NOTE: Keep in mind this is all inside the {article}...{/article} tag from ArticlesAnywhere.

I hope others can use it. 😉 👍


Any suggestions to improve are most welcome!

1 month 1 week ago #90103

Peter van Westen's Avatar Peter van Westen Admin

Indeed, echoing out the complete html tag is the most stable solution.

You could place the function inside an external php file too, and include it like:

{source}<?php require_once JPATH_SITE . '/my_scripts/functions.php'; ?>{/source}

And a small refactor for the function
function getPhoneUrl($phone) {
    $prefix = 'tel:+32';
    $target = str_replace(' ', '' , $phone);
    $target = $prefix . ltrim($target, '0');

    return '<a title="Call ' . $phone . '" href="' . $target . '">' . $phone . '</a>';
}

1 month 1 week ago #90113

TVBZ's Avatar TVBZ

Hi Peter, thanks for the input.
I will indeed put the function in an external php file, to keep my module code a bit cleaner. 🙂

Is there something like JPATH_TEMPLATE I could use instead of JPATH_SITE?
So it points to active template folder instead of root?

Cheers! 😁 👍

1 month 1 week ago #90114

Peter van Westen's Avatar Peter van Westen Admin

You could do:

{source}<?php
$template = Factory::getApplication()->getTemplate();
require_once JPATH_THEMES . '/' . $template . '/my_scripts/functions.php'; 
?>{/source}

1 month 1 week ago #90116

TVBZ's Avatar TVBZ

Peter van Westen wrote:

You could do:
{source}<?php
$template = Factory::getApplication()->getTemplate();
require_once JPATH_THEMES . '/' . $template . '/my_scripts/functions.php'; 
?>{/source}

I see. Thanks again! 👍