Andiana
Andiana

Reputation: 1952

Joomla 3 replace text plugin using preg replace not working

I am following a tutorial about joomla 3 extension development. I am using Joomla 3.2.4 I have a plugin name clicktocall, which to make all phone number text displayed as a link. Phone number format is XXXX-XXXX or XXXX XXXX, X is digit. and I want display any phone number as "> The method is using pattern as replace any text match the pattern by link tag

I installed, enabled the plugin I do it after a tutorial in an ebook, in the book everything are so smoothly, but in my site, after I view an article which have phone number text, there are nothing happen. The plugin not working.

My code: clicktocall.php

defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

class plgContentClicktocall extends JPlugin {

function plgContentClicktocall(&$subject, $params) {
    parent::__construct($subject, $params);
}

public function onContentPrepare($context, &$row, &$params, $page = 0) {
    //don't run this when the content is indexing
    if ($context == 'com_finder.indexer') {
        return true;
    }
    if (is_object($row)) {
        echo $row->text;
        return $this->clickToCall($row->text, $params);
    }
    return $this->clickToCall($row, $params);
}

protected function clickToCall(&$text, &$params) {
    // matches 4 numbers followed by an optional hyphen or space, 
    // then followed by 4 numbers.
    // phone number is in the form XXXX-XXXX or XXXX XXXX
    $pattern = '/(\W[0-9]{4})-? ?(\W[0-9]{4})/';
    $replacement = '<a href="tel:$1$2">$1$2</a>';
    $text = preg_replace($pattern, $replacement, $text);
    return true;
}

}

clicktocall.xml

<?xml version="1.0" encoding="UTF-8"?>
<extension
version="3.0"
type="plugin"
group="content"
method="upgrade">
<name>Content - Click To Call</name>
<author>Tim Plummer</author>
<creationDate>April 2013</creationDate>
<copyright>Copyright (C) 2013 Packt Publishing. All rights  
reserved.</copyright>
<license> http://www.gnu.org/licenses/gpl-3.0.html</license>
<authorEmail>[email protected]</authorEmail>
<authorUrl>http://packtpub.com</authorUrl>
<version>1.0.0</version>
<description>This plugin will replace phone numbers with click  
to call links. Requires Joomla! 3.0 or greater.
Don't forget to publish this plugin!
</description>
<files>
<filename plugin="clicktocall">clicktocall.php</filename>
<filename>index.html</filename>
</files>
</extension>

index.html : blank tags only

Sorry for the XML, I try for 10 minutes to make it pre-formatted but seem to be useless, but I confirm it's OK, included all files in my plugin

Upvotes: 2

Views: 894

Answers (1)

Brian Bolli
Brian Bolli

Reputation: 1873

I believe the issue is you are returning the value from your click2Call() method inside your onContentPrepare() method. Try reformatting like so:

public function onContentPrepare($context, &$row, &$params, $page = 0) {
    //don't run this when the content is indexing
    if ($context == 'com_finder.indexer') {
        return true;
    }
    if (is_object($row)) {
        echo $row->text;
        $this->clickToCall($row->text, $params);
    } else {
        $this->clickToCall($row, $params);
    }

    return true;
}

Since the row variable is referenced, any changes you make to the row data you're making to the actual data. Therefor, no need to return any data outside of the the return true at the end of the method.

Upvotes: 2

Related Questions