[joomla][轉載]開發Joomla的plugin外掛
有時候用久別人提供的免費擴展套件,總是認為不夠好,上網找找DIY的方式,不過還真是有難度,果然Joomla是站在巨人上的CMS,當巨人不是這麼容易的....
轉載來源:阿誌研究工作室
Joomla在功能擴增的彈性方面,提供了非常良好的機制,「外掛」是其中一種方式,依功能不同而區分為八大掛外分類:Authentication、Content、Editors、Editors-XTD(extended)、Search、 System、User、XML-RPC,此八種外掛以目錄名稱來存放於Joomla的plugins目錄中,本文將介紹如何開發Joomla Plugin以擴增Joomla的功能。
轉載來源:阿誌研究工作室
Joomla在功能擴增的彈性方面,提供了非常良好的機制,「外掛」是其中一種方式,依功能不同而區分為八大掛外分類:Authentication、Content、Editors、Editors-XTD(extended)、Search、 System、User、XML-RPC,此八種外掛以目錄名稱來存放於Joomla的plugins目錄中,本文將介紹如何開發Joomla Plugin以擴增Joomla的功能。
八種外掛功能
在開發外掛之前,首先必須瞭解這八種外掛的主要功能與目的。- Authentication
Joomla登入認證的資料是存放於資料庫,若想使用LDAP或其他登入方式,就必須使用Authentication外掛。Joomla內建已有LDAP、OpenID、Google等外掛。 - Content
Content的外掛主要在於文章內容顯示的前後,經過額外的加工處理,通常是針對文章中特定的表示法來處理呈現的結果,例如程式碼標識的外掛。在Joomla 1.5.4版之後,還可針對文章儲存的前後做處理。 - Editors
在編輯文章時所使用的編輯器就是Editor外掛。 - Editors-XTD
Editors-XTD是用來擴增Editor外掛的功能,例如分頁設定、附檔等外掛。 - Search
Joomla的搜尋功能預設只針對內部的文章、聯絡人、網站連結等資料做搜尋,若有新元件加入時又想要能搜尋到該元件的資料,就必須要有相對映的Search外掛才可搜尋到資料。 - System
System外掛是在Joomla執行時期,可以加入其他額外的動作,例如Log、cache等。 - User
此外掛可對使用者登入前後或資料異動時,做另外的處理,通常也可以用來做不同系統之間的使用者資料橋接功能。 - XML-RPC
Joomla內建有XML-RPC的功能,使用者可離線編輯、發佈文章 (必須要有配合的軟體),其模式類似web services,如果想對外提供額外的存取服務,可以自行開發XML-RPC外掛。
外掛開發步驟
Joomla外掛的開發步驟很簡單,只要先瞭解下列幾點開發的規則與事項,就可以很快上手開發出自己所需的外掛。
- 確認要實作的外掛是上述八大類別中的何種類別,以及外掛的名稱。
- 外掛的class名稱是有固定規則,因此確定類別與外掛名稱後,即可決定class名稱,其規則如下:
plg + 外掛種類 + 外掛名稱,例如Content外掛名稱為MyBlock,則class名稱為 plgContentMyBlock。
- 外掛的class必須繼承JPlugin,須 jimport( 'joomla.plugin.plugin' )。
- 實作外掛所需處理的事件。不同種類的外掛會有不同的觸發事件,如下表所示。不同的事件會有不同的參數,可至Joomla Document查看,或是直接看Joomla plugins分類目錄下的example檔。
- 最後就是撰寫外掛描述檔,主要為安裝外掛時使用。
種類 事件
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
-
-
// Check to ensure this file is included in Joomla!
-
-
jimport( 'joomla.plugin.plugin' );
-
/**
-
* myBlock Content Plugin
-
*/
-
class plgContentMyBlock extends JPlugin
-
{
-
function plgContentMyBlock( &$subject, $params )
-
{
-
parent::__construct( $subject, $params );
-
}
-
/**
-
* Example prepare content method
-
*
-
* Method is called by the view
-
*
-
* @param object The article object. Note $article->text is also available
-
* @param object The article params
-
* @param int The 'page' number
-
*/
-
function onPrepareContent( &$article, &$params, $limitstart )
-
{
-
global $mainframe;
-
-
return true;
-
}
-
$regex = "#\[block\](.*?)\[/block]#s";
-
-
-
$article->text );
-
}
-
-
function blockText(&$matches) {
-
return '
'
-
. $matches[1] . '
';
-
}
-
}
-
?>
MyBlock.xml (安裝描述檔)
-
version="1.0" encoding="utf-8"?>
-
version="1.5" type="plugin" group="content">
-
> Content - MyBlock>
-
> Huang-Chih Hsieh>
-
> December 2008>
-
> Copyright (C) 2008 AChih Workshop. All rights reserved.>
-
> http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL>
-
-
> www.achih.idv.tw>
-
> 1.0>
-
> An block content plugin>
-
>
-
plugin="MyBlock"> MyBlock.php>
-
>
-
/>
-
>
-
安裝描述檔不管是元件、模組、外掛或是佈景主題,都必須要有一個安裝描述檔,用來說明程式的種類,本範例程式為Content的外掛,所以type屬性為"plugin"、group屬性為"content",其餘的XML Tag是有關程式、作者、版權的一些描述,比較重要的是 tag必須指明外掛所需安裝的檔案,若有多個檔案需安裝,則每一檔案都必須以 指明。外掛若有參數可設定,則可以在標籤設定,本範例無設定參數,在此不多做說明,若想瞭解參數設定方式,可參考Joomla內建外掛的寫法。
接下來,將MyBlock.php、MyBlock.xml壓縮為ZIP檔,並安裝至Joomla擴充套件中。安裝之後,記得到外掛管理中啟動。
馬上試一下自己寫的外掛看看效果如何。
加上[block] [/block]的文字果然呈現在框框之中了。
plg + 外掛種類 + 外掛名稱,例如Content外掛名稱為MyBlock,則class名稱為 plgContentMyBlock。
- onAuthenticate
- onPrepareContent
- onAfterDisplayTitle
- onBeforeDisplayContent
- onBeforeContentSave (new in 1.5.4)
- onAfterContentSave (new in 1.5.4)
- onInit
- onGetContent
- onSetContent
- onSave
- onDisplay
- onGetInsertMethod
- onDisplay
- onSearch
- onSearchAreas
- onAfterInitialise
- onAfterRoute
- onAfterDispatch
- onAfterRender
- onLoginUser
- onLoginFailure
- onLogoutUser
- onLogoutFailure
- onBeforeStoreUser
- onAfterStoreUser
- onBeforeDeleteUser
- onAfterDeleteUser
- onGetWebServices
- // Check to ensure this file is included in Joomla!
- jimport( 'joomla.plugin.plugin' );
- /**
- * myBlock Content Plugin
- */
- class plgContentMyBlock extends JPlugin
- {
- function plgContentMyBlock( &$subject, $params )
- {
- parent::__construct( $subject, $params );
- }
- /**
- * Example prepare content method
- *
- * Method is called by the view
- *
- * @param object The article object. Note $article->text is also available
- * @param object The article params
- * @param int The 'page' number
- */
- function onPrepareContent( &$article, &$params, $limitstart )
- {
- global $mainframe;
- return true;
- }
- $regex = "#\[block\](.*?)\[/block]#s";
- $article->text );
- }
- function blockText(&$matches) {
- return '
' - . $matches[1] . '';
- }
- }
- ?>
- version="1.0" encoding="utf-8"?>
version="1.5" type="plugin" group="content"> > Content - MyBlock>> Huang-Chih Hsieh>> December 2008>> Copyright (C) 2008 AChih Workshop. All rights reserved.>> http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL>> www.achih.idv.tw>> 1.0>> An block content plugin>> plugin="MyBlock"> MyBlock.php>- >
/> - >
留言
張貼留言
留言請注意禮節與尊重他人,良好的交流環境需要你我共同維護。
VtigerCRM 相關留言討論,請改至FaceBook社團申請加入使用
https://www.facebook.com/groups/vTigerCRMtoTaiwan/