Joomla文档中文翻译 - 如何创建并使用插件对文章内容进行修改 I

Joomla文档中文翻译如需转载,请注明官方英文文档及本人译文的出处,谢谢。查看官方英文文档: Make changes to an article automatically using a plugin

概述

插件(plugin)是一种很容易创建的扩展。一个简单的插件只需要两个文件,非常小巧。

插件的一个典型应用就是每当保存一篇文章的时候,对该文章进行某种自定义处理例如:

  • 改变文章的发布状态。
  • 为文章批量增加某种标准化的文字。
  • 为文章设定section或category.

下面我们来看看如何通过创建简单的插件实现这些需求。

第一个插件

在原生的Joomla系统中,如果一个属于“作者”组的用户提交了一篇新文章,那么这篇文章默认的状态将是“未发布”。然而,一旦管理员确认并发布了该文 章,这名用户就可以编辑这篇文章了,而这篇文章将永远处于“已发布”的状态。在我们的例子中,我们希望改变这种模式,即希望每次用户进行了文章编辑之后,文章的状态都会变成“未发布”。下面我们来创建第一个插件。

一个简单的插件至少要包括两个文件:一个XML文件,用来告诉Joomla如何安装该插件;另外一个是负责插件实际功能的PHP文件。

为了让人们可以更容易的编写自己的插件,Joomla在完成安装后会提供插件范本。因为我们需要通过插件来修改文章内容,所以要编写的是针对“内容”(content)的插件。我们所需要的插件范本位于:

  • Joomla根路径/plugins/content/example.xml
  • Joomla根路径/plugins/content/example.php

我们将自己的插件命名为modifyarticle,于是XML文件的名字就是modifyarticle.xml.我们只需要将 plugins/content/example.xml文件中的代码复制粘帖到我们自己的空白的modifyarticle.xml中,将该文件置于一个文件夹,并将该文件夹放在Joomla根路径之外(我们之后会使用Joomla的安装程序将该插件安装到系统中,所以现在只需要将它独立的置于 Joomla系统之外就可以)。下面我们将modifyarticle.xml文件的代码修改为:

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="content">
<name>Modify Edited Articles</name>
<creationDate>January 2009</creationDate>
<author>Joomla Doc Team</author>
<authorEmail> This e-mail address is being protected from spambots. You need JavaScript enabled to view it </authorEmail>
<authorUrl>http://joomlacode.org</authorUrl>
<copyright>Copyright (c) 2009</copyright>
<license>GPL</license>
<version>1.0.0</version>
<description>Example plugin for Joomla! wiki.</description>
<files>
<filename plugin="modifyarticle">modifyarticle.php</filename>
</files>
</install>

接下来,我们将plugins/content/example.php文件中的代码复制粘帖到我们自己的空白的modifyarticle.php文件中;与modifyarticle.xml一样,将PHP文件放在之前的那个独立的文件夹中。然后我们将代码修改为:

<?php
/**
* @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
*/


// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentModifyArticle extends JPlugin
{

/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for plugins
* because func_get_args ( void ) returns a copy of all passed arguments NOT references.
* This causes problems with cross-referencing necessary for the observer design pattern.
*
* @param object $subject The object to observe
* @param object $params The object that holds the plugin parameters
* @since 1.5
*/

function plgContentModifyArticle ( &$subject, $params )
{
parent::__construct( $subject, $params );
}

/**
* Example before save content method
*
* Method is called right before content is saved into the database.
* Article object is passed by reference, so any changes will be saved!
* NOTE: Returning false will abort the save with an error.
* You can set the error by calling $article->setError($message)
*
* @param object A JTableContent object
* @param bool If the content is just about to be created
* @return bool If false, abort the save
*/

function onBeforeContentSave( &$article, $isNew )
{
global $mainframe;
$user =& JFactory::getUser(); // get the user
if ($user->usertype == 'Author') { // unpublish if user is "Author"
$article->state = 0; // from dev 11:59
}
return true;
}
}

注意,在原来的example.php文件中,构造器(constructor)含有6个方法(methods)。这些方法涵盖了6种通常可能用到的事件应用。在我们的例子中,我们只需要在文章被保存到数据库之前对文章进行修改,所以我们将我们的方法(函数)命名为"onBeforeContentSave".其名称必须准确,它会告诉Joomla合适运行我们的插件对文章进行处理。

上面的代码即可实现我们的需求。首先获得用户信息,如果用户属于“作者”组,就将文章的状态设置为0,即“未发布”状态。注意"onBeforeContentSave"方法使用$article作为参数,所以文章对象的字段和方法对我们来说都是可以调用的。

安装插件

我们已经搞定了插件的代码,接下来我们需要创建zip格式的文件包,用来安装到我们的Joomla站点中。步骤如下:

  1. 使用普通的压缩软件将我们之前创建的两个文件打包为Zip或Gzip文件。
  2. 进入站点管理后台中的“扩展”→“安装/卸载”(Extensions → Install / Uninstall)。
  3. 点击“浏览”(Browse)并找到我们之前创建的压缩包。
  4. 点击“上传文件”(Upload File)并执行安装(Install)。成功安装后会看到系统提示。
  5. 进入“扩展”→“插件管理”(Extensions → Plugin Manager),找到我们新安装的插件(根据我们在XML文件中的设置,插件名应该是“Modify Edited Articles”),激活。

搞定。

待续——第二部分:如何创建并使用插件为文章添加标准化文字;设置section或category等。

译者:viiiix.com


收藏与分享

添加评论


Security code
换一张图