DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:action_plugins

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
devel:action_plugins [2014-05-06 20:11] – [Working principle] Klap-indevel:action_plugins [2023-09-01 17:10] Klap-in
Line 12: Line 12:
 An Action Plugin //Example// needs: An Action Plugin //Example// needs:
   * class name  ''action_plugin_example''   * class name  ''action_plugin_example''
-  * which extends [[xref>DokuWiki_Action_Plugin]]((defined in ''lib/plugins/action.php'')). +  * which extends [[xref>ActionPlugin]]((defined in ''[[xref>inc/Extension/ActionPlugin.php]]'')). 
   * to be stored in a file ''lib/plugins/example/action.php''.   * to be stored in a file ''lib/plugins/example/action.php''.
 Moreover, a [[plugin_info|plugin.info.txt]] file is needed. For full details of plugins and their files and how to create more action components refer to [[plugin file structure]]. Moreover, a [[plugin_info|plugin.info.txt]] file is needed. For full details of plugins and their files and how to create more action components refer to [[plugin file structure]].
Line 18: Line 18:
   * The plugin must declare one method ''register()'' and some handler that is registered there.   * The plugin must declare one method ''register()'' and some handler that is registered there.
   * External libs must be loaded at the time the plugin needs them or in the constructor and not at the top of the file   * External libs must be loaded at the time the plugin needs them or in the constructor and not at the top of the file
 +
 ==== Required methods ==== ==== Required methods ====
  
 An action plugin requires at least two methods: An action plugin requires at least two methods:
-  * **''register(Doku_Event_Handler $controller)''** Use this method to register your handlers with the DokuWiki's event controller +  * **''register(EventHandler $controller)''** Use this method to register your handlers with the DokuWiki's event controller 
-  * **''<event handler>(&$event, $param)''** Your event handler(s), that perform your actions when they are triggered.+  * **''<event handler>(Event $event, $param)''** Your event handler(s), that perform your actions when they are triggered.
  
 You can register multiple events in a single action plugin. When doing this you may need multiple ''<event handler>()'' functions. You can register multiple events in a single action plugin. When doing this you may need multiple ''<event handler>()'' functions.
Line 33: Line 34:
 <code php> <code php>
 /** /**
- * plugin should use this method to register its handlers with the DokuWiki's event controller+ * plugin should use this method to register its handlers with the DokuWiki' 
 + event controller
  *  *
- * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER+ * @param EventHandler $controller DokuWiki's event controller object.  
 +                                 Also available as global $EVENT_HANDLER
  *  *
  * @return not required  * @return not required
  */  */
-public function register(Doku_Event_Handler $controller) { +public function register(EventHandler $controller) { 
-    $controller->register_hook(<EVENT NAME>, <EVENT ADVISE>, $this, <event handler function>, <parameters to be passed to event handler>,<sequence number>);+    $controller->register_hook(<EVENT NAME>, <EVENT ADVISE>, $this, <event handler function>, <parameter to be passed to event handler>,<sequence number>);
 } }
 </code> </code>
Line 49: Line 52:
   - ''$this'': An object reference to your action class containing the ''<event handler function>'', usually ''$this''.   - ''$this'': An object reference to your action class containing the ''<event handler function>'', usually ''$this''.
   - ''<event handler function>'': Name of the function to handle the event as string.   - ''<event handler function>'': Name of the function to handle the event as string.
-  - ''<parameters>'': (optional) this will passed directly and unchanged to your ''<event handler function>''+  - ''<parameter>'': (optional) parameter will passed directly and unchanged to your ''<event handler function>()''
-  - ''<sequence number>'': //(optional) used to affect the order in which hooks are executed.//+  - ''<sequence number>'': (optional) used to affect the order in which hooks are executed. Defaults to 0. It is recommended to use ranges of sequence numbers and avoid +/- PHP_INT_MAX: 
 + 
 +|  -3999 - -3000  | for "very early" | 
 +|  -2999 - -2000  | for "earlier"
 +|  -1999 - -1000  | for "early"
 +|  -999 - -1  | for "earlier than default"
 +|  0  | default | 
 +|  1 - 999  | for "later than default"
 +|  1000 - 1999  | for "late"
 +|  2000 - 2999  | for "later"
 +|  3000 - 3999  | for "very late" |
  
 ===== <event handler>() method ===== ===== <event handler>() method =====
Line 64: Line 77:
  * custom event handler  * custom event handler
  *  *
- * @param Doku_Event $event  event object by reference + * @param Event $event event object by reference 
- * @param mixed      $param  the parameters passed to register_hook when this  + * @param mixed $param the parameters passed to register_hook when this  
-                           handler was registered+                     handler was registered
  *  *
  * @return   not required  * @return   not required
  */  */
-public function <event_handler>(Doku_Event &$event, $param) {+public function <event_handler>(Event $event, $param) {
      // custom script statements ...      // custom script statements ...
 } }
Line 107: Line 120:
   * Add javascript information to "script" meta headers as array type.   * Add javascript information to "script" meta headers as array type.
  
-<code php action.php>+<code php lib/plugins/example/action.php>
 <?php <?php
 +
 +use dokuwiki\Extension\ActionPlugin;
 +use dokuwiki\Extension\EventHandler;
 +use dokuwiki\Extension\Event;
 +
 /** /**
- * Example Action Plugin:   Example Component.+ * Example Action Plugin: Example Component.
  *  *
- * @author     Samuele Tognini <samuele@cli.di.unipi.it>+ * @author Samuele Tognini <samuele@cli.di.unipi.it>
  */  */
  
-if(!defined('DOKU_INC')) die(); +class action_plugin_example extends ActionPlugin {
- +
- +
-class action_plugin_example extends DokuWiki_Action_Plugin {+
  
     /**     /**
      * Register its handlers with the DokuWiki's event controller      * Register its handlers with the DokuWiki's event controller
      */      */
-    public function register(Doku_Event_Handler $controller) {+    public function register(EventHandler $controller) {
         $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this,         $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this,
                                    '_hookjs');                                    '_hookjs');
Line 133: Line 148:
      * @author Samuele Tognini <samuele@cli.di.unipi.it>      * @author Samuele Tognini <samuele@cli.di.unipi.it>
      */      */
-    public function _hookjs(&$event, $param) { +    public function _hookjs(Event $event) { 
-        $event->data['script'][] = array( +        $event->data['script'][] = [ 
-                            'type'    => 'text/javascript', +            'type'    => 'text/javascript', 
-                            'charset' => 'utf-8', +            'charset' => 'utf-8', 
-                            '_data'   => '', +            '_data'   => '', 
-                            'src'     => DOKU_PLUGIN.'example/example.js');+            'src'     => DOKU_PLUGIN . 'example/example.js' 
 +        ];
     }     }
-}</code>+} 
 +</code>
  
 ==== Sample: add toolbar button ==== ==== Sample: add toolbar button ====
Line 149: Line 166:
   * adds a button definition to the event's ''data''   * adds a button definition to the event's ''data''
  
-<code php addbuton.php>+<code php lib/plugin/example/action.php>
 <?php <?php
 +
 +use dokuwiki\Extension\ActionPlugin;
 +use dokuwiki\Extension\EventHandler;
 +use dokuwiki\Extension\Event;
 +
 /** /**
  * Example Action Plugin: Inserts a button into the toolbar  * Example Action Plugin: Inserts a button into the toolbar
Line 157: Line 179:
  */  */
  
-if (!defined('DOKU_INC')) die(); +class action_plugin_example extends ActionPlugin {
- +
-class action_plugin_actionexample extends DokuWiki_Action_Plugin {+
  
     /**     /**
      * Register the eventhandlers      * Register the eventhandlers
      */      */
-    public function register(Doku_Event_Handler $controller) { +    public function register(EventHandler $controller) { 
-        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', array ());+        $controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button', []);
     }     }
  
Line 171: Line 191:
      * Inserts the toolbar button      * Inserts the toolbar button
      */      */
-    public function insert_button($event, $param) { +    public function insert_button(Event $event, $param) { 
-        $event->data[] = array (+        $event->data[] = [
             'type' => 'format',             'type' => 'format',
             'title' => $this->getLang('qb_abutton'),             'title' => $this->getLang('qb_abutton'),
Line 178: Line 198:
             'open' => '<abutton>',             'open' => '<abutton>',
             'close' => '</abutton>',             'close' => '</abutton>',
-        );+        ];
     }     }
  
devel/action_plugins.txt · Last modified: 2023-09-01 23:52 by Klap-in

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki