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

Looking to create my first addon....

Discussion in 'XenForo Development Discussions' started by speedway, Apr 26, 2013.

  1. speedway

    speedway Well-Known Member

    Hi all

    I would like to create an addon that displays the thread age next to the thread title, for example in the Whats New page. Here is a shot of what I mean:

    Screen shot 2013-04-26 at 2.52.15 PM.png

    I have this on my VB forum at the moment and the code doesn't look like anything special. There are three places it happens (from VB):

    The first one is placed into the hook "threadbit_process":

    Code:
    $ta_start = "<span style=\"color: " . vB_Template_Runtime::fetchStyleVar('boofo_threaddays_color') . "\">";
    $ta_end = "</span>";
     
    $threaddays = floor((TIMENOW - $thread['dateline'])/86400);
    switch ($threaddays)
    {
    case '0':
    $thread['threaddays'] = "&ndash; $ta_start<b>" . $vbphrase['today'] . "</b>$ta_end";
    break;
    case '1':
    $thread['threaddays'] = "&ndash; $ta_start" . $threaddays . " " . $vbphrase['boofo_threaddays_dayold'] . "$ta_end";
    break;
    default:
    $thread['threaddays'] = "&ndash; $ta_start" . $threaddays . " " . $vbphrase['boofo_threaddays_daysold'] . "$ta_end";
    break;
    }
    
    The next hook to be used is "showthread_getinfo":

    Code:
    $ta_start = "<span style=\"color: " . vB_Template_Runtime::fetchStyleVar('boofo_threaddays_color') . "\">";
    $ta_end = "</span>";
     
    $threaddays = floor((TIMENOW - $thread['dateline'])/86400);
    switch ($threaddays)
    {
    case '0':
    $thread['threaddays'] = "&ndash; $ta_start<b>" . $vbphrase['today'] . "</b>$ta_end";
    break;
    case '1':
    $thread['threaddays'] = "&ndash; $ta_start" . $threaddays . " " . $vbphrase['boofo_threaddays_dayold'] . "$ta_end";
    break;
    default:
    $thread['threaddays'] = "&ndash; $ta_start" . $threaddays . " " . $vbphrase['boofo_threaddays_daysold'] . "$ta_end";
    break;
    }
    
    and lastly in the hook "process_template_complete":

    Code:
    $location = $vbulletin->options['boofo_threaddays_location'];
    switch ($location)
    {
    case '0':
    default:
    break;
    case '1':
    require_once(DIR . '/includes/class_template_parser.php');
    $parser = new vB_TemplateParser('{vb:raw thread.threadtitle}</a>');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $find = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $parser = new vB_TemplateParser('{vb:raw thread.threadtitle}</a> {vb:raw thread.threaddays}');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $replace = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $vbulletin->templatecache['search_threadbit'] = str_replace($find, $replace, $vbulletin->templatecache['search_threadbit']);
    $vbulletin->templatecache['threadbit'] = str_replace($find, $replace, $vbulletin->templatecache['threadbit']);
    $vbulletin->templatecache['threadbit_deleted'] = str_replace($find, $replace, $vbulletin->templatecache['threadbit_deleted']);
     
    unset($find, $replace);
     
    // Showthread title at top of thread
    $parser = new vB_TemplateParser('{vb:raw threadinfo.title}</a>');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $find = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $parser = new vB_TemplateParser('{vb:raw threadinfo.title}</a> {vb:raw thread.threaddays}');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $replace = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $vbulletin->templatecache['SHOWTHREAD'] = str_replace($find, $replace, $vbulletin->templatecache['SHOWTHREAD']);
     
    unset($find, $replace);
    break;
    case '2':
    require_once(DIR . '/includes/class_template_parser.php');
    $parser = new vB_TemplateParser('{vb:rawphrase started_by_x_y_z_a, {vb:link member, {vb:raw thread}, null, \'postuserid\', \'postusername\'}, {vb:raw thread.postusername}, {vb:raw thread.startdate}, {vb:raw thread.starttime}, {vb:stylevar dirmark}}');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $find = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $parser = new vB_TemplateParser('{vb:rawphrase started_by_x_y_z_a, {vb:link member, {vb:raw thread}, null, \'postuserid\', \'postusername\'}, {vb:raw thread.postusername}, {vb:raw thread.startdate}, {vb:raw thread.starttime}, {vb:stylevar dirmark}} {vb:raw thread.threaddays}');
    $parser->dom_doc = new vB_DomDocument($parser->fetch_dom_compatible());
    $replace = trim($parser->_parse_nodes($parser->dom_doc->childNodes()));
     
    $vbulletin->templatecache['search_threadbit'] = str_replace($find, $replace, $vbulletin->templatecache['search_threadbit']);
    $vbulletin->templatecache['threadbit'] = str_replace($find, $replace, $vbulletin->templatecache['threadbit']);
    $vbulletin->templatecache['threadbit_deleted'] = str_replace($find, $replace, $vbulletin->templatecache['threadbit_deleted']);
     
    unset($find, $replace);
    break;
    }
    
    Where this appears is controlled by an option:

    Screen shot 2013-04-26 at 3.01.02 PM.png


    If someone could give me some pointers as to where to start and I'll go from there.

    Cheers
     
  2. tyteen4a03

    tyteen4a03 Well-Known Member

    Firstly start by turning on debug mode (duh), then take a look at template hooks. I believe the resource section has guides on how to use them.

    Also, this template function may help you a lot: <xen:datetime time="$time_in_the_past" />
     
  3. speedway

    speedway Well-Known Member

    Excellent, thank you. Some afternoon reading ahead :)
     

Share This Page