• 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

AndyB

Well-known member
#1
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:

AndyB

Well-known member
#2
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']))
		);
	}
}
 

AndyB

Well-known member
#5
if you're going to use option, just use {$xenOptions} in your template
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:

AndyB

Well-known member
#8
Extend the controller, add to $parent->params[];.
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
 

AndyB

Well-known member
#10
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
 

xf_phantom

Well-known member
#11
Hi tyteen4a03,

When I try to add {xen:helper dump $xenOptions} to the create_thread template, I get the following error:
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 )
 

Brogan

XenForo moderator
Staff member
#12
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.
    }
}
 

AndyB

Well-known member
#15
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;
	}
}

?>
 

Jeremy

Well-known member
#16
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.
 

AndyB

Well-known member
#17
2.

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

3. You're calling 2x parent::renderHtml(); (not necessary:D )
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.
 

AndyB

Well-known member
#18
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.
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?
 

AndyB

Well-known member
#20
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.