User

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

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component
 
(11 intermediate revisions by the same user not shown)
Line 42: Line 42:
 
                 JRequest::setVar('hidemainmenu', true);
 
                 JRequest::setVar('hidemainmenu', true);
 
                 $isNew = ($this->item->id == 0);
 
                 $isNew = ($this->item->id == 0);
                 JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld');
+
                 JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW')  
 +
                        : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld');
 
                 JToolBarHelper::save('helloworld.save');
 
                 JToolBarHelper::save('helloworld.save');
 
                 JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
 
                 JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
Line 111: Line 112:
 
== Updating the entry point ==
 
== Updating the entry point ==
  
The first thing we need to do in order to add a pretty image to the views in our component (in the backend) is to add a style declaration for this image.
+
The next thing we need to do in order to add the pretty little image to the views in our component (in the backend) is to add a style declaration for this image. We already decided what to call the icon, namely '''icon-48-helloworld''' so in our entry point we add this style declaration to the document.
  
 
With your favorite editor, modify the following file to look like this
 
With your favorite editor, modify the following file to look like this
Line 140: Line 141:
 
</span>
 
</span>
  
In order to add the class, we simply get the document object and add a style declaration to it. The class name is somewhat predetermined as the '''icon-48''' part is automatically added and the '''helloworld''' part is customizable using the second argument to <code>JToolBarHelper::title()</code>.
+
== Updating language files ==
 +
 
 +
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
 +
 
 +
<span id="admin/language/en-GB/en-GB.com_helloworld.ini">
 +
'''<tt>admin/language/en-GB/en-GB.com_helloworld.ini</tt>'''
 +
<source lang="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"
 +
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
 +
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
 +
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"
 +
</source>
 +
</span>
  
 
== Installation manifest ==
 
== Installation manifest ==
Line 150: Line 174:
 
<source lang="xml" highlight="46">
 
<source lang="xml" highlight="46">
 
<?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>COM_HELLOWORLD</name>
 
         <name>COM_HELLOWORLD</name>
Line 219: Line 243:
 
For details on how to install the component into your Joomla! site, refer to the information provided in
 
For details on how to install the component into your Joomla! site, refer to the information provided in
 
[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#Testing_your_component|Part 01]].
 
[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#Testing_your_component|Part 01]].
 +
 +
Check the Components menu and make sure that the HelloWorld menu item now has an icon, and the same goes for the view itself which should display a bigger icon in its title.
  
 
== File listing ==  
 
== File listing ==  
Line 238: Line 264:
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#site/language/en-GB/en-GB.com_helloworld.ini|site/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/index.html]]</tt>
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/helloworld.php|admin/helloworld.php]]</tt>
+
* <tt>[[#admin/helloworld.php|admin/helloworld.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/controller.php|admin/controller.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/controller.php|admin/controller.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/controllers/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/controllers/index.html]]</tt>
* <tt>[[#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/controllers/helloworld.php|admin/controllers/helloworld.php]]</tt>
* <tt>[[#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/controllers/helloworlds.php|admin/controllers/helloworlds.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/index.html]]</tt>
* <tt>[[#admin/models/helloworld.php|admin/models/helloworld.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/models/helloworld.php|admin/models/helloworld.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/models/helloworlds.php|admin/models/helloworlds.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/models/helloworlds.php|admin/models/helloworlds.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/fields/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/fields/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_06#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_06#admin/models/fields/helloworld.php|admin/models/fields/helloworld.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/forms/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/models/forms/index.html]]</tt>
* <tt>[[#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/models/forms/helloworld.xml|admin/models/forms/helloworld.xml]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/models/helloworld.php|admin/models/helloworld.php]]
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworld/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworld/index.html]]</tt>
 
* <tt>[[#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]</tt>
 
* <tt>[[#admin/views/helloworld/view.html.php|admin/views/helloworld/view.html.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworld/tmpl/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworld/tmpl/index.html]]</tt>
* <tt>[[#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/views/helloworld/tmpl/edit.php|admin/views/helloworld/tmpl/edit.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworlds/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworlds/index.html]]</tt>
 
* <tt>[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]</tt>
 
* <tt>[[#admin/views/helloworlds/view.html.php|admin/views/helloworlds/view.html.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworlds/tmpl/index.html]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|admin/views/helloworlds/tmpl/index.html]]</tt>
* <tt>[[#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]</tt>
+
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_09#admin/views/helloworlds/tmpl/default.php|admin/views/helloworlds/tmpl/default.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/views/helloworlds/tmpl/default_head.php|admin/views/helloworlds/tmpl/default_head.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/views/helloworlds/tmpl/default_head.php|admin/views/helloworlds/tmpl/default_head.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/views/helloworlds/tmpl/default_body.php|admin/views/helloworlds/tmpl/default_body.php]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_07#admin/views/helloworlds/tmpl/default_body.php|admin/views/helloworlds/tmpl/default_body.php]]</tt>
Line 271: Line 298:
 
* <tt>[[#admin/language/en-GB/en-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[#admin/language/en-GB/en-GB.com_helloworld.ini|admin/language/en-GB/en-GB.com_helloworld.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#admin/language/en-GB/en-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]</tt>
 
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_08#admin/language/en-GB/en-GB.com_helloworld.sys.ini|admin/language/en-GB/en-GB.com_helloworld.sys.ini]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|media/index.html]]</tt>
 +
* <tt>[[User:Rvsjoen/tutorial/Developing_an_MVC_Component/Part_01#index.html|media/images/index.html]]</tt>
 +
* <tt>[[#media/images/tux-16x16.png|media/images/tux-16x16.png]]</tt>
 +
* <tt>[[#media/images/tux-16x16.png|media/images/tux-48x48.png]]</tt>
  
 
== Download this part ==
 
== Download this part ==
 +
 +
[https://github.com/downloads/rvsjoen/joomla-tutorials/com_helloworld-part10.zip Download example package]
  
 
== Articles in this series ==
 
== Articles in this series ==

Latest revision as of 09:34, 26 January 2012

Adding decorations to the backend[edit]

We like pretty things, and decorations tend to make things pretty, so let us add some decorations to the backend views.

Updating the toolbars[edit]

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

admin/views/helloworld/view.html.php

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

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

class HelloWorldViewHelloWorld extends JView
{
        public function display($tpl = null) 
        {
                // get the Data
                $form = $this->get('Form');
                $item = $this->get('Item');

                // Assign the Data
                $this->form = $form;
                $this->item = $item;

                // Set the toolbar
                $this->addToolBar();

                // Display the template
                parent::display($tpl);

                // Set the document
                $this->setDocument();
        }

        protected function addToolBar() 
        {
                JRequest::setVar('hidemainmenu', true);
                $isNew = ($this->item->id == 0);
                JToolBarHelper::title($isNew ? JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_NEW') 
                         : JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLD_EDIT'), 'helloworld');
                JToolBarHelper::save('helloworld.save');
                JToolBarHelper::cancel('helloworld.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
        }

        protected function setDocument() 
        {
                $isNew = ($this->item->id < 1);
                $document = JFactory::getDocument();
                $document->setTitle($isNew ? JText::_('COM_HELLOWORLD_HELLOWORLD_CREATING') : JText::_('COM_HELLOWORLD_HELLOWORLD_EDITING'));
        }
}

We added the second parameter to JToolBarHelper::title() to make the image class, and we also added the setDocument() function that changes the title of the view, depending on whether we are editing an existing item, or creating a new one. It really is the little things that matter. Now let's do the same for the helloworlds view.

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

admin/views/helloworlds/view.html.php

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

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

class HelloWorldViewHelloWorlds extends JView
{
        function display($tpl = null) 
        {
                // Get data from the model
                $items = $this->get('Items');
                $pagination = $this->get('Pagination');

                // Assign data to the view
                $this->items = $items;
                $this->pagination = $pagination;

                // Set the toolbar
                $this->addToolBar();

                // Display the template
                parent::display($tpl);

                // Set the document
                $this->setDocument();
        }

        protected function addToolBar() 
        {
                JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'), 'helloworld');
                JToolBarHelper::deleteListX('', 'helloworlds.delete');
                JToolBarHelper::editListX('helloworld.edit');
                JToolBarHelper::addNewX('helloworld.add');
        }

        protected function setDocument() 
        {
                $document = JFactory::getDocument();
                $document->setTitle(JText::_('COM_HELLOWORLD_ADMINISTRATION'));
        }
}

Updating the entry point[edit]

The next thing we need to do in order to add the pretty little image to the views in our component (in the backend) is to add a style declaration for this image. We already decided what to call the icon, namely icon-48-helloworld so in our entry point we add this style declaration to the document.

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

admin/helloworld.php

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

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

// Set some global property
$document = JFactory::getDocument();
$document->addStyleDeclaration('.icon-48-helloworld {background-image: url(../media/com_helloworld/images/tux-48x48.png);}');

// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');

// Perform the Request task
$controller->execute(JRequest::getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

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"
COM_HELLOWORLD_ADMINISTRATION="HelloWorld - Administration"
COM_HELLOWORLD_HELLOWORLD_CREATING="HelloWorld - Creating"
COM_HELLOWORLD_HELLOWORLD_EDITING="HelloWorld - Editing"

Installation manifest[edit]

The changes to the installation manifest are the version number and addition of the menu image in the administrator menu section. In addition we added the media folder to be copied.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.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.10</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>

        <media destination="com_helloworld" folder="media">
                <filename>index.html</filename>
                <folder>images</folder>
        </media>

        <administration>
                <menu img="../media/com_helloworld/images/tux-16x16.png">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.

Check the Components menu and make sure that the HelloWorld menu item now has an icon, and the same goes for the view itself which should display a bigger icon in its title.

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