First Add-on, could use some love :)

Sylar

Well-known member
Well, finally got into creating my first add-on...pretty much stuck at the front door :P

Here's basically what I want to do: I'd like to add a small template edit to the node view(i.e.: this page: http://xenforo.com/community/forums/xenforo-development-discussions.34/), and I want to have something within that edit to call the node value based on what node it's in(i.e.: if the template edit was in node 7, it would be able to know that and the id would be placed in the edit; possibly in a url). That little link will then pop you into a page in the ACP :)
Now, I am a bit confused about the controllers, listeners, etc. that I will need to handle. Also, a bit lost on how to add template edits with an add-on :/

Any help would be greatly appreciated, and you will get a mention in the release page :notworthy:
 
Ok, well you're starting simply, so this is good news. For this one you shouldn't need to know anything about Controllers.

But, to achieve this, you will need a Listener.

First, we need an idea of where the code needs to go. Ideally with an add-on you need to add code into a template hook. So to build a Listener (which would listen to the template hook event) you would need to know what template hook the code needs to sit in.

You can identify where the hooks are by looking in the forum_view template.

Once you know what the hook is, you need a Listener.php that looks like this:

Rich (BB code):
<?php
class YourAddOn_Listener
{
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
{​
if ($hookName == 'the_hook_you_want')​
{​
$contents .= $template->create('your_template');​
}​
}​
}

This is actually quite basic, but it may do what you need.

Notice in blue is the hook you want to insert a template edit into.

In red is a template that you want to insert into the hook.

So, all you need to do now is create an add-on in the Admin CP, a code event listener which references the listener and then create a template called your_template (or whatever you want it to be called) which contains the template edits you want to make.

I've cut down a few steps, but see how you get on with these first :)
 
That seems to be going well. Now, the question is how do I get the link inside the template to grab the node id?
 
That seems to be going well. Now, the question is how do I get the link inside the template to grab the node id?

You could try:
{$forum.node_id} in your template.

It might not work which might mean we need to do more in the Listener, but in theory it should work.
 
<a href="{$forum.node_id}">Link</a>
Doesn't seem to be working. Redirects me back to the index.
 
Yes, that would do.

Effectively, that would result in the URL looking like this:

<a href="7">Link</a>

I need to remind myself of the correct syntax but it would need to look something like:

<a href="{xen:link forums, '', '{$forum.node_id}'}">Link</a>

I will need to check that out though...

EDIT:

Actually, this should work:

<a href="{xen:link 'forums', $forum}">Link</a>
 
Ok...

This is what I meant by "It might not work which might mean we need to do more in the Listener, but in theory it should work."

So does your Listener look something like this? Try adding the code in red.

Rich (BB code):
<?php
class YourAddOn_Listener
{
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
{​
if ($hookName == 'the_hook_you_want')​
{​
$params = $template->getParams();
$params += $hookParams;
$contents .= $template->create('your_template', $params);​
}​
}​
}

What this does is ensures any parameters that are ordinarily available in that hook and the forum_view template will still be available in your own template.

That should work...
 
Woah swish man, that did the trick :D
Thank you so much, I know I'm an idiot and idiots are annoying to deal with :notworthy: Will be packing this up and slapping your name up as a huge contributor in a bit :)
 
Woah swish man, that did the trick :D
Thank you so much, I know I'm an idiot and idiots are annoying to deal with :notworthy: Will be packing this up and slapping your name up as a huge contributor in a bit :)
:D

Hey, not at all annoying.

It was only a few months ago that I myself was being an idiot and PMing Jake all the time for help :D

If you ever need help again, post again and I'll be happy to help. :)
 
Back
Top Bottom