User

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

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
 
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

Adding options to menu items[edit]

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[edit]

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

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modelitem');

class HelloWorldModelHelloWorld extends JModelItem
{
        protected $item;

        /**
         * Get the message
         * @return string The message to be displayed to the user
         */
        public function getItem() 
        {
                if (!isset($this->item)) {
                        $id = JRequest::getInt('id');
                        switch ($id) 
                        {
                        case 2:
                                $this->item = 'Good bye World!';
                        break;
                        default:
                        case 1:
                                $this->item = 'Hello World!';
                        break;
                        }
                }
                return $this->item;
        }
}

Installation manifest[edit]

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

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

        <name>Hello World!</name>
        <!-- The following elements are optional and free of formatting constraints -->
        <creationDate>June 2011</creationDate>
        <author>John Doe</author>
        <authorEmail>john.doe@example.org</authorEmail>
        <authorUrl>http://www.example.org</authorUrl>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <!--  The version string is stored in the components table -->
        <version>0.0.5</version>
        <!-- The description is optional and defaults to the name -->
        <description>Description of the Hello World component ...</description>

        <!-- Note the folder attribute: This attribute describes the folder
                to copy FROM in the package to install therefore files copied
                in this section are copied from "site/" in the package -->
        <files folder="site">
                <filename>index.html</filename>
                <filename>helloworld.php</filename>
                <filename>controller.php</filename>
                <folder>views</folder>
                <folder>models</folder>
        </files>

        <administration>
                <menu>Hello World!</menu>
                <!-- Note the folder attribute: This attribute describes the folder
                        to copy FROM in the package to install therefore files copied
                        in this section are copied from "admin/" in the package -->
                <files folder="admin">
                        <filename>index.html</filename>
                        <filename>helloworld.php</filename>
                </files>
        </administration>

</extension>

Testing your component[edit]

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[edit]

Download this part[edit]

Download example package

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5