User

Difference between revisions of "Mike dowler"

From Joomla! Documentation

Line 58: Line 58:
 
===1. Component-wide default parameters===
 
===1. Component-wide default parameters===
  
These are defined in a file named <code>config.xml</code> which is located in <code>/PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/</code>.  The file should have the following structure:
+
These can be created at installation of the component, by including <code><param /></code> elements in the installation XML file (typically called COMPONENT_NAME.xml, and located in the root folder of the component package.  As far as the parameters are concerned, this XML file has the following structure:
 +
<source lang="xml">
 +
<install>
 +
  ...
 +
  <params>
 +
    <param />
 +
   
 +
    <!-- Example: the following will create a line in the parameters field reading 'font_size=16\n':
 +
    <param name="font_size" default="16" />
 +
    -->
 +
    ...
 +
  </params>
 +
  ...
 +
</install>
 +
</source>
 +
Each <code><param /></code> element must be given the attributes <code>name</code> and <code>default</code>, which define the parameter name and value respectively.  These values are then copied to the jos_components table in the database.
 +
 
 +
However, on its own this is not very useful, as it doesn't allow users to change the values of the parameters.  To do this, parameter form elements need to be defined in a file named <code>config.xml</code> which is located in <code>/PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/</code>.  The file should have the following structure:
 
<source lang="xml">
 
<source lang="xml">
 
<root>
 
<root>
 
   <params>
 
   <params>
 
     <param />
 
     <param />
 +
   
 +
    <!-- Example: the following will produce a text box 3 characters wide with the label 'Font size:'.  When the user hovers the mouse over the label, a tooltip will pop up which states 'Please enter the required font size'.  On saving, a value entered into the text box will be saved as the 'font_size' parameter value.
 +
    <param type="text" name="font_size" size="3" label="Font size:" description="Please enter the required font size" />
 +
    -->
 
     ...
 
     ...
 
   </params>
 
   </params>
 
</root>
 
</root>
 
</source>
 
</source>
Note that the <code><root></code> element can have any name you wish, but there must be one element at the root of the document (this is a requirement for a well-formed XML document). You can have more than one <code><params></code> element, but only the first one will be considered.
+
Note that the <code><root></code> element can have any name you wish, but there must be one element at the root of the document (this is a requirement for a well-formed XML document). You can have more than one <code><params></code> element, but only the first one will be considered.  It is not currently possible to group certain parameter form elements together.
  
 
So that administrators to access these parameters, you will need to add a button to the toolbar in the backend of your component.  This can be done using the code:
 
So that administrators to access these parameters, you will need to add a button to the toolbar in the backend of your component.  This can be done using the code:
Line 82: Line 103:
 
Clicking on the toolbar button produces a pop-up window containing the form fields for each parameter defined in <code>config.xml</code>.
 
Clicking on the toolbar button produces a pop-up window containing the form fields for each parameter defined in <code>config.xml</code>.
  
It is possible to define default values for each of these parameters in the <code><param /></code> statements in the <code>config.xml</code> file.  However, in order to save these default values to the database, a user would need to click on the <code>Parameters</code> button (at which point any field not having a pre-saved value will be given the default value), and then click the 'Save' button.  Since you won't want to rely on your users doing this before using the component, Joomla! 1.5 allows you define default values for the component parameters, to be saved to the database on installation.  These default values are set in the installation XML file (typically called <code>COMPONENT_NAME.xml</code>), which has the following structure:
+
Note that there is no need to define <code>default</code> attributes for the <code><param /></code> elements in the <code>config.xml</code> file, since the default values will already have been saved in the installation of the component.
<source lang="xml">
 
<install>
 
  ...
 
  <params>
 
    <param />
 
    ...
 
  </params>
 
  ...
 
</install>
 
</source>
 
Although the structure of these <code><param /></code> elements is superficially similar to those from the <code>config.xml</code> file, you should note that the <code><param /></code> elements from the install file are never used to produce form fields.  There is therefore no benefit to setting the types of parameter.  The only attributes required for the <code><param /></code> elements are the <code>name</code> and <code>default</code> values - it is these values which will be copied to the database on installation.
 
  
 
===2. Menu item specific parameters===
 
===2. Menu item specific parameters===

Revision as of 17:17, 20 May 2008

Using parameters with a component[edit]

Background[edit]

One thing that sets Joomla! components apart from other types of Extension (i.e. Modules and Plugins) is the ability to store significant quantities of data in the Joomla! MySQL database. Typically, this data will be content for displaying on a page (such as the content of an article, or its title) or data about how that content should be displayed (e.g. a setting specifying whether or not the title should be shown). A component normally creates at least one table in the database to store this data - for example, data for articles associated with the com_content component are stored in the jos_content table (where jos is the table prefix for the site).

However, choosing exactly how to store each item of data requires a little more thought. There are essentially two choices: each data item may be stored in its own field, or a collection of data items may be stored within a single parameters field. (Of course, there is nothing to stop you making use of both methods, for different data items, within the same component). Which approach you use for a given data item is largely a matter of personal choice, although there are some factors to consider:

  • Core code included in the Joomla! installation makes it very easy to produce the appropriate form elements for entering data into parameters in the backend. If you choose to save the data in separate fields, you will need to code the appropriate form elements yourself.
  • Again, Joomla! core code allows you to set default values for parameters across the whole component. Individual component items (e.g. individual articles) can then choose to use the default values, or to override them with values specific to that item. See below for details of how to do this.
  • It is not possible to extract individual parameter values directly from the database. Instead, the whole parameters field must be extracted and parsed to obtain the different parameter values. This means that it is not very easy, for example, to run MySQL queries that search for a parameter with a particular value. Any data that you want to run queries against should be given its own table field.
  • Similarly, it is not possible to run data validation on parameter fields before saving data.

In general, parameters are conventionally used to store data relating to the presentation of information. The information itself is generally stored in separate fields.

Types of component parameter[edit]

1. Component-wide default parameters[edit]

These parameters are set once for the whole component, and are generally used to provide the 'default' parameter settings for the component on that particular site.

2. Menu item specific parameters[edit]

A menu link to an individual component item (e.g. an article) can also have parameters associated with it, which can affect the way the component item is displayed using that link. The types of parameters available will depend on the way that the component item is displayed (the 'view' in MVC terms, but don't worry if this doesn't mean anything to you!). However, the precise values of those parameters can be set for each menu link individually. If there is another menu link to that same component item, then it is important to realise each menu link may have different parameter values, and so the same article may be displayed differently in the two cases.

A menu link can also override the component-wide default parameters, so that the override values (if set) are used in place of the default values for that component item when accessed via that menu link. As you would expect, when no override value is set, the default value is used instead, in the normal manner.

3. 'Article' specific parameters[edit]

Finally, each component item (e.g. article) can have parameters which apply to that item, regardless of how it is accessed (i.e. via any available menu link, or even without reference to a menu at all!). If the component designer wants, the article specific parameters can also be set up to use the 'override' behaviour, so that they default to the current values of the corresponding component-wide parameters, though this is not essential. Again, this is described further below.

Storing the parameters and their values.[edit]

In general, a set of parameters is stored within the database in a single text field. Different parameters are separated by newline characters ('/n' in MySQL - each parameter will appear on a different line when you look at the data in something like phpMyAdmin). Each parameter consists of the parameter name and value, separated by an equals sign (=). As an example:

parameter1=parameter1value
parameter2=
parameter3=parameter3value
parameter4=parameter4value

Here, parameter1, parameter3, and parameter4 have all been assigned values, whilst parameter2 has no value. Typically, a parameter will be given no value where it is set up to allow override of a default value, but 'no override is required' - i.e. it is desired to use the default value.

1. Component-wide default parameters[edit]

These are stored in the params field of the jos_components table, in the row corresponding to the particular component to which they apply.

2. Menu item specific parameters[edit]

These are stored in the params field of the jos_menu table, in the row corresponding to the particular menu item to which they apply.

3. 'Article' specific parameters[edit]

These are stored in a text field in the table created by the component. Whilst there is no strict rule for naming the field, it is common to extend the above pattern using the params name. However, com_content stores article parameters in a field called attribs.

Accessing the parameters - backend[edit]

Joomla! includes some shortcuts to enable component creators to easily add parameter forms to the backend of their component. These forms allow a site administrator to enter a parameter value by, for example, entering some text in a box, choosing from a drop-down list, or selecting a radio button. To create these forms, each parameter needs to have an associated <param /> object in an XML file. The type of <param /> object determines the form elements that will be shown in the backend. A list of the available form elements is given here. More information is given below about the XML file names, structure and location required for each set of parameters.

1. Component-wide default parameters[edit]

These can be created at installation of the component, by including <param /> elements in the installation XML file (typically called COMPONENT_NAME.xml, and located in the root folder of the component package. As far as the parameters are concerned, this XML file has the following structure:

<install>
  ...
  <params>
    <param />
    
    <!-- Example: the following will create a line in the parameters field reading 'font_size=16\n':
    <param name="font_size" default="16" />
    -->
    ...
  </params>
  ...
</install>

Each <param /> element must be given the attributes name and default, which define the parameter name and value respectively. These values are then copied to the jos_components table in the database.

However, on its own this is not very useful, as it doesn't allow users to change the values of the parameters. To do this, parameter form elements need to be defined in a file named config.xml which is located in /PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/. The file should have the following structure:

<root>
  <params>
    <param />
    
    <!-- Example: the following will produce a text box 3 characters wide with the label 'Font size:'.  When the user hovers the mouse over the label, a tooltip will pop up which states 'Please enter the required font size'.  On saving, a value entered into the text box will be saved as the 'font_size' parameter value.
    <param type="text" name="font_size" size="3" label="Font size:" description="Please enter the required font size" />
    -->
    ...
  </params>
</root>

Note that the <root> element can have any name you wish, but there must be one element at the root of the document (this is a requirement for a well-formed XML document). You can have more than one <params> element, but only the first one will be considered. It is not currently possible to group certain parameter form elements together.

So that administrators to access these parameters, you will need to add a button to the toolbar in the backend of your component. This can be done using the code:

JToolBarHelper::preferences( 'com_COMPONENT_NAME' );
// e.g. for the core Content component, you would use JToolBarHelper::preferences( 'com_content' );

which is typically placed in the PHP file responsible for the backend output. (For example, using an MVC structure, you might place this code at the top of /PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/default.php.

This adds a Parameters button to the toolbar:

Toolbar newsfeed.png

Clicking on the toolbar button produces a pop-up window containing the form fields for each parameter defined in config.xml.

Note that there is no need to define default attributes for the <param /> elements in the config.xml file, since the default values will already have been saved in the installation of the component.

2. Menu item specific parameters[edit]

A menu item is associated with a particular view from your component's frontend - following the menu link in the frontend will display that view (usually for a particular component item). Joomla! 1.5 allows you to define parameters for a particular view, so that a copy of those parameters can be associated with each menu item using the view. You can also choose to override the component-wide default parameters as mentioned above, so that the override values will apply to the particular menu item.

The view parameters are defined in an XML file stored in the frontend template directory and with the same name as the template, i.e. /PATH_TO_JOOMLA/components/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/TEMPLATE_NAME.xml. The structure of the file is:

<metadata>
  ...
  <state>
    ...
    <params>
      <param />
      ...
    </params>
    <advanced>
      <param />
      ...
    </advanced>
  </state>
</metadata>

Displaying the forms for these parameters is handled automatically by the core com_menus component, which creates a number of slider panes on the right hand side (in the default Kepri backend template) of the menu item edit screen. Any parameters defined within the <params> tag will appear in the Parameters - Basic slider pane. Any parameters defined within the <advanced> tag will appear in the Parameters - Advanced slider pane.

For the override of the component-wide default parameters, com_menus uses the same config.xml file as above, so there is no need to define these parameters again. The override forms are automatically added to a slider pane named Parameters - Component. However, there are a number of changes from the forms used in the Preferences pop-up window (despite being based on the same config.xml file):

  • Any radio button form fields are replaced by drop-down lists.
  • All drop-down lists (both those defined as such in config.xml, and those defined as radio buttons) are given an extra option. This extra option displays the text "Use Global" but has an associated value of an empty string. The "Use Global" option is the default option for the list.
  • Any text box fields defined with a default value in config.xml retain that default value (unlike the drop-down lists).

3. 'Article' specific parameters[edit]

These are defined in an XML file which can be stored anywhere you like, but is typically placed within the models folder of the component backend (if you are using an MVC structure) and named according to the model to which they apply, i.e. PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/models/MODEL_NAME.xml. The XML file has the following structure:

<root>
  <params>
    <param />
    ...
  </params>
  <params group="GROUP_NAME">
    <param />
    ...
  </params>
  ...
</root>

As above, you can give the <root> element whatever name you wish. You can also have as many <params> groups as you like, though each group must have a group attribute with a different GROUP_NAME. If there are two or more <params> groups with the same GROUP_NAME, only the last one in the list will be considered.

To display the forms for these parameters in the backend of your component, you will need to do a little work yourself. Firstly, you need to access the saved parameter data from the database. For this, you will need to identify both the location of the stored parameters data in the database, and the XML file that you are using to define the parameter form fields. Assuming that you have already created a $row variable to hold your component item (i.e. 'article') data, that the parameters data is stored in the params field of the $row, and that the XML file is located in the models folder as specified above, you could use the following code:

$paramsdata = $row->params;
$paramsdefs = 'PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/models/MODEL_NAME.xml';
$params = new JParameter( $paramsdata, $paramsdefs );

This code is typically placed in the View file (e.g. view.html.php) within the corresponding folder.

To display the parameters forms in slider panes (in the same way that they are displayed in the menu item backend), you should use the following code in the template file (e.g. PATH_TO_JOOMLA/administrator/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/default.php):

$pane =& JPane::getInstance( 'sliders' );

echo $pane->startPane( 'content-pane' );

// First slider panel
// Create a slider panel with a title of SLIDER_PANEL_1_TITLE and a title id attribute of SLIDER_PANEL_1_NAME
echo $pane->startPanel( JText::_( 'SLIDER_PANEL_1_TITLE' ), 'SLIDER_PANEL_1_NAME' );
// Display the parameters defined in the <params> group with no 'group' attribute
echo $this->params->render( 'params' );
echo $pane->endPanel();

//Second slider panel
// Create a slider panel with a title of SLIDER_PANEL_2_TITLE and a title id attribute of SLIDER_PANEL_2_NAME
echo $pane->startPanel( JText::_( 'SLIDER_PANEL_2_TITLE' ), 'SLIDER_PANEL_2_NAME' );
// Display the parameters defined in the <params> group with the 'group' attribute of 'GROUP_NAME'
echo $this->params->render( 'params', 'GROUP_NAME' );
echo $pane->endPanel();

// Repeat for each additional slider panel required

echo $pane->endPane();

Accessing the parameters - frontend[edit]