Question about code listener.

arcaneex

Active member
EDIT: Quick question , I just made a code listener , for template hook which includes a php file. In my class , the hook is message_user_info_avatar and the template message_user info calls the hook. Means that the code listener will include the PHP file there aswell?
 
Yes in theory this is possible.

You can pass parameters from the event listener to your template via a hook.

If you need any specific help, let me know.
 
Quick question , I just made a code listener , for template hook which includes a php file. In my class , the hook is message_user_info_avatar and the template message_user info calls the hook. Means that the code listener will include the PHP file there aswell?
 
I have no idea how you're including the PHP file, what the variables look like, whether the PHP file itself contains HTML to render stuff.

It's all a bit too vague to answer with much detail.

That being said, typically your template hook would look like this (bare bones)

Rich (BB code):
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'message_user_info_avatar')​
{​
}​
}

How you include your PHP file, and what you want to achieve, I have no idea. But if that PHP file contains an array of data called $stuff (for example) then you could pass that to the hook like this:

Rich (BB code):
$hookParams['stuff'] = $stuff;

You would now be able to use {$stuff} in your template.

You can verify the "stuff" has been passed to the template by dumping it using:

{xen:helper dump, $stuff}

This would output the raw data from the array in your template.

But again, this is all very vague as I'd probably need to see all of your code and work out what you're trying to achieve to be more accurate/specific. But hopefully what I've said so far will help.
 
I have no idea how you're including the PHP file, what the variables look like, whether the PHP file itself contains HTML to render stuff.

It's all a bit too vague to answer with much detail.

That being said, typically your template hook would look like this (bare bones)

Rich (BB code):
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if ($hookName == 'message_user_info_avatar')​
{​
}​
}

How you include your PHP file, and what you want to achieve, I have no idea. But if that PHP file contains an array of data called $stuff (for example) then you could pass that to the hook like this:

Rich (BB code):
$hookParams['stuff'] = $stuff;

You would now be able to use {$stuff} in your template.

You can verify the "stuff" has been passed to the template by dumping it using:

{xen:helper dump, $stuff}

This would output the raw data from the array in your template.

But again, this is all very vague as I'd probably need to see all of your code and work out what you're trying to achieve to be more accurate/specific. But hopefully what I've said so far will help.
Right now , I have this:
a class which includes my php file , a variable in that php file and a plugin/code event listener.
 
That really isn't any less vague than you already have been...

If you can paste some example code from your class and code event listener that would be useful.

I imagine it should look something like this:
  • You've created an add-on in the Admin CP. Let's pretend it's called "Stuff".
  • You've created a Listener.php (or similar name) in your library/Stuff folder
  • That should look like this:
Rich (BB code):
<?php
class Stuff_Listener
{
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
{​
if ($hookName == 'message_user_info_avatar')​
{​
include 'your_stuff.php';​
// Example: let's assume $stuff is a variable in your_stuff.php​
$hookParams['stuff'] = $stuff;​
$params = $template->getParams();​
$params += $hookParams;​
$contents .= $template->create('your_template', $params);​
}​
}​
}
  • You should have created a Code Event Listener in the Admin CP with event Template Hook, callback being: Stuff_Listener::templateHook
  • (Example) Created a template called your_template in the Admin CP .
  • In your_template type (for demo purposes) {xen:helper dump, $stuff}
With that code example above, theoretically in the message_user_info_avatar section you should now see a load of data being dumped which should hopefully be the data from the your_stuff.php script.
Hope that helps.
 
That really isn't any less vague than you already have been...

If you can paste some example code from your class and code event listener that would be useful.

I imagine it should look something like this:
  • You've created an add-on in the Admin CP. Let's pretend it's called "Stuff".
  • You've created a Listener.php (or similar name) in your library/Stuff folder
  • That should look like this:
Rich (BB code):
<?php
class Stuff_Listener
{
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
{​
if ($hookName == 'message_user_info_avatar')​
{​
include 'your_stuff.php';​
// Example: let's assume $stuff is a variable in your_stuff.php​
$hookParams['stuff'] = $stuff;​
$params = $template->getParams();​
$params += $hookParams;​
$contents .= $template->create('your_template', $params);​
}​
}​
}
  • You should have created a Code Event Listener in the Admin CP with event Template Hook, callback being: Stuff_Listener::templateHook
  • (Example) Created a template called your_template in the Admin CP .
  • In your_template type (for demo purposes) {xen:helper dump, $stuff}
With that code example above, theoretically in the message_user_info_avatar section you should now see a load of data being dumped which should hopefully be the data from the your_stuff.php script.
Hope that helps.
What do I put in $hookParams['stuff'] = $stuff;[
Yes , I did all the above besides params.
 
$hookParams is the array that stores all of the parameters that are available to the template hook.

$hookParams['stuff'] = $stuff;

...is adding an additional parameter to the template hook called stuff. Its contents is the PHP variable $stuff.

In my example $stuff is whatever variable you need to pull from your included PHP script.

Once you've got that, you should be able to use {$stuff} in your template. My suggestion initially is to use {xen:helper dump, $stuff} in your template for testing purposes.

If this still isn't working then I can't help you any further unless you post your existing code, and a more detailed explanation of what you're trying to achieve.
 
$hookParams is the array that stores all of the parameters that are available to the template hook.

$hookParams['stuff'] = $stuff;

...is adding an additional parameter to the template hook called stuff. Its contents is the PHP variable $stuff.

In my example $stuff is whatever variable you need to pull from your included PHP script.

Once you've got that, you should be able to use {$stuff} in your template. My suggestion initially is to use {xen:helper dump, $stuff} in your template for testing purposes.

If this still isn't working then I can't help you any further unless you post your existing code, and a more detailed explanation of what you're trying to achieve.
Thanks! I have more questions if you don't mind. I have this code , this is the php file which is included.
PHP:
    <?php
class Steam_Rep_Rep {
 
    // cURL Variable
    private $ch = null;
 
    public function __construct() {
        // Setup cURL
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, 4);
    }   
 
    public function getUserInfo($steamrep_id) {
        // cURL
        curl_setopt($this->ch, CURLOPT_URL, "http://steamrep.com/api/beta/reputation/76561197971691194?json=1");
        $result = curl_exec($this->ch);
        $xml = json_decode($result);
        if(!empty($xml->steamrep->reputation)) {
            return array(
                'rep' => $xml->steamrep->reputation,
            );
        } else {
            return array(
                'rep' => "No reputation found",
            );
        }
    }
}
$steam = new Steam_Rep_Rep();
$rep = $steam->getUserInfo('76561197971691194'); 
echo $rep['rep'];
?>
Now , you see those three last lines after the code? without them , as it seems ( atleast the first two ) it doesn't output it. My IDE just says Document contains no data.
Now here's the thing , I have completely no idea what I should do. I need this file to output the contents of the link (http://steamrep.com/api/beta/reputation/76561197971691194?json=1) which are in json , decode it and then store it in the variable. From what I understood , $rep is basically the variable which the data gets stored in. If I use the code above , it does all that. Outputs what I need. But if I understood correctly.
If I just upload it like this , use my event listener , the plugin and the listener.php and use the variable $rep in the template - will it work?
So make it less vague:
Listener:
PHP:
 <?php
class Steamrep_Rep_Rep {
public static function includePhpFile($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
{
if($hookName == 'message_user_info_avatar')
{
ob_start();
require_once('php_include.php');
$contents .= ob_get_contents();
ob_end_clean();
}
}
}
?>
And the php which I need included is found above. So yeah.
Hope you can help.
 
Top Bottom