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

Title Control v1.0

AndyB

Well-known member
#1
Purpose:

The purpose of this thread is to provide additional information on the Title Control v1.0 add-on. To download and install this add-on please visit the following XenForo Resource:

http://xenforo.com/community/resources/title-control.2485/

Description:

The Title Control add-on will do the following:
  • Control maximum length of thread titles (Option)
  • Force the first character of the thread title to uppercase (Option)
  • Allows additional title control using external PHP file (Option)

Admin Options:

pic001.jpg

Path to PHP:

This feature allows admins to create an optional PHP file that can control any aspect of the thread title. Here's an example that will remove all quotes from the thread title:

PHP:
<?php

// get title
$title = $this->get('title');

// remove quotes
if (substr_count($title, '&quot;') > 0){
    $patterns[0] = '/\&quot;/';
    $replacements[0] = '';
    $title = preg_replace($patterns, $replacements, $title);
}

// set title
$this->set('title', $title);
?>
 
Last edited:

AndyB

Well-known member
#3
library/Andy/TitleControl/DataWriter/Discussion/Thread.php

PHP:
<?php

class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
{	
	protected function _discussionPreSave()
	{
		//########################################
		// default code start
		//########################################
		
		if ($this->get('prefix_id') && $this->isChanged('node_id'))
		{
			if (!$this->_getPrefixModel()->getPrefixIfInForum($this->get('prefix_id'), $this->get('node_id')))
			{
				$this->set('prefix_id', 0); // invalid prefix
			}
		}
		
		//########################################
		// default code end
		//########################################		
		
		// get options from Admin CP -> Options -> Title Control -> Maximum Length    
		$maxLength = XenForo_Application::get('options')->maxLength;
		
		// get options from Admin CP -> Options -> Title Control -> Uppercase First    
		$uppercaseFirst = XenForo_Application::get('options')->uppercaseFirst;	
		
		// get options from Admin CP -> Options -> Title Control -> Path to PHP    
		$pathToPHP = XenForo_Application::get('options')->pathToPHP;			
		
		// maximum length
		if ($maxLength != '')
		{
			$title = $this->get('title'); 
			
			$currentTitleLength = strlen($title);
			
			if ($currentTitleLength > $maxLength)
			{
				$title = substr($title, 0, $maxLength);
				$this->set('title', $title);			
			}			
		}
		
		// uppercase first
		if ($uppercaseFirst != '')
		{		
			$title = $this->get('title');	
			$title = ucfirst($title);
			$this->set('title', $title);
		}
		
		// path to php
		if ($pathToPHP != '')
		{		
			include($pathToPHP);
		}
	}
}

?>
 
Last edited:

AndyB

Well-known member
#4
library/Andy/TitleControl/Listener.php

PHP:
<?php

class Andy_TitleControl_Listener
{
	public static function Thread($class, array &$extend)
	{		
		$extend[] = 'Andy_TitleControl_DataWriter_Discussion_Thread';
	}
}

?>
 

AndyB

Well-known member
#7
You didn't call the parent function. This is bad practice and can cause issues with other addons.
Thank you for bringing this to my attention, Jeremy.

Is this the correct code?

PHP:
class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
{	
	protected function _discussionPreSave()
	{
		// call parent function
		$parent = parent::_discussionPreSave();
...
 

AndyB

Well-known member
#9
You don't need to store the return.
Thank you, Jeremy.

PHP:
class Andy_TitleControl_DataWriter_Discussion_Thread extends XFCP_Andy_TitleControl_DataWriter_Discussion_Thread
{	
	protected function _discussionPreSave()
	{
		// call parent function
		parent::_discussionPreSave();