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

What is the best way to alter the "Post New Thread" text in the new thread button from my addon?

Discussion in 'XenForo Development Discussions' started by SpecialK, Jun 24, 2016.

  1. SpecialK

    SpecialK Member

    I am developing an addon that, under certain circumstances, needs to change the text in the "Post New Thread" button. Only in certain cases will the label be changed, and it won't always be changed to the same text. This is part of a larger addon that I'm developing. The rest of my addon development is going fine, but I just can't figure out how to go about this. I have a class created and the listener added to the load_class_controller event and my listener is firing and working well. I just can't figure out what to actually do in my listener to update that text. At first I thought about updating the phrase cache with the new text, but that didn't even seem to do anything and probably isn't the best way to go about it anyway.

    Any help here would be appreciated. Here is the method as it currently stands:

    PHP:
    class Special_ControllerPublic_Forum extends XFCP_Special_ControllerPublic_Forum
    {
        public function 
    actionIndex()
        {
            
    $special_redirect_map $this->get_special_redirect_map();

            
    $forumId $this->_input->filterSingle('node_id'XenForo_Input::UINT);

            if(
    array_key_exists($forumId$special_redirect_map))
            {
    //Here is where I need to update the label
            
    }

            
    $return parent::actionIndex();

            return 
    $return;

        }
     
  2. Thomas.B

    Thomas.B Well-Known Member

    You could create a template mod that decides which phrase is used based on a param that you include in the controller response.
     
  3. SpecialK

    SpecialK Member

    OK, I'm still a bit lost on this one unfortunately. I have created a template modification that replaces {xen:phrase post_new_thread} with a conditional:

    PHP:
    <xen:if is="{$special_new_thread_label}">
    $special_new_thread_label
    <xen:else />
    {
    xen:phrase post_new_thread}
    </
    xen:if>
    but I don't know how to actually set that variable. I have a listener that is running on actionForum() but I can't figure out how to insert my custom variable into the view before it's rendered.

    My code as of right now:
    PHP:
    public function actionForum()
    {
            
    $return parent::actionForum();
            
    $return->viewParams['special_new_thread_label'] = false;

            
    $special_redirect_map $this->get_special_redirect_map();

            
    $forumId $this->_input->filterSingle('node_id'XenForo_Input::UINT);

            if (
    array_key_exists($forumId$special_redirect_map)) {
                
    $return->viewParams['special_new_thread_label'] = $special_redirect_map[$forumId]['label'];
            }

            return 
    $return;
    }
    Clearly adding to the viewParams array isn't going to do much after the response has been rendered and saved to the $return variable.
     
  4. Thomas.B

    Thomas.B Well-Known Member

    Everything you include in the controller response will be available when the template is rendered subsequently. There are only two errors in your code. If you add curly braces to your variable in the template mod and change
    PHP:
    $return->viewParams['special_new_thread_label'];
    to
    PHP:
     $return->params['special_new_thread_label'];
    it should work.
     
  5. SpecialK

    SpecialK Member

    You're a lifesaver. I knew that template needed curly braces around the var. No idea why I didn't include that.

    I looked into the base Forum controller in XF and saw that viewParams in the actionForum method so that's why I was including my custom text there. I don't see the params property in that base model, though. Where is that defined?

    Thanks for your help. Your advice worked perfectly!
     
  6. Thomas.B

    Thomas.B Well-Known Member

    Actions (mostly) use the responseView() method of the XenForo_Controller class to get a XenForo_ControllerResponse_View object which is then returned. Have a look at the library/XenForo/ControllerResponse directory to see the different kinds of response classes and their properties.

    You're welcome! I'm glad I could help :)
     
    Steve F likes this.

Share This Page