Jump to content

Including modifiers when extending nodes: Difference between revisions

From NusaATK
Juneih (talk | contribs)
No edit summary
Despam
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Howto|easy|[[June Henriksen]] <juneih@linpro.no>}}
{{Howto|advanced|[[June Henriksen]] <juneih@linpro.no>}}


This howto explains how you can include modifiers when you extend a node.
== Intro ==


All modifiers added to a node runs from the node's init function. Which modifiers that runs however, depends on the m_type of a node. When you extend a node, you get a new m_type if you set the atkNode in the constructor.
ATK features a concept called '[[modifiers]]' that allows one to change functionality of a node without touching the original code of the node (See the '[[Using modifiers]]' howto).
 
The way modifiers work however, prevents modifiers from being applied to classes that extend the node. This may be fixed in a later version of ATK. For now, you can use this howto which presents a workaround to this problem.
 
== Adding modifiers to derived nodes ==
 
All modifiers added to a node run from the node's init function. Which modifiers that runs however, depends on the m_type of a node. When you extend a node, you get a new m_type if you set the atkNode in the constructor.


<span style="color: #000000; font-style: italic;">
<span style="color: #000000; font-style: italic;">
Line 12: Line 18:


<p></p>
<p></p>
The following is an example init_node for a node that extends the hours node:
The following is an example init_node for a node that extends the timereg.hours node:
<p></p>
<p></p>


<span style="color: #000000; font-style: italic;">
<syntaxhighlight lang="php">
 
  function init()
function init()
   {
   {
     $ret = parent::init();
     $ret = parent::init();
Line 24: Line 29:
     $this->m_type='hours';
     $this->m_type='hours';
     $this->m_module='timereg';
     $this->m_module='timereg';
 
 
     global $g_modifiers;
     global $g_modifiers;
 
 
     if (isset($g_modifiers[$this->atknodetype()]))
     if (isset($g_modifiers[$this->atknodetype()]))
     {
     {
Line 37: Line 42:
     $this->m_module=$oldmodule;
     $this->m_module=$oldmodule;
     $this->m_type = $oldtype;
     $this->m_type = $oldtype;
 
 
     return $ret;
     return $ret;
   }
   }
</span>
</syntaxhighlight>


As mentioned before, this example is for running the modifiers added to the hours modules, but this can of course be exchanged for any other node. In fact, the only thing that needs to be changed in this code is the 'hours' part! Remember, the function itself is put in the extending node.  
As mentioned, this example is for running the modifiers added to the hours node, but this can of course be exchanged for any other node. In fact, the only thing that needs to be changed in this code is the nodename ('hours') and modulename ('timereg')! Remember, the function itself is put in the extending node.  


The result of including this is that you now will have access to all the attributes added by modifiers to the node that is extended.  
The result of including this is that you now will have access to all the attributes added by modifiers to the node that is extended.  


<p></p>
<p></p>


--[[User:juneih|juneih]] 16:29, 23 November 2006 (CEST)
--[[User:juneih|juneih]] 16:29, 23 November 2006 (CEST)

Latest revision as of 06:57, 16 July 2007

ATK Howto: Including modifiers when extending nodes

Complexity: advanced
Author: June Henriksen <juneih@linpro.no>

List of other Howto's

Intro

ATK features a concept called 'modifiers' that allows one to change functionality of a node without touching the original code of the node (See the 'Using modifiers' howto).

The way modifiers work however, prevents modifiers from being applied to classes that extend the node. This may be fixed in a later version of ATK. For now, you can use this howto which presents a workaround to this problem.

Adding modifiers to derived nodes

All modifiers added to a node run from the node's init function. Which modifiers that runs however, depends on the m_type of a node. When you extend a node, you get a new m_type if you set the atkNode in the constructor.

$this->atkNode("nodename");

In order to have the modifiers run, some changes has to be made in the extended node's init node.

The following is an example init_node for a node that extends the timereg.hours node:

  function init()
  {
    $ret = parent::init();
    $oldtype = $this->m_type;
    $oldmodule= $this->m_module;
    $this->m_type='hours';
    $this->m_module='timereg';
  
    global $g_modifiers;
  
    if (isset($g_modifiers[$this->atknodetype()]))
    {
      foreach ($g_modifiers[$this->atknodetype()] as $modulename)
      {
        $module = &getModule($modulename);
        $module->modifier($this);
      }
    }
    $this->m_module=$oldmodule;
    $this->m_type = $oldtype;
  
    return $ret;
  }

As mentioned, this example is for running the modifiers added to the hours node, but this can of course be exchanged for any other node. In fact, the only thing that needs to be changed in this code is the nodename ('hours') and modulename ('timereg')! Remember, the function itself is put in the extending node.

The result of including this is that you now will have access to all the attributes added by modifiers to the node that is extended.


--juneih 16:29, 23 November 2006 (CEST)