Creating a custom plugin

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
waterboy
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jul 06, 2011 11:43 am

Creating a custom plugin

Post by waterboy » Mon Nov 03, 2014 8:09 pm

Hello,

My first step in developing a plugin for joomla. My plugin uses the helper.
i need to understand what i am doing wrong in calling my helper.
i think there is something wrong with my logic.

Here is the main code:

Code: Select all


//-- No direct access
defined('_JEXEC') or die;


jimport('joomla.plugin.plugin');

/**
 * Content Plugin.
 *
 * @package    JomSpintax
 * @subpackage Plugin
 */
class plgContentJomSpintax extends JPlugin
{
	private $callbackFunction;
     /**
	 * Define Constructor function 
	 */
	public function __construct(&$subject, $config = array())
	{
		/**
		* Run the parent Constructor
		* so we do not forget or ignor anything that is run by jplugin 
		*/
		parent::__construct($subject, $config);
		/**
		* Define Helper class
		*/
		require_once __DIR__ . '/helper/helper.php';
		/**
		* Instantiate helper class
		* $this->params contain all parameter define in the xml file		
		*/
		$helper = new SpinTaxHelper($this->params);
		$this->callbackFunction = array($helper, 'spintext');
	}
	/**
	 * Initiate the plugin
	 * @param  string $context The context of the content passes to the plugin
	 * @param  object $article The article object
	 * @param  object $params  The article params
	 *
	 * @return boolean         True if the function is bypassed. Else True/False based on the replacement action
	 */
	public function onContentBeforeDisplay($context, $article, $params)
	{
		/**
		* Fail safe check so it only run on content
		*/	
		$parts 	= explode(".", $context);
		if ($parts[0] != "com_content")
		{
			return;
		}

		if (stripos($article->text, '{spintext=off}') === true)
		{
			$article->text = str_ireplace('{spintext=off}', '', $article->text);
		}

		$app = JFactory::getApplication();
		$NoSpin = $app->input->get('external', NULL);

		if ($Nospin)
		{
			SpinTextHelper::SpinText($article, $external);
		} else 
		{
			$pattern = '/\{(((?>[^\{\}]+)|(?R))*)\}/x';
			$article->text = preg_replace_callback($pattern, $this->callbackFunction, $article->text);
		}

	}
}
And here is my Helper code

Code: Select all


//-- No direct access
defined('_JEXEC') || die('=;)')

class SpinTextHelper
	{
	/* 
	* init Helper
	*/
	public $params = null;

	public function __construct($params = null)
	{
		$this->params = $params;
	}
	/*
	* Function that does the spinning of the text
	*/
	public function SpinText($text)
	{
		return preg_replace_callback($pattern,array($this, 'replace'),$text);
	}

	public function replace($text)
	{
	$text = $this->process($text[1]);
	$parts = explode('|', $text);
	return $parts[array_rand($parts)];
	}
}



User avatar
sitesrus
Joomla! Ace
Joomla! Ace
Posts: 1469
Joined: Mon Nov 12, 2012 10:48 pm

Re: Creating a custom plugin

Post by sitesrus » Mon Nov 03, 2014 8:39 pm

Well if you are using a static class there's no need for a constructor unless you are calling new which you're not, so remove that. Also I think you need to declare your SpinText and replace functions as static to call them statically ie SpinTextHelper::SpinText()

Check your error logs, there's probably warnings/notices about your setup or errors too.
I like working with Joomla :). I offer the following professional services: Custom extension development, SEO/marketing, maintenance/support, security and WCAG audits, and will work on websites at a reasonable rate.

waterboy
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jul 06, 2011 11:43 am

Re: Creating a custom plugin

Post by waterboy » Mon Nov 03, 2014 9:35 pm

Thanks for the quick reply.

After a few fixs this is the error i get
trict Standards: Non-static method SpinTaxHelper::SpinText() should not be called statically, assuming $this from incompatible context in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\plugins\content\jomspintax\jomspintax.php on line 79

Warning: preg_replace_callback(): Requires argument 2, 'plgContentJomSpintax::replace', to be a valid callback in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\plugins\content\jomspintax\helper\helper.php on line 29
This is the new version of the code

Main:

Code: Select all

//-- No direct access
defined('_JEXEC') or die;


jimport('joomla.plugin.plugin');

/**
 * Content Plugin.
 *
 * @package    JomSpintax
 * @subpackage Plugin
 */
class plgContentJomSpintax extends JPlugin
{
	private $callbackFunction;
     /**
	 * Define Constructor function 
	 */
	public function __construct(&$subject, $config = array())
	{
		/**
		* Run the parent Constructor
		* so we do not forget or ignor anything that is run by jplugin 
		*/
		parent::__construct($subject, $config);
		/**
		* Define Helper class
		*/
		require_once __DIR__ . '/helper/helper.php';
		/**
		* Instantiate helper class
		* $this->params contain all parameter define in the xml file		
		*/
		$helper = new SpinTaxHelper($this->params);
		$this->callbackFunction = array($helper, 'SpinText');
	}
	/**
	 * Initiate the plugin
	 * @param  string $context The context of the content passes to the plugin
	 * @param  object $article The article object
	 * @param  object $params  The article params
	 *
	 * @return boolean         True if the function is bypassed. Else True/False based on the replacement action
	 */
	public function onContentBeforeDisplay($context, $article, $params)
	{
		
		/**
		* Fail safe check so it only run on content
		*/	
		$parts 	= explode(".", $context);
		if ($parts[0] != "com_content")
		{
			return;
		}

		if (stripos($article->text, '{spintext=off}') === true)
		{
			$article->text = str_ireplace('{spintext=off}', '', $article->text);
		}

		$app = JFactory::getApplication();
		$NoSpin = $app->input->get('external', NULL);

		if ($NoSpin)
		{
			SpinTaxHelper::SpinText($article, $external);
		} else 
		{
			SpinTaxHelper::SpinText($article);
		}

	}
this is the helper

Code: Select all


//-- No direct access
defined('_JEXEC') or die;

class SpinTaxHelper
{
	/* 
	* init Helper
	*/
	public $params = null;

	public function __construct($params = null)
	{
		$this->params = $params;
	}
	/*
	* Function that does the spinning of the text
	*/
	public function SpinText($text)
	{
		return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array($this, 'replace'),$text);
	}

	public function replace($text)
	{
	$text = $this->process($text[1]);
	$parts = explode('|', $text);
	return $parts[array_rand($parts)];
	}
}

I a real newbie, and i am following an example on the web substituting some code for what i want.

I do not know how to get rid of the helper?

waterboy
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jul 06, 2011 11:43 am

Re: Creating a custom plugin

Post by waterboy » Tue Nov 04, 2014 12:39 am

So in a effort to keep it Simple , i have strip the Helper .

This is what i have now

Code: Select all

<?php
/**
 * @package    JomSpintax
 * @subpackage Base
 * @author     Pierre McCarragher {@link http://divesitenet.com}
 * @author     Created on 02-Nov-2014
 * @license    GNU/GPL
 */

//-- No direct access
defined('_JEXEC') or die;


jimport('joomla.plugin.plugin');

/**
 * Content Plugin.
 *
 * @package    JomSpintax
 * @subpackage Plugin
 */
class plgContentJomSpintax extends JPlugin
{
    
     /**
     * 
     */
    public function process($text)
    {
        return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array($this, 'replace'),$text);
    }

    public function replace($text)
    {

        $text = $this->process($text[1]);
        $parts = explode('|', $text);
        return $parts[array_rand($parts)];
    }

    /**
     * Initiate the plugin
     * @param  string $context The context of the content passes to the plugin
     * @param  object $article The article object
     * @param  object $params  The article params
     *
     * @return boolean         True if the function is bypassed. Else True/False based on the replacement action
     */
    public function onContentBeforeDisplay($context, $article, $params)
    {
        
        /**
        * Fail safe check so it only run on content
        */    
        $parts     = explode(".", $context);
        if ($parts[0] != "com_content")
        {
            return;
        }
/*
        if (stripos($article->text, '{spintext=off}') === true)
        {
            $article->text = str_ireplace('{spintext=off}', '', $article->text);
        }
*/    
        $spintax = new plgContentJomSpintax();
        $spintax->process($article);
        
        }
}
This is the error it give me.

Warning: Missing argument 1 for JPlugin::__construct(), called in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\plugins\content\jomspintax\jomspintax.php on line 66 and defined in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\libraries\cms\plugin\plugin.php on line 63

Fatal error: Call to a member function attach() on a non-object in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\libraries\joomla\event\event.php on line 39
Can someone explain to me that this error says?

Thanks

User avatar
sitesrus
Joomla! Ace
Joomla! Ace
Posts: 1469
Joined: Mon Nov 12, 2012 10:48 pm

Re: Creating a custom plugin

Post by sitesrus » Tue Nov 04, 2014 2:03 pm

Okay, you do not need to get rid of the helper. You either have to use a class like an object and call new or use it statically by declaring the functions static, that's it.

So go back to what you had originally and REMOVE the __construct() function completely from the helper and add the static keyword to your functions in the helper,

public static blah($blah1,$blah2)

That's the only way you can call Helper::blah() statically, it needs to be static. This is basic OO stuff, a class object needs to be called using new and than you can use $this in the object but if it's static you can't use $this or access it statically without the static keyword.

So I told you how to fix it, and you went off and redesigned your code only to get more errors on the redesign. Just start over and make the small tweaks I suggested and you'll be done with it.

Your new code is even worse because your trying to instantiate a new plugin off the same plugin within the plugin object, your missing parameters. Joomla fires off the plugin and passes parameters to it, why you would create your own version of the same plugin in the plugin is odd. Go back to the helper, it was fine you just didn't understand the OO concepts behind the issues you had and I told you how to fix it.
I like working with Joomla :). I offer the following professional services: Custom extension development, SEO/marketing, maintenance/support, security and WCAG audits, and will work on websites at a reasonable rate.

waterboy
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jul 06, 2011 11:43 am

Re: Creating a custom plugin

Post by waterboy » Wed Nov 05, 2014 12:25 am

Then it should look like this?

Code: Select all

class SpinTaxHelper
{
	/*
	* Function that does the spinning of the text
	*/
	public static function SpinText($text)
	{
		return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array($this, 'replace'),$text);
	}

	public static function replace($text)
	{
		$text = $this->process($text[1]);
		$parts = explode('|', $text);
		return $parts[array_rand($parts)];
	}
}
I am sorry I really am very very Green at this.

Can you recommend a good online resource to understand OO?

User avatar
sitesrus
Joomla! Ace
Joomla! Ace
Posts: 1469
Joined: Mon Nov 12, 2012 10:48 pm

Re: Creating a custom plugin

Post by sitesrus » Wed Nov 05, 2014 7:14 pm

Close, however you have to fix the part of the code where you call $this, $this means you're in an object and getting $this (reference to the object). You're not doing that, your inside a static class and there is no object so you can't call $this.

Revise,

Code: Select all

return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array($this, 'replace'),$text);
to,

Code: Select all

return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array('SpinTaxHelper', 'replace'),$text);
The only difference is that you're referncing the static class/function instead of the object which doesn't exist.

Do you understand how what you have no is a static utility type class and not an object? And do you understand what $this means in the context of an object? You don't have an instance created with new using the class constructor is the main difference, one's an object and one's not.
I like working with Joomla :). I offer the following professional services: Custom extension development, SEO/marketing, maintenance/support, security and WCAG audits, and will work on websites at a reasonable rate.

waterboy
Joomla! Intern
Joomla! Intern
Posts: 56
Joined: Wed Jul 06, 2011 11:43 am

Re: Creating a custom plugin

Post by waterboy » Thu Nov 06, 2014 1:03 am

Hello,

Code: Select all

class SpinTaxHelper
{
	/*
	* Function that does the spinning of the text
	*/
	public static function SpinText($text)
	{
		return preg_replace_callback('/\{(((?>[^\{\}]+)|(?R))*)\}/x',array('SpinTaxHelper', 'replace'),$text);
	}

	public static function replace($text)
	{
		$text = $this->process($text[1]);
		$parts = explode('|', $text);
		return $parts[array_rand($parts)];
	}
}
now I get this error
Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files (x86)\NuSphere\TechPlat\apache\htdocs\plugins\content\jomspintax\helper\helper.php on line 20

User avatar
sitesrus
Joomla! Ace
Joomla! Ace
Posts: 1469
Joined: Mon Nov 12, 2012 10:48 pm

Re: Creating a custom plugin

Post by sitesrus » Thu Nov 06, 2014 2:12 pm

Can you show me the file and point out what line 20 is? The codey ou posted doesn't go up to 20 lines so I'll need you to identify it.

This has nothing to do with OO it's your usage of the stdClass object, you're trying to use it as a string somewhere. stdClass is like a generic object you can just add data to and pass it around, somewhere you're probably treating it as a string which is not allowed because it has no rules telling it how to convert it to a string.

When you code objects there's __toString() which is a magic method used to convert objects to strings when being used as such, stdClass doesn't have this implementation because it's just a generic container for the most part.

So show me that code, and you likely have to do something like echo $stdClass => echo $stdClass->some_var or whatever.
I like working with Joomla :). I offer the following professional services: Custom extension development, SEO/marketing, maintenance/support, security and WCAG audits, and will work on websites at a reasonable rate.

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: Creating a custom plugin

Post by sovainfo » Thu Nov 06, 2014 2:27 pm

Probably: $text = $this->process($text[1]);
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

User avatar
sitesrus
Joomla! Ace
Joomla! Ace
Posts: 1469
Joined: Mon Nov 12, 2012 10:48 pm

Re: Creating a custom plugin

Post by sitesrus » Fri Nov 07, 2014 12:00 am

Oh ya I missed that line lol. You can't call $ this it's not an object. Call it statically and if you don't have that function add it. Use Class:: or self:: if inside the class.
I like working with Joomla :). I offer the following professional services: Custom extension development, SEO/marketing, maintenance/support, security and WCAG audits, and will work on websites at a reasonable rate.


Locked

Return to “Joomla! 3.x Coding”