[joomla][轉載]開發Joomla的plugin外掛

有時候用久別人提供的免費擴展套件,總是認為不夠好,上網找找DIY的方式,不過還真是有難度,果然Joomla是站在巨人上的CMS,當巨人不是這麼容易的....

轉載來源:阿誌研究工作室

Joomla在功能擴增的彈性方面,提供了非常良好的機制,「外掛」是其中一種方式,依功能不同而區分為八大掛外分類:Authentication、Content、Editors、Editors-XTD(extended)、Search、 System、User、XML-RPC,此八種外掛以目錄名稱來存放於Joomla的plugins目錄中,本文將介紹如何開發Joomla Plugin以擴增Joomla的功能。

八種外掛功能

在開發外掛之前,首先必須瞭解這八種外掛的主要功能與目的。
  1. Authentication
    Joomla登入認證的資料是存放於資料庫,若想使用LDAP或其他登入方式,就必須使用Authentication外掛。Joomla內建已有LDAP、OpenID、Google等外掛。
  2. Content
    Content的外掛主要在於文章內容顯示的前後,經過額外的加工處理,通常是針對文章中特定的表示法來處理呈現的結果,例如程式碼標識的外掛。在Joomla 1.5.4版之後,還可針對文章儲存的前後做處理。
  3. Editors
    在編輯文章時所使用的編輯器就是Editor外掛。
  4. Editors-XTD
    Editors-XTD是用來擴增Editor外掛的功能,例如分頁設定、附檔等外掛。
  5. Search
    Joomla的搜尋功能預設只針對內部的文章、聯絡人、網站連結等資料做搜尋,若有新元件加入時又想要能搜尋到該元件的資料,就必須要有相對映的Search外掛才可搜尋到資料。
  6. System
    System外掛是在Joomla執行時期,可以加入其他額外的動作,例如Log、cache等。
  7. User
    此外掛可對使用者登入前後或資料異動時,做另外的處理,通常也可以用來做不同系統之間的使用者資料橋接功能。
  8. XML-RPC
    Joomla內建有XML-RPC的功能,使用者可離線編輯、發佈文章 (必須要有配合的軟體),其模式類似web services,如果想對外提供額外的存取服務,可以自行開發XML-RPC外掛。

外掛開發步驟

Joomla外掛的開發步驟很簡單,只要先瞭解下列幾點開發的規則與事項,就可以很快上手開發出自己所需的外掛。
  1. 確認要實作的外掛是上述八大類別中的何種類別,以及外掛的名稱。
  2. 外掛的class名稱是有固定規則,因此確定類別與外掛名稱後,即可決定class名稱,其規則如下:
    plg + 外掛種類 + 外掛名稱,例如Content外掛名稱為MyBlock,則class名稱為 plgContentMyBlock
  3. 外掛的class必須繼承JPlugin,須 jimport( 'joomla.plugin.plugin' )
  4. 實作外掛所需處理的事件。不同種類的外掛會有不同的觸發事件,如下表所示。不同的事件會有不同的參數,可至Joomla Document查看,或是直接看Joomla plugins分類目錄下的example檔。
  5. 最後就是撰寫外掛描述檔,主要為安裝外掛時使用。
種類事件
Authentication
  • onAuthenticate
Content
  • onPrepareContent
  • onAfterDisplayTitle
  • onBeforeDisplayContent
  • onBeforeContentSave (new in 1.5.4)
  • onAfterContentSave (new in 1.5.4)
Editors
  • onInit
  • onGetContent
  • onSetContent
  • onSave
  • onDisplay
  • onGetInsertMethod
Editors XTD
  • onDisplay
Seach
  • onSearch
  • onSearchAreas
System
  • onAfterInitialise
  • onAfterRoute
  • onAfterDispatch
  • onAfterRender
User
  • onLoginUser
  • onLoginFailure
  • onLogoutUser
  • onLogoutFailure
  • onBeforeStoreUser
  • onAfterStoreUser
  • onBeforeDeleteUser
  • onAfterDeleteUser
XML-RPC
  • onGetWebServices

外掛實作範例

瞭解外掛的開發步驟與種類之後,接下來將以一個範例程式來說明外掛實作的過程。以下的外掛功能主要為將文章中[block] [/block]所包起來的文字呈現於方框中並以粗體顯示,其外掛名稱為MyBlock。因為外掛是處理文章的呈現,故該外掛則為Content類別的外掛。程式碼如下所示。
MyBlock.php
  1. // Check to ensure this file is included in Joomla!
  2. defined( '_JEXEC' ) or die( 'Restricted access' );
  3. jimport( 'joomla.plugin.plugin' );
  4. /**
  5.  * myBlock Content Plugin
  6.  */
  7. class plgContentMyBlock extends JPlugin
  8. {
  9.     function plgContentMyBlock( &$subject, $params )
  10.     {
  11.         parent::__construct( $subject, $params );
  12.     }
  13.     /**
  14.      * Example prepare content method
  15.      *
  16.      * Method is called by the view
  17.      *
  18.      * @param  object   The article object.  Note $article->text is also available
  19.      * @param  object   The article params
  20.      * @param  int     The 'page' number
  21.      */
  22.     function onPrepareContent( &$article, &$params, $limitstart )
  23.     {
  24.         global $mainframe;
  25.         if ( JString::strpos( $article->text, 'block]' ) === false ) {
  26.           return true;
  27.         }
  28.         $regex = "#\[block\](.*?)\[/block]#s";
  29.         $article->text = preg_replace_callback($regex,
  30.                                                array($this, 'blockText'),
  31.                                                $article->text );
  32.     }
  33.  
  34.     function blockText(&$matches) {
  35.         return '
    '
  36.                . $matches[1] . '
    ';
  37.     }
  38. }
  39. ?>
MyBlock.xml (安裝描述檔)
  1. version="1.0" encoding="utf-8"?>
  2. version="1.5" type="plugin" group="content">
  3.     >Content - MyBlock>
  4.     >Huang-Chih Hsieh>
  5.     >December 2008>
  6.     >Copyright (C) 2008 AChih Workshop. All rights reserved.>
  7.     >http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL>
  8.     > javachih@gmail.com >
  9.     >www.achih.idv.tw>
  10.     >1.0>
  11.     >An block content plugin>
  12.     >
  13.         plugin="MyBlock">MyBlock.php>
  14.     >
  15.     />
  16. >
安裝描述檔不管是元件、模組、外掛或是佈景主題,都必須要有一個安裝描述檔,用來說明程式的種類,本範例程式為Content的外掛,所以type屬性為"plugin"、group屬性為"content",其餘的XML Tag是有關程式、作者、版權的一些描述,比較重要的是 tag必須指明外掛所需安裝的檔案,若有多個檔案需安裝,則每一檔案都必須以指明。外掛若有參數可設定,則可以在標籤設定,本範例無設定參數,在此不多做說明,若想瞭解參數設定方式,可參考Joomla內建外掛的寫法。
接下來,將MyBlock.php、MyBlock.xml壓縮為ZIP檔,並安裝至Joomla擴充套件中。安裝之後,記得到外掛管理中啟動。
馬上試一下自己寫的外掛看看效果如何。

加上[block] [/block]的文字果然呈現在框框之中了。

留言