Joomla Content 事件

2023-03-10 16:00 更新

內(nèi)容事件在內(nèi)容創(chuàng)建過程中觸發(fā)。這些事件在大多數(shù)的組件和視圖中被觸發(fā) - 不限定于com_content組件。

onContentPrepare

說明

這是準(zhǔn)備輸出內(nèi)容的第一步,大多數(shù)內(nèi)容插件可以響應(yīng)本事件完成功能。由于文章和相關(guān)參數(shù)是通過引用傳遞的,因此事件處理程序可以在顯示之前對其進(jìn)行修改。

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • &article (引用傳遞)內(nèi)容對象 ,不同的觸發(fā)組件可能內(nèi)容不一樣(如: 對于文章組建,我們可以通過 $article->text 得到文章的內(nèi)容).
  • &params (引用傳遞)內(nèi)容的參數(shù). 
  • page An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

返回值

無或者True / False.

示例

onContentAfterTitle

說明

這個事件僅存在于 Joomla 3.x 及以上版本. 它的作用就是在文章輸出標(biāo)題后允許你輸出內(nèi)容. 盡管這個插件傳遞的參數(shù)也是引用,但不建議在這個插件中修改內(nèi)容,應(yīng)該使用 onContentPrepare 事件來修改內(nèi)容. 另外,對于文章組件,這個事件具有特殊的目的,文章組件使用這個事件來實現(xiàn)文章引言輸出功能.

參數(shù)

有關(guān)這些參數(shù)的更多討論,請參見onContentPrepare事件

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • &article (引用傳遞)內(nèi)容對象 ,不同的觸發(fā)組件可能內(nèi)容不一樣(如: 對于文章組建,我們可以通過 $article->text 得到文章的內(nèi)容).
  • &params (引用傳遞)內(nèi)容的參數(shù). 
  • page An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

返回值

字符串. 插件的返回值將會輸出到占位符處. 大多數(shù)模板在文章分隔符之后顯示此占位符

onContentBeforeDisplay

說明

在內(nèi)容輸出之前觸發(fā)的事件

參數(shù)

有關(guān)這些參數(shù)的更多討論,請參見onContentPrepare事件. 注意 不同于 onContentPrepare, 這個插件的參數(shù)都是值傳遞.

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • article 內(nèi)容對象 ,不同的觸發(fā)組件可能內(nèi)容不一樣(如: 對于文章組建,我們可以通過 $article->text 得到文章的內(nèi)容).
  • params 內(nèi)容的參數(shù)
  • page An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

返回值

字符串. 插件的返回值將會輸出到占位符處. 大多數(shù)模板在文章分隔符之后顯示此占位符

示例

onContentAfterDisplay

說明

內(nèi)容輸出之后觸發(fā)的事件

參數(shù)

有關(guān)這些參數(shù)的更多討論,請參見onContentPrepare事件. 注意 不同于 onContentPrepare, 這個插件的參數(shù)都是值傳遞.

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • article 內(nèi)容對象 ,不同的觸發(fā)組件可能內(nèi)容不一樣(如: 對于文章組建,我們可以通過 $article->text 得到文章的內(nèi)容).
  • params 內(nèi)容的參數(shù)
  • page An integer that determines the "page" of the content that is to be generated. Note that in the context of views that might not generate HTML output, a page is a reasonably abstract concept that depends on the context.

返回值

字符串. 插件的返回值將會輸出到占位符處. 大多數(shù)模板在文章內(nèi)容之后顯示此占位符

onContentBeforeSave

說明

在內(nèi)容存儲到數(shù)據(jù)庫之前會觸發(fā)這個事件. 通過在插件返回false,可以終止保存操作. 以 JModelLegacy 舉例說明,但我們終止了保存,可以通過調(diào)用 $this->setError($table->getError); 將錯誤信息輸出給用戶

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • &article (引用傳遞)JTableContent對象 該對象代表了內(nèi)容的數(shù)據(jù) .
  • isNew bool值,如果內(nèi)容是新建的,那么就為true,否則為false
  • data 需要保存的數(shù)據(jù). 這里的data已經(jīng)經(jīng)過了組件的校驗處理. 從 Joomla 3.7后 需要插件自己負(fù)責(zé)設(shè)置這個屬性,因為在 Joomla 4.0 中核心的內(nèi)容插件會使用到這個屬性

返回值

成功返回true,失敗返回false. 這個返回值將會影響保存的處理過程。如果返回false,保存會提示失敗

onContentAfterSave

說明

將內(nèi)容保存到數(shù)據(jù)庫后,將觸發(fā)此事件.

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • article JTableContent對象 該對象代表了內(nèi)容的數(shù)據(jù) .
  • isNew bool值,如果內(nèi)容是新建的,那么就為true,否則為false

返回值

無.

onContentPrepareForm

說明

在JForm渲染之前觸發(fā)此事件. 通常可以在這個事件中修改內(nèi)存中的JForm對象 如, 使用 JForm->loadFile() 添加新的字段 或者 JForm->removeField() 移除字段. 或者使用 JForm->setFieldAttribute() 去修改表單字段的屬性。

參數(shù)

  • form JForm 對象. 使用 $form->getName() 可以檢查當(dāng)前的表單是否是你需要處理的表單
  • data 對象包含了表單的數(shù)據(jù)

返回值

  • boolean 成功返回True,失敗返回False.

示例

onContentPrepareData

說明

在獲得JForm數(shù)據(jù)的時候被觸發(fā). 通常和onContentPrepareForm一起使用,可以實現(xiàn)動態(tài)的修改表單的字段和數(shù)據(jù) 

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • data 對象包含了表單的數(shù)據(jù)

返回值

  • boolean 成功返回True,失敗返回False.

示例

onContentBeforeDelete

說明

在數(shù)據(jù)被從數(shù)據(jù)庫刪除之前觸發(fā)此事件. 通過在插件返回false,可以終止保存操作. 以 JModelLegacy 舉例說明,但我們終止了保存,可以通過調(diào)用 $this->setError($table->getError); 將錯誤信息輸出給用戶

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • data  JTableContent 對象的引用,這個對象包含了正在刪除的內(nèi)容

返回值

成功返回true,失敗返回false. 這個返回值將會影響保存的處理過程。如果返回false,保存會提示失敗

示例

onContentAfterDelete

說明

數(shù)據(jù)從數(shù)據(jù)庫中刪除之后觸發(fā)此事件

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • data  JTableContent 對象的引用,這個對象包含了已經(jīng)刪除的內(nèi)容

返回值

示例

content/finder/finder.php

onContentChangeState

說明

當(dāng)內(nèi)容的狀態(tài)改變的時候觸發(fā)此事件 (如.發(fā)布或者停止發(fā)布).

參數(shù)

  • context 內(nèi)容的上下文 - 通常是組件和視圖的拼接字符串 - 或者模塊的名稱 (如: com_content.article). 通過這個參數(shù)可以判斷當(dāng)前是否是我們需要處理的環(huán)境.
  • $pks 內(nèi)容主鍵值(id)得數(shù)組.
  • $value 新的狀態(tài)值.

返回值

示例

content/finder/finder.php

onContentSearch

說明

此事件由各種與搜索相關(guān)的操作觸發(fā). 插件響應(yīng)此事件來返回對應(yīng)的搜索結(jié)果. 返回的結(jié)果必須包含下面的字段:

  • browsernav
  • catslug
  • created
  • href
  • section
  • slug
  • text
  • title

參數(shù)

  • $text 搜索的字符串.
  • $phrase 匹配模式 (exact|any|all). 默認(rèn)為 "any".
  • $ordering 排序選項 (newest|oldest|popular|alpha|category). 默認(rèn) "newest".
  • $areas 搜索返回的數(shù)組,null表示搜索全部.

返回值

具有上述成員的stdClass對象數(shù)組

在以下文件中用到

onContentSearchAreas

說明

當(dāng)在輸出搜索范圍的時候這個事件被觸發(fā)

參數(shù)

返回值

一個關(guān)聯(lián)數(shù)組. 如, array( 'categories' => 'Categories' ).

在以下文件中用到

  • components/com_search/models/search.php
  • plugins/search/categories.php
  • plugins/search/contacts.php
  • plugins/search/content.php
  • plugins/search/newsfeeds.php
  • plugins/search/tags/tags.php
  • plugins/search/weblinks.php


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號