Title Control v1.0

AndyB

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

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:
The directory structure:

--Andy
----TitleControl
------DataWriter
--------Discussion
----------Thread.php
----Listener.php
 
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:
library/Andy/TitleControl/Listener.php

PHP:
<?php

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

?>
 
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();
...
 
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();
 
Back
Top Bottom