Creating a Custom BB Code in XenForo: A Comprehensive Guide

It may be because these are added after URL is. Try adding the parent call after you define yours.
 
I am so glad King wrote this, great read and I am learning something. Thanks!
 
Does anyone know how to include a CSS template inside of a callback? I'm trying to do this:

Code:
    public function renderTagWiki(array $tag, array $rendererStates)
    {
        $text = $this->renderSubTree($tag['children'], $rendererStates);

        if ($page = XenForo_Model::create('EWRcarta_Model_Pages')->getPageBySlug($text))
        {
            $page = XenForo_Model::create('EWRcarta_Model_Pages')->renderPage($page);

            return '<div class="wiki">'.$page['HTML'].'</div>';
        }
        else
        {
            return false;
        }
    }
I have the pageHTML encapsulated in a class called wiki, which is defined in a template called EWRcarta.css
 
Maybe try creating a template and with the content of <div class="wiki">{xen:raw $variable}</div> and return the tempate as the BB Code?
 
Okay... this what I did...
Code:
<?php

class EWRcarta_BbCode_Formatter extends XFCP_EWRcarta_BbCode_Formatter
{
    protected $_tags;

    public function getTags()
    {
        $this->_tags = parent::getTags();

        $this->_tags['h2'] = array('hasOption' => false, 'replace' => array('<h2>', '</h2>'), 'trimLeadingLinesAfter' => 1);
        $this->_tags['h3'] = array('hasOption' => false, 'replace' => array('<h3>', '</h3>'), 'trimLeadingLinesAfter' => 1);
        $this->_tags['h4'] = array('hasOption' => false, 'replace' => array('<h4>', '</h4>'), 'trimLeadingLinesAfter' => 1);
        $this->_tags['h5'] = array('hasOption' => false, 'replace' => array('<h5>', '</h5>'), 'trimLeadingLinesAfter' => 1);
        $this->_tags['h6'] = array('hasOption' => false, 'replace' => array('<h6>', '</h6>'), 'trimLeadingLinesAfter' => 1);

        $this->_tags['wiki'] = array(
			'hasOption' => false,
			'trimLeadingLinesAfter' => 1,
			'callback' => array($this, 'renderTagWiki')
		);

        return $this->_tags;
    }

	public function renderTagWiki(array $tag, array $rendererStates)
	{
		$text = $this->renderSubTree($tag['children'], $rendererStates);

		if ($page = XenForo_Model::create('EWRcarta_Model_Pages')->getPageBySlug($text))
		{
			if ($this->_view)
			{
				$page = XenForo_Model::create('EWRcarta_Model_Pages')->renderPage($page);
				$template = $this->_view->createTemplateObject('EWRcarta_BBcode', array('page' => $page));

				return $template->render();
			}
			else
			{
				return '<div>Wiki:<pre><a href="'.XenForo_Link::buildPublicLink('full:wiki', $page).'">' . $page['page_name'] . '</a></pre></div>';
			}
		}
		else
		{
			return '[wiki]'.$text.'[/wiki]';
		}
	}
}

There are several points of notice that people need to be made aware of. This renderTagWiki function creates the contents of the tag through a template; and it receives the information through a model. While the models are accessable everywhere, the template view is not. If you try to render a template in an area that doesn't have access to the view, you will get a fatal error. So the way this tag worked...

Code:
IF media exists (checked through model)
    IF view exists
        render template view (view property must exist)
    ELSE
        render text view (just basic text)
ELSE
    render input (original text)
 
No the autolinking happens (iirc) before parsing of my bbcode.
Auto-linking happens before posts are saved, in XenForo_ControllerPublic_Post::actionSave().
To disable autolinking, it looks like you can hook into the load_class_bb_code code event to extend XenForo_BbCode_Formatter_BbCode_AutoLink, and add tag names to $this->_disableAutoLink.
 
Auto-linking happens before posts are saved, in XenForo_ControllerPublic_Post::actionSave().
To disable autolinking, it looks like you can hook into the load_class_bb_code code event to extend XenForo_BbCode_Formatter_BbCode_AutoLink, and add tag names to $this->_disableAutoLink.
I'll have to keep this in mind for t he next version of BBCM. :)
 
Actually, I am failing at this. I attempted to add things to $this->_disableAutoLink, but when I extend the class, and try to do that, it doesn't allow me to post. If I manually add tags to the array in the XenForo class, it autolinks it still and adds [/url][/ENDTAG] at teh end of it. I'm at a loss, any thoughts?
 
I've started working on part 3 and rewriting parts of this guide. Included will be inclusion in help pages. And some explanations on options.
 
How do I strip nested BBcodes? Lets say I have a BBcode...
Code:
[wiki]Arthur[FONT=Georgia][/wiki][/FONT]

I need to extract the word "Arthur" in my BBcode, and that works great. But the problem is the extra font information in the code. It shouldn't be there; it should be automatically stripped. How would I do this?
 
Hello,
is this tutorial still valid vor 1.3 ?

When i do it exactly according to this tutorial i get the following error:

Code:
Fatal error: Class 'XenBBCode_BbCode_Formatter_Base' not found in /<mypath>/library/XenForo/Application.php(514) : eval()'d code on line 1
anyone who could help me here?
 
Then you have en error in your created file. The Path is incorrect or the name of the class.

The Classname must be exactly like the path.

Class XenBBCode_BbCode_Formatter_Base
must be placed in the directory
library/XenBBCode//BbCode/Formatter/ with the filename Base.php
 
Hi,
thanks for your help.

... removed ...

edit
i've found my error! just a small typo within the Base.php -.-
thanks for your help!
 
Last edited:
Back
Top Bottom