1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

First Add-on, could use some love :)

Discussion in 'XenForo Development Discussions' started by Sylar, Sep 3, 2012.

  1. Sylar

    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:
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    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:

    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 :)
     
    RobinHood, CyclingTribe and Sylar like this.
  3. Sylar

    Sylar Well-Known Member

    That seems to be going well. Now, the question is how do I get the link inside the template to grab the node id?
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
  5. Sylar

    Sylar Well-Known Member

    <a href="{$forum.node_id}">Link</a>
    Doesn't seem to be working. Redirects me back to the index.
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    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>
     
  7. Sylar

    Sylar Well-Known Member

    That gets me to yourlink.com/index.php?forums/ however still missing the node id :(
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    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.

    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...
     
    Sylar and CyclingTribe like this.
  9. Sylar

    Sylar Well-Known Member

    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 :)
     
    Chris D likes this.
  10. Chris D

    Chris D XenForo Developer Staff Member

    :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. :)
     
    Sylar likes this.
  11. Sylar

    Sylar Well-Known Member

    Awesome mate! Thanks again :)
     
    Chris D likes this.

Share This Page