How to add a variable to create_thread template

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:
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']))
		);
	}
}
 
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:
Tried {xen:helper dump $xenOptions} to see what is in it?

Hi tyteen4a03,

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

The following templates contained errors and were not saved: thread_create: 1) Line 151: Template syntax error.
 
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
 
Shouldn't it be something like:

PHP:
$maxLength = $parent->params['maxLength'];
 
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
 
Hi tyteen4a03,

When I try to add {xen:helper dump $xenOptions} to the create_thread template, I get the following error:
It's
Rich (BB code):
{xen:helper dump, $xenOptions}

2.

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

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

?>
 
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.
 
2.

Rich (BB 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.
 
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?
 
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.
 
Top Bottom