Custom fields in the plugin. In onBeforeCompileHead() Topic is solved

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
bombapiter
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue May 11, 2021 11:23 am

Custom fields in the plugin. In onBeforeCompileHead()

Post by bombapiter » Tue May 11, 2021 11:28 am

Hello.

Google for a long time .... Help is needed.

I'm making a plugin that adds meta tags.

Data for meta tags is taken from custom fields. Here's a piece of code:

Code: Select all

public function onBeforeCompileHead()
{
$priceprd = $this->params->get('google-product-price');
$pricecur = $this->params->get('google-product-priceCurrency');

 foreach($article->jcfields as $jcfield)
{
$article->jcFields[$jcfield->name] = $jcfield;
}

$metatype                     = '<meta property="og:type" content="product" />';
$metacategory                     = ' <meta property="product:category" content="'. $category->title.'" />';
$metaamount                   = ' <meta property="product:price:amount" content="'.$article->jcFields[$priceprd]-.'" />';
$metacurrency                  = ' <meta property="product:price:currency" content="'.$article->jcFields[$pricecur ]-.'" />';

}
How to get $article in onBeforeCompileHead() function?

illovo
Joomla! Intern
Joomla! Intern
Posts: 50
Joined: Sun Jan 13, 2013 9:30 am
Contact:

Re: Custom fields in the plugin. In onBeforeCompileHead()

Post by illovo » Tue May 11, 2021 12:28 pm

I would define a property

Code: Select all

protected $myJcFields = null;
for the plugin.

Prepare data and fill property in method onContentPrepare if current page is an article view:

Code: Select all

$this->myJcFields = $article->jcfields;
Pick it in onBeforeCompileHead

Code: Select all

if ($this->myJcFields !== null) {do what you want to do};
Something like that. Not tested.

bombapiter
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Tue May 11, 2021 11:23 am

Re: Custom fields in the plugin. In onBeforeCompileHead()

Post by bombapiter » Tue May 11, 2021 1:27 pm

Thanks for the answer.


Do it like this:

Code: Select all


class PlgSystemScmeta extends JPlugin
{
	protected $myJcFields = null;
	
	public function onContentBeforeDisplay($context, &$article, &$params, $page = 0)
	{		
		$this->myJcFields = $article->jcfields;
		}
	public function onBeforeCompileHead()
	{
		if ($this->myJcFields !== null) {
			echo "<pre>";
			print_r($this->myJcFields );
			echo "</pre>";
			}	
	}		
}		


Works.

Or is there another way?


Post Reply

Return to “Extensions for Joomla! 3.x”