Knowledge Base Display Hooks
The nine hooks over the knowledge base a visitor sees: the body, the tree, the scope, the popular box, search, and four observation points.
Overview
Everything a visitor sees passes through these hooks: the article body, the category tree, the popular box and the search results. All four are passed by link, so you make your change by writing over the incoming value rather than returning it.
Beside them sit four observation hooks: an article read, a category opened, a vote and a search. They change nothing; they report that something happened.
Reference
Following an article being read
Runs when an article is viewed. The visitor may well not be logged in.
Hook::add('action:knowledgebase.article.viewed', 10, function ($id, $article) {
// The visitor may not be logged in: build no assumption about identity.
Acme::trackRead($id);
});Following an article vote
Runs when a visitor marks an article useful or not.
useful or useless.Hook::add('action:knowledgebase.article.voted', 10, function ($id, $type) {
// Collect the unhelpful votes for the writing team.
if ($type === 'useless') Acme::flagForReview($id);
});Following a category being opened
Runs when a category page is viewed.
Hook::add('action:knowledgebase.category.viewed', 10, function ($catId, $cat) {
Acme::trackCategory($catId);
});Following a search
Runs when a visitor searches. The result may be empty, and searches that find nothing are the best list of articles still to write.
Hook::add('action:knowledgebase.searched', 10, function ($query, $results, $lang) {
// Searches that find nothing point at the article still to write.
if (!$results) Acme::noteGap($query, $lang);
});Changing the article body
Runs before the article body reaches the screen. Resolve your own shortcodes here, and drop in values like a version number.
Hook::add('filter:knowledgebase.article_content', 10, function (&$content, &$ctx) {
// Resolve a shortcode of your own.
$content = str_replace('[version]', Acme::currentVersion(), $content);
});Limiting which categories are visible
Runs while article queries are built. This is how you narrow the knowledge base to an audience: one set for resellers, another for end customers.
Hook::add('filter:knowledgebase.category_scope', 10, function (&$scope, &$ctx) {
// Leaving it empty means "no limit": fill it to narrow the view.
if (!Acme::isReseller()) $scope = Acme::publicCategories();
});Changing the category tree
Runs once the category tree beside the content is prepared.
Hook::add('filter:knowledgebase.category_tree', 10, function (&$tree, &$ctx) {
// Hide the internal branch from visitors.
$tree = array_values(array_filter($tree, fn ($n) => ($n['id'] ?? 0) !== Acme::INTERNAL_CAT));
});Changing the popular article list
Runs while the popular article box beside the content is filled.
Hook::add('filter:knowledgebase.popular_articles', 10, function (&$articles, &$ctx) {
// The limit arrives in the context: the box does not stretch.
array_unshift($articles, Acme::featuredArticle());
$articles = array_slice($articles, 0, (int) ($ctx['limit'] ?? 6));
});Changing the search results
Runs before the search results reach the screen. Add results from a source of your own here, or change the order.
Hook::add('filter:knowledgebase.search_results', 10, function (&$results, &$ctx) {
// Add results from a source of your own.
foreach (Acme::search($ctx['query'] ?? '', $ctx['lang'] ?? '') as $row) $results[] = $row;
});Pitfalls
The scope list always arrives empty, and leaving it that way means "show the whole knowledge base". Wanting to narrow access and leaving the list empty does the opposite.
The article body goes straight to the screen and is not cleaned. If you embed a value from outside, escape it yourself.
Related Articles
- Knowledge Base and Notification Hooks
- Notification Hooks
- How Hooks Work
Thanks for your feedback!
Our support team is here around the clock for anything you can't find above.