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

How to add a variable to create_thread template

Discussion in 'XenForo Development Discussions' started by AndyB, Nov 10, 2013.

  1. AndyB

    AndyB Well-Known Member

    The create_thread template has the following code:

    maxlength="100"

    This controls the maximum title length and I would like to be able to change this with my add-on.

    Using Template Modification I will replace the default code so it will become:

    maxlength="{$maxLength}"

    The value of $maxLength will be set in my add-on Options.

    EDIT: see post #20 for solution.
     
    Last edited: Nov 10, 2013
  2. AndyB

    AndyB Well-Known Member

    I assume I need to extend XenForo_ViewPublic_Thread_Create so here is the default code:

    PHP:
    <?php

    class XenForo_ViewPublic_Thread_Create extends XenForo_ViewPublic_Base
    {
        public function 
    renderHtml()
        {
            if (!empty(
    $this->_params['captcha']))
            {
                
    $this->_params['captcha'] = $this->_params['captcha']->render($this);
            }

            
    $this->_params['editorTemplate'] = XenForo_ViewPublic_Helper_Editor::getEditorTemplate(
                
    $this'message', !empty($this->_params['draft']) ? $this->_params['draft']['message'] : '',
                array(
    'autoSaveUrl' => XenForo_Link::buildPublicLink('forums/save-draft'$this->_params['forum']))
            );
        }
    }
     
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    Extend the controller, add to $parent->params[];.
     
  4. Milano

    Milano Well-Known Member

    if you're going to use option, just use {$xenOptions} in your template
     
  5. AndyB

    AndyB Well-Known Member

    Hi Milano,

    Thank you for your suggestion.

    I've tried several variations of using <xen: option> but not able to make that work. Could you provide an example.
     
    Last edited: Nov 10, 2013
  6. tyteen4a03

    tyteen4a03 Well-Known Member

    Tried {xen:helper dump $xenOptions} to see what is in it?
     
  7. AndyB

    AndyB Well-Known Member

    Hi tyteen4a03,

    When I try to add {xen:helper dump $xenOptions} to the create_thread template, I get the following error:

     
  8. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    Thank you for the suggestion. I have successfully extended XenForo_ViewPublic_Thread_Create. But not sure how to add $parent->params[];. When I try the following code:

    PHP:
    <?php

    class Andy_TitleControl_ViewPublic_Thread_Create extends XFCP_Andy_TitleControl_ViewPublic_Thread_Create
    {
        public function 
    renderHtml()
        {
            
    $parent parent::renderHtml();
            
            
    $maxLength 75;
            
            
    // prepare $viewParams for template
            
    $viewParams = array(
                
    'maxLength' => $maxLength,
            );
            
            
    $this->_params['editorTemplate'] = array_merge($parent->params$viewParams);
            
            return 
    parent::renderHtml();
        }
    }

    ?>
    I get the following error:

    An exception occurred: Trying to get property of non-object in /home/southbay/www/forums/library/Andy/TitleControl/ViewPublic/Thread/Create.php on line 16
     
  9. Brogan

    Brogan XenForo Moderator Staff Member

    Shouldn't it be something like:

    PHP:
    $maxLength $parent->params['maxLength'];
     
  10. AndyB

    AndyB Well-Known Member

    Hi Brogan,

    Thank you for the suggestion. When I try:

    PHP:
    <?php

    class Andy_TitleControl_ViewPublic_Thread_Create extends XFCP_Andy_TitleControl_ViewPublic_Thread_Create
    {
        public function 
    renderHtml()
        {
            
    $parent parent::renderHtml();
            
            
    $maxLength $parent->params['maxLength']; 
            
            return 
    parent::renderHtml();
        }
    }

    ?>
    I get the following error:

    An exception occurred: Trying to get property of non-object in /home/southbay/www/forums/library/Andy/TitleControl/ViewPublic/Thread/Create.php on line 9
     
  11. xf_phantom

    xf_phantom Well-Known Member

    It's
    Code:
    {xen:helper dump, $xenOptions}
    
    2.

    Code:
    $this->_params['editorTemplate'] = array_merge($parent->params,
    You notice the difference?

    3. You're calling 2x parent::renderHtml(); (not necessary:D )
     
  12. Brogan

    Brogan XenForo Moderator Staff Member

    This is how it is in my add-on:

    PHP:
    class CTA_FeaturedThreads_ControllerPublic_Thread extends XFCP_CTA_FeaturedThreads_ControllerPublic_Thread
    {
        public function 
    actionIndex()
        {
            
    $parent parent::actionIndex(); // Extend the default function.

            
    $thread $parent->params['thread']; // Get the existing $thread record from the parent.

            
    $featureThreadModel XenForo_Model::create('CTA_FeaturedThreads_Model_Featured'); // Call the Featured Thread model.

            
    $parent->params['canFeatureThread'] = $featureThreadModel->canFeatureThread($thread); //Set the {$canFeatureThread} param to whatever is returned by the canFeatureThread function in the model.

            
    return $parent// We've added the canFeatureThread param to the $parent so we can just return the $parent.
        
    }
    }
     
  13. xf_phantom

    xf_phantom Well-Known Member

    @Brogan have to use $parent->params because he's "proxyextending:D " the controller class
    @AndyB have to use $parent->_params because he's "proxyextending:D" the view class
     
    Jeremy and Brogan like this.
  14. Brogan

    Brogan XenForo Moderator Staff Member

    Ah yes, good point.
     
  15. AndyB

    AndyB Well-Known Member

    This is what I have so far.

    PHP:
    <?php

    class Andy_TitleControl_ViewPublic_Thread_Create extends XFCP_Andy_TitleControl_ViewPublic_Thread_Create
    {
        public function 
    renderHtml()
        {
            
    $parent parent::renderHtml();
            
            
    // get options from Admin CP -> Options -> Title Control -> Maximum Length    
            
    $maxLength XenForo_Application::get('options')->maxLength;    
            
            
    // looking for correct code to add here
            
            
    return $parent;
        }
    }

    ?>
     
  16. Jeremy

    Jeremy XenForo Moderator Staff Member

    xf_phantom gave you the code a few times and Paul showed you an example of adding it via the controller. I suggest the controller.
     
  17. AndyB

    AndyB Well-Known Member

    Hi xf_phantom,

    I've looked at your example but cannot see any difference. What is the code I should use to array_merge?

    Thank you.
     
  18. AndyB

    AndyB Well-Known Member

    I've tried several variations of Paul's code but not able to make it work.

    Jeremy, what do you mean "adding it via the controller". I thought the correct way is to extend XenForo_ViewPublic_Thread_Create. Should I be extending some other class?
     
  19. Jeremy

    Jeremy XenForo Moderator Staff Member

    Extend the controller as Paul has done. I do not know the action off hand.
     
  20. AndyB

    AndyB Well-Known Member

    Okay got it figured out. Thank you, xf_phantom for your help.

    The solution is very simple, you don't need to extend any class. To include an Option that has been created in your add-on, all you have to do is use this in your template:

    {$xenOptions.maxLength}

    In this example maxLength is the Option ID.
     
    xf_phantom likes this.

Share This Page