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

How do I create an add-on that allows a user to click a link and call php code?

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 18, 2013.

  1. AndyB

    AndyB Well-Known Member

    I would like to create an add-on that allows an admin to change the date of a post. Under each post there will be a link the admin can click. A form will be displayed and when submitted the database is updated. I already have a stand alone php script to do this, but I would like to learn how to create a proper add-on.

    Jake told me the initial steps.

    1) You need to create a route so you have a location to request with the link.
    2) You need a controller for that route to actually execute php code when that link is clicked.
    3) Then add the link to the template.

    So lets start with step #1, how do I create a route?
     
  2. Nobita.Kun

    Nobita.Kun Well-Known Member

    I think you need extends ControllerPublic_Post so you can do that.
     
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    Last edited: Sep 18, 2013
  4. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    I would like to learn how to create an add-on. I looked at the add-on "Edit Post Date by Waindigo", but it's very complicated and I have no idea what does what. So I hope to be able to create this add-on with help from others.

    So how do I define a route? Also will the XML file for the add-on define the route as well?
     
    Last edited: Sep 18, 2013
  5. AndyB

    AndyB Well-Known Member

    I assume to create a route this screen in the Admin CP is used.

    pic001.jpg

    Route Prefix: tools
    Route Type: Public
    Route Class: I have no idea what goes here
    Use class to build link: Only when data is provided
    Add-on: Change Date
     
  6. Nobita.Kun

    Nobita.Kun Well-Known Member

    As I said before. Create new route isn't nessasery, Why You don't extends XenForo_ControllerPublic_Post then you can using actionChangeDate. If you create new route you need create new ControllerPublic... Not good for your aim
     
  7. AndyB

    AndyB Well-Known Member

    Hi Nobita.Kun,

    Thank you for your assistance. I have no idea what "extends XenForo_ControllerPublic_Post" means.

    What I would like to do is have a link located below the post. When the user clicks the link it will show them a few input fields and a submit button.
     
  8. Nobita.Kun

    Nobita.Kun Well-Known Member

    • Display link you should using Template_Hook for display
    • Display form overlay in class link you must use: class="OverlayTrigger"
    • Want to change datetime of post you need post_id so extends XenForo_ControllerPublic_Post is better for that. You can find post_id by $postId = $this->_input->filterSingle('post_id', XenForo_Input::UINT); then to do something
    • When You extends that so link should be for using: {xen:link 'posts/whatever', $post}
     
    SneakyDave likes this.
  9. AndyB

    AndyB Well-Known Member

    I'll start with the last bullet. Lets say I change whatever to "changedate". So when the user hovers over the link would it display something like:

    http://www.mysite.com/forums/posts/123456/changedate
     
  10. Nobita.Kun

    Nobita.Kun Well-Known Member

    ? Are your forum using XenForo Software? Look like Its other software :oops:
     
  11. AndyB

    AndyB Well-Known Member

    Could I use Template Modification instead?
     
  12. AndyB

    AndyB Well-Known Member

    Yes I'm using XenForo software 1.2.0.
     
  13. Nobita.Kun

    Nobita.Kun Well-Known Member

    No. I said Template_Hook. You can use hook_name: post_public_controls
    Ha? Are you create new Event Listener: load_class_controller
     
  14. AndyB

    AndyB Well-Known Member

    At this point all I have created is the Add-on.

    pic001.jpg
     
  15. Nobita.Kun

    Nobita.Kun Well-Known Member

    No. You need create new Event Listener here: {boardUrl}/admin.php?code-event-listeners/
     
  16. AndyB

    AndyB Well-Known Member

    Okay I'll do that in a second.

    But first if I assume correctly, I need to create an index.php file here:

    library/Andy/ChangeDate/index.php

    PHP:
    <?php

    class Andy_ChangeDate_index
    {
       public static function 
    templateHook($hookName, &$contents, array $hookParamsXenForo_Template_Abstract $template)
       {   
         if (
    $hookName == 'post_private_controls')
         {
           
    $contents $contents 'test';
         }
       }
    }

     
    Last edited: Sep 18, 2013
  17. AndyB

    AndyB Well-Known Member

    Code Event Listener.

    pic001.jpg
     
    Last edited: Sep 18, 2013
  18. Jeremy

    Jeremy XenForo Moderator Staff Member

    No. Wrong class and your PHP is invalid.
     
  19. Chris D

    Chris D XenForo Developer Staff Member

    Why are you insisting he uses a template hook when they are deprecated?
     
  20. AndyB

    AndyB Well-Known Member

    Hi Chris,

    Would it be better to use Template Modifications?
     

Share This Page