KnowledgeTree Plugins, Development and ConsultancyKTPlugins.com KnowledgeTree insights and news
  • Home
  • Blog
  • Plugins
    • Custom Plugins
    • Free Plugins
  • Services
  • About
  • Contact Us

Plugin Framework Advanced Trigger Tutorial

Plugin Framework Advanced Trigger Tutorial

As plugin developers we happily use the KnowledgeTree plugin framework on a day-to-day basis. In my opinion it’s greatest feature is that it allows developers to customize the application to great extent without modifying it’s actual source code. Although one of the great advantages of open source is being able to modify the source code in practice this can cause some problems. Besides issues like possibly losing support the issues that bothers me most is that once a new version of the software is release you have to migrate all of your modifications to the new version making upgrading costly.

So we’ve made it our policy to never modify any actual KnowledgeTree sourcecode in our work and up to today we’ve never violated this policy. This policy does requires us to be creative when developing our software to deal with the limitations of the KnowledgeTree framework. Although the framework offers useful entry points for adding new functionality like document actions, dashlets, admin pages it offers a lot less tools to do more advanced things like modifying or enhancing existing functionality. The only feature the KnowledgeTree framework offers are triggers which may also be known to some as ‘hooks’ which provide some, but limited, integration possibilities.

To solve this problem we’ve added a feature we call Advanced Triggers to our KTPlugins.com Framework which complements the KnowledgeTree plugin framework. Advanced triggers allow you to intercept execution and rendering of any page. We’ve implemented these triggers on top of the existing KnowledgeTree plugin framework so technically this was already possible, we just made it a lot easier, robust and flexible.

Read on to learn how to use this functionality.

  • Intended audience: KnowledgeTree Plugin Developers
  • Assumed knowledge: KnowledgeTree Plugin Development, PHP

Output Triggers

Output triggers are executed just before a page it outputted to the browser after KnowledgeTree has done all it’s processing. Your trigger receives the raw HTML as a parameter, is allowed to modify it, and is expected to return the modified HTML. We e.g. use this feature on the Document Details page to provide additional information about documents. Let’s skip to the code:

  1. class ExampleOutputTrigger {
  2.    function runTrigger($html){
  3.       //replace document workflow page with custom output
  4.       if ($_GET['kt_path_info'] == 'ktcore.actions.document.workflow' && is_numeric($_GET['fDocumentId'])) {
  5.           $html = "<b>Intercepted!</b>";
  6.       }
  7.    return $html;
  8.    }
  9. }

This trigger changes the output of the workflow page of documents to “Intercepted!“. The runTrigger method is called for every page, the if statement determines on which page(s) the trigger’s logic is executed.

Render Triggers

Render Triggers are a little different in the sense that they are executed before all KnowledgeTree logic is executed. These types of triggers are useful for saving the results of forms etc. The code is very similar:

  1. class ExampleRenderTrigger {
  2.        function runTrigger(){
  3.             //redirect all requests to ktplugins.com
  4.             if ($_GET['kt_path_info'] == 'ktcore.actions.document.workflow' && is_numeric($_GET['fDocumentId'])) {
  5.                  header("Location: http://www.ktplugins.com");
  6.                  exit;
  7.             }
  8.       }
  9. }

This trigger redirects all requests to the workflow page of a document to http://www.ktplugins.com.

Trigger Registration

We’ve seen how to develop the advanced triggers, but now let’s look at how to register them in order to get them executed. Advanced triggers need to be registered at the same time you would register conventional triggers; at plugin registration. Let’s look at an example:

  1. class ExamplePlugin{
  2.      var $sNamespace = "example.namespace";
  3.  
  4.      function setup(){
  5.           KTPUtil::registerRenderTrigger($this->sNamespace, "ExampleRenderTrigger ", "/plugins/ExamplePlugin/ExampleTriggers.php");
  6.           KTPUtil::registerAdminRenderTrigger($this->sNamespace, "ExampleAdminRenderTrigger ", "/plugins/ExamplePlugin/ExampleTriggers.php");
  7.           KTPUtil::registerOutputTrigger($this->sNamespace, "ExampleOutputTrigger ", "/plugins/ExamplePlugin/ExampleTriggers.php");
  8.           KTPUtil::registerAdminOutputTrigger($this->sNamespace, "ExampleAdminOutputTrigger ", "/plugins/ExamplePlugin/ExampleTriggers.php");
  9.      }
  10.  
  11. }

The KTPUtil::register***Trigger methods have a three parameters:

  • The plugin namespace
  • The name of the trigger class
  • The file where the trigger class is located

You probably noticed the introduction of the registerAdminOutputTrigger and the registerAdminRenderTrigger methods. These provide the same functionality for their “non-admin” counterparts with the only difference that admin triggers are executed for admin pages whereas normal triggers are only executed for frontend pages.

This concludes this tutorial. If you’re ready to use the advanced triggers download the KTPlugins.com KnowledgeTree Plugin Framework and get started!



Contact Information

Johanna Naber-erf 215
3315 HG Dordrecht
The Netherlands
info@ktplugins.com
+1.646.257.3764

BTW Nr. NL818575037B01
K.V.K. Rotterdam 23087694
  • Home
  • Blog
  • Plugins
  • Services
  • About
  • Contact Us
Startmotor Copyright © KTPlugins.com - 2010