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

XF 2.0 How to properly extend setupThreadCreate

AndyB

Well-known member
#1
I have an add-on called Title control, it's purpose is to make changes to the thread title. The following code is used when a new thread is created.

The code works fine, but don't I need to get the parent, then make needed updates to the creator object then return the parent? Or is the code proper?

PHP:
<?php

namespace Andy\TitleControl\XF\Pub\Controller;

use XF\Mvc\ParameterBag;

class Forum extends XFCP_Forum
{
	protected function setupThreadCreate(\XF\Entity\Forum $forum)
	{
		$title = $this->filter('title', 'str');

		$message = $this->plugin('XF:Editor')->fromInput('message');

		$creator = $this->service('XF:Thread\Creator', $forum);
		
		// get options
		$options = \XF::options();
		
		// get options from Admin CP -> Options -> Title control -> Uppercase first  
		$uppercaseFirst = $options->titleControlUppercaseFirst;
		
		// get options from Admin CP -> Options -> Title control -> External file
		$externalFile = $options->titleControlExternalFile;
		
		// get options from Admin CP -> Options -> Title control -> Multibyte  
		$multibyte = $options->titleControlMultibyte;
		
		// uppercase first
		if ($uppercaseFirst)
		{	
			if ($multibyte)
			{
				// to work with non-latin characters
				$title = mb_strtoupper(mb_substr($title, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr($title, 1, mb_strlen($title), 'UTF-8');
			}
			else
			{
				$title = ucfirst($title);
			}
		}
		
		// include optional external file
		if ($externalFile != '')
		{		
			if (file_exists($externalFile))
			{
				include($externalFile);
			}
		}

		$creator->setContent($title, $message);

		return $creator;
	}
}