Skip to content

Instantly share code, notes, and snippets.

@stefanheimes
Last active January 4, 2016 18:09
Contao - Inserttag for counting entries.
<?php
// config.php
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('ExtendedComments', 'myReplaceInsertTags');
class ExtendedComments
{
/**
* Replace a inserttag.
*
* @param $strTag The inserttag.
*
* @return bool|mixed|null The value or false on error or unknown inserttag.
*/
public function myReplaceInsertTags($strTag)
{
$arrParts = trimsplit('::', $strTag);
if ($arrParts[0] == 'comments')
{
return $this->countComments($arrParts[1], $arrParts[2]);
}
return false;
}
/**
* Get the count for comments.
*
* @param string $strParentTable Name of parent e.g. tl_news.
*
* @param int $intParentId Id of the parent.
*
* @return bool|mixed|null The count value or false on error or null if empty.
*/
protected function countComments($strParentTable, $intParentId)
{
if (empty($strParentTable) || empty($intParentId))
{
return false;
}
$objResult = \Database::getInstance()
->prepare('SELECT COUNT(*) as mycount FROM tl_comments WHERE source=? AND parent=?')
->execute($strParentTable, $intParentId);
return $objResult->mycount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment