Actions

User

Difference between revisions of "Rvsjoen/tutorial/Developing an MVC Component/Part 05"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
(Download this part)
m (Installation manifest)
 
Line 83: Line 83:
 
<source lang="xml" line highlight="13">
 
<source lang="xml" line highlight="13">
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="1.6.0" method="upgrade">
+
<extension type="component" version="2.5.0" method="upgrade">
  
 
         <name>Hello World!</name>
 
         <name>Hello World!</name>

Latest revision as of 09:27, 26 January 2012

Contents

Adding options to menu items

Alright, so we want to be able to set some options on our menu items, there are two steps involved in this process. First we need to actually write some options into the default.xml file, and afterwards we have to make the model respect this option and return the proper message.

With your favorite editor, modify the following file to look like this

site/views/helloworld/tmpl/default.xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
        <layout title="Hello World">
                <message>Hello World menu item description</message>
        </layout>
        <fields name="request">
                <fieldset name="request">
                        <field
                                name="id"
                                type="list"
                                label="Greeting"
                                description="Select a greeting"
                                default="1"
                        >
                                <option value="1">Hello World!</option>
                                <option value="2">Good bye World!</option>
                        </field>
                </fieldset>
        </fields>
</metadata>

Updating the model

We need to make our model respect the option set in the menu item, and return the proper message. We do this by simply implementing a conditional switch statement, deciding which message to return based on the $id requested.

With your favorite editor, modify the following file to look like this

site/models/helloworld.php

  1. <?php
  2. // No direct access to this file
  3. defined('_JEXEC') or die('Restricted access');
  4.  
  5. jimport('joomla.application.component.modelitem');
  6.  
  7. class HelloWorldModelHelloWorld extends JModelItem
  8. {
  9.         protected $item;
  10.  
  11.         /**
  12.          * Get the message
  13.          * @return string The message to be displayed to the user
  14.          */
  15.         public function getItem() 
  16.         {
  17.                 if (!isset($this->item)) {
  18.                         $id = JRequest::getInt('id');
  19.                         switch ($id) 
  20.                         {
  21.                         case 2:
  22.                                 $this->item = 'Good bye World!';
  23.                         break;
  24.                         default:
  25.                         case 1:
  26.                                 $this->item = 'Hello World!';
  27.                         break;
  28.                         }
  29.                 }
  30.                 return $this->item;
  31.         }
  32. }

Installation manifest

No changes have been made to the installation manifest apart from updating the version number.

helloworld.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <extension type="component" version="2.5.0" method="upgrade">
  3.  
  4.         <name>Hello World!</name>
  5.         <!-- The following elements are optional and free of formatting constraints -->
  6.         <creationDate>June 2011</creationDate>
  7.         <author>John Doe</author>
  8.         <authorEmail>john.doe@example.org</authorEmail>
  9.         <authorUrl>http://www.example.org</authorUrl>
  10.         <copyright>Copyright Info</copyright>
  11.         <license>License Info</license>
  12.         <!--  The version string is stored in the components table -->
  13.         <version>0.0.5</version>
  14.         <!-- The description is optional and defaults to the name -->
  15.         <description>Description of the Hello World component ...</description>
  16.  
  17.         <!-- Note the folder attribute: This attribute describes the folder
  18.                 to copy FROM in the package to install therefore files copied
  19.                 in this section are copied from "site/" in the package -->
  20.         <files folder="site">
  21.                 <filename>index.html</filename>
  22.                 <filename>helloworld.php</filename>
  23.                 <filename>controller.php</filename>
  24.                 <folder>views</folder>
  25.                 <folder>models</folder>
  26.         </files>
  27.  
  28.         <administration>
  29.                 <menu>Hello World!</menu>
  30.                 <!-- Note the folder attribute: This attribute describes the folder
  31.                         to copy FROM in the package to install therefore files copied
  32.                         in this section are copied from "admin/" in the package -->
  33.                 <files folder="admin">
  34.                         <filename>index.html</filename>
  35.                         <filename>helloworld.php</filename>
  36.                 </files>
  37.         </administration>
  38.  
  39. </extension>

Testing your component

For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.

In order to test this component, go to the administrator interface and create a new menu item. In the menu item type selection interface, pick Hello World as the menu item type. In the options of the menu item you will be able to choose which hello message to show, the message you choose should be displayed in the frontend as in the previous part.

File listing

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5