User

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

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
Line 74: Line 74:
 
                 // Get the form.
 
                 // Get the form.
 
                 $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', array('control' => 'jform', 'load_data' => $loadData));
 
                 $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', array('control' => 'jform', 'load_data' => $loadData));
                if(empty($form)){
 
                        return false;
 
                }
 
 
                 return $form;
 
                 return $form;
 
         }
 
         }

Revision as of 04:35, 12 July 2011

Adding actions to backend[edit]

Now, we don't want to only be able to see a table with all the greetings in the backend. We also want to be able to manage them by editing, adding new greetings, deleting greetings and so on. This means we are going to have to add some more logic to the backend. We will implement a controller to handle these task and an editing view to make them available for the user.

Creating the controllers[edit]

In fact, we are going to implement not only one, but two controllers. One controller will be responsible for handling the actions taken in the view we already have (the table listing the messages). And one controller will be responsible for handling the actions taken when we are in the editing view of a single message.

With your favorite editor, create the following file

admin/controllers/helloworlds.php

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

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

class HelloWorldControllerHelloWorlds extends JControllerAdmin
{
        public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel') 
        {
                $model = parent::getModel($name, $prefix, array('ignore_request' => true));
                return $model;
        }
}

The important thing to notice about this controller is that it extends from JControllerAdmin and not from JController, this means that it has a lot of builtin functionality for handling lists of items like you would normally see in the backend. We override the default getModel() function in order to use the HelloWorldModelHelloWorld instead of HelloWorldModelHellos for these actions so we don't have to duplicate functionality in two different controllers.

Now that we have the controller to handle the list view, we also need to create the controller to handle the single item editing view, this will be handled by a controller extending JControllerForm.

With your favorite editor, create the following file

admin/controllers/helloworld.php

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

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

class HelloWorldControllerHelloWorld extends JControllerForm
{

}

We do not have to add any functionality to this controller for our component since JControllerForm has everything that we need.

Creating the model[edit]

As we previously said, we also need a model for our components to work with to perform options on the individual messages. We already have a controller.

With your favorite editor, create the following file

admin/controllers/helloworld.php

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

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

class HelloWorldModelHelloWorld extends JModelAdmin
{
        public function getForm($data = array(), $loadData = true) 
        {
                // Get the form.
                $form = $this->loadForm('com_helloworld.helloworld', 'helloworld', array('control' => 'jform', 'load_data' => $loadData));
                return $form;
        }

        protected function loadFormData() 
        {
                // Check the session for previously entered form data.
                $data = JFactory::getApplication()->getUserState('com_helloworld.edit.helloworld.data', array());
                if(empty($data)){
                        $data = $this->getItem();
                }
                return $data;
        }
}


In the model there are two functions that we need to create. getForm() is declared abstract in JModelForm so we have no choice whether or not to implement this if we want to extend from JModelAdmin, and it is basically used to get the form from the XML form definition (which we will create in a minute) using the protected function loadForm().

Updating language files[edit]

Since we added a couple of new language strings, we need to translate these and put them in the proper language file

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

admin/language/en-GB/en-GB.com_helloworld.ini

COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="ID"
COM_HELLOWORLD_HELLOWORLD_DETAILS="Details"
COM_HELLOWORLD_MANAGER_HELLOWORLDS="HelloWorld manager"
COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW="HelloWorld manager: New Message"
COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT="HelloWorld manager: Edit Message"
COM_HELLOWORLD_N_ITEMS_DELETED_1="One message deleted"
COM_HELLOWORLD_N_ITEMS_DELETED_MORE="%d messages deleted"

Installation manifest[edit]

The changes to the installation manifest are the version number and the addition of the controller folder in the administrator file copy section.

helloworld.xml

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

	<name>COM_HELLOWORLD</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.9</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_HELLOWORLD_DESCRIPTION</description>

	<install>
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall>
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>

	<!-- 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>
		<folder>language</folder>
	</files>

	<administration>
		<menu>COM_HELLOWORLD_MENU</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>
			<filename>controller.php</filename>
			<folder>sql</folder>
			<folder>tables</folder>
			<folder>models</folder>
			<folder>views</folder>
			<folder>language</folder>
			<folder>controllers</folder>
		</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.

File listing[edit]

Download this part[edit]

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5