Creating a search plugin

From Joomla! Documentation
(Difference between revisions)
Jump to: navigation, search
Line 81: Line 81:
 
}</source>
 
}</source>
 
And yet it is the time to define the parameters! First get the right plugin; 'search' (the group), 'nameofplugin'.  
 
And yet it is the time to define the parameters! First get the right plugin; 'search' (the group), 'nameofplugin'.  
<source language="PHP">$plugin =& JPluginHelper::getPlugin('search', 'nameofplugin'); </source>
+
<source lang="PHP">
 +
$plugin =& JPluginHelper::getPlugin('search', 'nameofplugin');
 +
</source>
 +
 
 
Then load the parameters of the Plugin..
 
Then load the parameters of the Plugin..
<source language="PHP">$pluginParams = new JParameter( $plugin->params );</source>
+
<source lang="PHP">
 +
$pluginParams = new JParameter( $plugin->params );
 +
</source>
 +
 
 
And define the parameters. For example like this..
 
And define the parameters. For example like this..
<source language="PHP">$limit = $pluginParams->def( 'nameofparameter', defaultsetting );</source>
+
 
 +
<source lang="PHP">
 +
$limit = $pluginParams->def( 'nameofparameter', defaultsetting );
 +
</source>
 +
 
 
Use the function trim to delete spaces in front of or at the back of the searching terms
 
Use the function trim to delete spaces in front of or at the back of the searching terms
<source language="PHP>$text = trim( $text );</source>
+
 
 +
<source langu="PHP">
 +
$text = trim( $text );
 +
</source>
  
  

Revision as of 14:47, 19 January 2008

Contents

Description

XML file

The XML file is named the same as the PHP file, and is one of the two required files. Always start off with the XML tag and define that it is written in a UTF-8 format.

<?xml version="1.0" encoding="utf-8"?>

To define that the plugin has to be a search plugin, add this line:

<install version="1.5" type="plugin" group="search">

The type will define it is a Plugin, the group defines the Plugin is in the group of Search Plugins.

After that, add some information about yourself and the Plugin, like this:

<name>Name of your Search Plugin</name>
<author>Your name</author>
<creationDate>Created Date</creationDate>
<copyright>Copyright</copyright>
<license>License, for example GNU/GPL</license>
<authorEmail>Your e-mail address</authorEmail>
<authorUrl>Your website</authorUrl>
<version>Version of the plugin</version>
<description>Description of the Plugin; showed with installation and when editing the Plugin in the Plugin Manager</description>

And now include your PHP file to the Search Plugin. The name of this file should be the same as the name of this XML file. Put this name also behind the plugin="" part.

You could also add more files for your plugin, for example an image. Just add another row between <files> and </file>, and then place the file between <filename> tags.

<files>
   <filename plugin="nameofplugin">nameofplugin.php</filename>
</files>

Optionally, you could add some parameters to the Plugin. These will look like this:

<params>
   <param name="paramname" type="typeofparameter" default="defaultsetting" label="title" description="description"/>
</params>
  • Param name: The name of the parameter. You will need this when creating the PHP file.
  • Param type: You could choose between several types of parameters. For example, "text" is used to display an input box.
  • Param default: The default setting for this parameter.
  • Param label: The name of this parameter displayed in the edit screen of this Plugin in the Plugin Manager.
  • Param description: The text which appears as a tool tip for this parameter.

And do not forget to end your XML file with the following tag:

</install>

PHP file

The PHP file of your Plugin is probably the most important file of the Plugin. First start with information about the Plugin and yourself. For example:

<?php
/**
 * @version             $Id: nameofplugin.php versionnumber date author
 * @copyright           Copyright
 * @license             License, for example GNU/GPL
 * All other information you would like to add
 */

To prevent accessing the document directly, enter this code:

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

Now define the registerEvent and the language file. Replace 'nameofplugin' by the name of your Plugin.

$mainframe->registerEvent( 'onSearch', 'plgSearchnameofplugin' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchnameofpluginAreas' );
 
JPlugin::loadLanguage( 'plg_search_nameofplugin' );

Then define a function to return an array of search areas. Replace 'nameofplugin' by the name of your Plugin.

function &plgSearchnameofpluginAreas()
{
        static $areas = array(
                'nameofplugin' => 'Nameofplugin'
        );
        return $areas;
}

Then the real function has to be created. The database connection should be made. The function will be closed with an } at the end of the file.

function plgSearchnameofplugin( $text, $phrase='', $ordering='', $areas=null )
{
        $db             =& JFactory::getDBO();
        $user   =& JFactory::getUser();

When the array is not correct, return it:

        if (is_array( $areas )) {
                if (!array_intersect( $areas, array_keys( plgSearchnameofpluginAreas() ) )) {
                        return array();
                }
        }

And yet it is the time to define the parameters! First get the right plugin; 'search' (the group), 'nameofplugin'.

$plugin =& JPluginHelper::getPlugin('search', 'nameofplugin');

Then load the parameters of the Plugin..

$pluginParams = new JParameter( $plugin->params );

And define the parameters. For example like this..

$limit = $pluginParams->def( 'nameofparameter', defaultsetting );

Use the function trim to delete spaces in front of or at the back of the searching terms

$text = trim( $text );


INI file(s)

Coding examples

Points to watch

Dependencies

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox