Building extensions - the life cycle

An important part of building extensions, is understanding the life cycle. The life cycle allows for extensions to interact with the framework and other extensions, at different stages during execution.

/LifeCycle.png

The image above demonstrates how the life cycle works. First the framework executes the PreInit function on every extension, then executes the Init function for every extension, and so on. This behaviour allows for all extensions to interact with each other at different stages.

An example of interaction between extensions, is when links are added to the menu extension (SMMenu). First SMMenu loads all links from the database (during Init). When RenderComplete is executed, the Admin menu item is added, if the user is logged in to the administration section. This allows for other extensions to add their links to the Admin menu item when the function PreTemplateUpdate is executed.

The table below explains the purpose of each life cycle function (event).


Function Description
PreInit The initilization events makes it possible for extensions to interact with each other. Resources, data, and functionality is made ready during PreInit, and may be consumed during Init or InitComplete, or even much later using one of the other life cycle events.
Init
InitComplete

At the stage between InitComplete and PreRender, some markup is added to the template; meta-data (generator), reference to the JavaScript library, and website title is modified.
PreRender This function executes just before the main extension is rendered. The main extension is the extension being rendered and displayed in the content area of the website. Clicking links in the administration menu is a good example of different extensions being executed as the main extension. The main extension being executed is defined in the URL: index.php?SMExt=SMLogin tells the controller to render the SMLogin extension and later display the result in the content area of the website.
Render This function is only executed on the main extension. The content for the content area of the website is rendered, but not yet inserted into the content area, which happens between PreTemplateUpdate and TemplateUpdateComplete.
RenderComplete This function executes immediately after Render has been called on the main extension (see Render explanation).
PreTemplateUpdate This function executes just before the template is filled with the content from the main extension (see Render explanation).
  An HTML Form element is inserted into the template, allowing all GUI controls within the main extension, to restore their values after post back (submit). Furthermore the result from rendering the main extension, is inserted into the content area of the template.
TemplateUpdateComplete This function executes immediately after the template has been filled with the content from the main extension (see Render explanation).
PreOutput The function executes just before the template is written/transfered to the client.
  All repeating blocks and place holders that have not been replaced within the template, is removed. The template (with content) is written/transfered to the client. Further changes to the template will not have any effect.
OutputComplete
This function executes immediately after the template has been written/transfered to the client.
Unload This function is the last chance to do database modifications, before the system commits all changes permanently.
  All data sources are committed. No more writing to data sources should take place.
Finalize This function may be used to do some final clean up before the execution terminates.


Building extensions - execution mode

Extensions may execution in two different modes; Shared or Dedicated. If an extension runs in Shared Execution Mode (default), it runs together with all the other extensions. This means that the life cycle of all extensions are executed (except for the Render function, which is only invoked on the main extension, as described earlier on this page).

An extension running in Dedicated Execution Mode executes all by it self. The entire life cycle of the given extension is executed, but no other extensions are allowed to execute code. This is handy if some code needs to run in pop-up-mode, and does not require the functionality of other extensions.

The URL parameter SMExecMode determines the execution mode of an extension; index.php?SMExt=SMLogin&SMExecMode=Dedicated. The value of SMExecMode could also be Shared. The default mode is Shared, if not specified in the URL. Not all extensions support Dedicated Execution Mode, and might thrown an exception, if attempts are made to execute in this mode.

The following snippet of code demonstrates how to determine the current execution mode.

class MyTest extends SMExtension
{
    private $sharedExecMode = true;

    public function Init()
    {
        if ($this->context->GetExecutionMode() === SMExecutionMode::$Shared)
            $this->sharedExecMode = true;
        else // SMExecutionMode::$Shared
            $this->sharedExecMode = false;
    }
}