J3.x

Difference between revisions of "Developing an MVC Component/Adding a menu type to the site part"

From Joomla! Documentation

< J3.x:Developing an MVC Component
m (tml needs to be tmpl)
(34 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{:J3.1:Developing a MVC Component}}
+
<noinclude><languages /></noinclude>
 +
{{:J3.1:Developing an MVC Component/<translate><!--T:1-->
 +
en</translate>}}
  
== Introduction ==
+
<translate>==Note== <!--T:19--></translate>
This tutorial is part of the [[J3.2:Developing a MVC Component|Developing a MVC Component for Joomla!3.2]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
+
<translate><!--T:20-->
 +
* This tutorial is part of the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Introduction|Developing a MVC Component for Joomla! 3.x]] tutorial. You are encouraged to read the previous parts of the tutorial before reading this.</translate>
  
== Abstract ==
+
<translate><!--T:21-->
 +
* You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the [https://github.com/scionescire/Joomla-3.2-Hello-World-Component/archive/step-3-adding-a-site-menu.zip archive]</translate>
  
Generally speaking, this article describes how to get a link on your joomla page to open a specific page of your component. This gets simply done by adding an xml file to your specific page into your view folder.  
+
<translate>
 +
== Adding a Menu Item to Hello World == <!--T:22-->
 +
</translate>
 +
<translate><!--T:23-->
 +
In this article we will cover how to add a menu item to a basic Joomla! component. For this example we will be continuing our work on the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component| Hello World!]] component.</translate>
  
E.g.
+
<translate><!--T:24-->
 +
There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.</translate>
 +
{| border=1
 +
| 1
 +
| <translate><!--T:25-->
 +
Manually add the files into</translate> <tt><path_to_joomla>/</tt>
 +
|-
 +
| 2
 +
| <translate><!--T:26-->
 +
Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install</translate>
 +
|-
 +
|3
 +
| <translate><!--T:27-->
 +
Update using the Joomla! Extension Manager and an [[S:MyLanguage/Deploying_an_Update_Server| Update Server]]</translate>
 +
|}
  
''site/views/helloworld/tmpl/''
+
<translate><!--T:28-->
 +
To add a menu item you will need to navigate to <tt>com_helloworld</tt>, which is the original directory we made for our component. You must use the updated directory structure from the last [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part | tutorial]]. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in [[#File Details|File Details]].</translate>
  
Contains your view page ''default.php'' that we want to open.
+
{| border=1
 +
| 1
 +
| <translate><!--T:29-->
 +
Create:</translate> [[#site/views/helloworld/tmpl/default.xml| default.xml]]
 +
| <tt><path_to_com_helloworld/site/views/helloworld/tmpl/default.xml<tt>
 +
|-
 +
| 2
 +
| <translate><!--T:30-->
 +
Update:</translate> [[#helloworld.xml| helloworld.xml]]
 +
| <tt><path_to_com_helloworld/helloworld.xml</tt>
 +
|}
  
A file ''default.xml'' is placed next to this file with some xml. This makes joomla able to recognize the view file ''default.php'' as a menu item.
+
<translate>
 +
=== Updating the Hello World! Component === <!--T:31-->
 +
</translate>
 +
<translate><!--T:32-->
 +
To update the Hello World! Component in the Joomla! website, please follow the same steps for the [[S:MyLanguage/J3.x:Developing_an_MVC_Component/Developing_a_Basic_Component#Installing the Hello World! Component |original installation]].</translate>
  
 +
<translate><!--T:33-->
 +
With the component successfully updated, we need to add the Menu Item. This will be done by using the Menu Manager of Joomla!.</translate>
  
== Adding a menu item type ==
+
<translate><!--T:34-->
In the Joomla framework, components are executed using menu items. If you go in the menu manager of your Joomla installation a ''HelloWorld'' menu item type does not yet exist. Adding this functionality is easy in Joomla. Simply put a ''site/views/helloworld/tmpl/default.xml'' file containing:
+
*Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <tt><yoursite>/joomla/administrator/index.php</tt>. For this example we will navigate to <tt>localhost/joomla/administrator/index.php</tt>.</translate>
 +
<translate><!--T:35-->
 +
*Click Menu {{rarr}} Manage {{rarr}} Menu Items {{rarr}} New {{rarr}} Select</translate>
 +
<translate><!--T:36-->
 +
*Navigate and Choose <tt>Hello World!</tt></translate>
 +
<translate><!--T:37-->
 +
*Navigate and Choose</translate> <tt>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLLE</tt>
 +
<translate><!--T:38-->
 +
*Click Menu Title and enter <tt>Hello World!</tt></translate>
 +
<translate><!--T:39-->
 +
*Click Save & Close</translate>
  
<span id="site/views/helloworld/tmpl/default.xml">
+
<translate>
''site/views/helloworld/tmpl/default.xml''
+
== File Details == <!--T:40-->
<source lang="xml">
+
</translate>
 +
{{vanchor|site/views/helloworld/tmpl/default.xml}}
 +
<source lang="xml>
 
<?xml version="1.0" encoding="utf-8"?>
 
<?xml version="1.0" encoding="utf-8"?>
 
<metadata>
 
<metadata>
 
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
 
<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
<message>
+
<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
<![CDATA[COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC]]>
 
</message>
 
 
</layout>
 
</layout>
 
</metadata>
 
</metadata>
 
</source>
 
</source>
</span>
 
For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.
 
  
Also modify your ''helloworld.xml'' file to indicate a new version:
+
{{vanchor|helloworld.xml}}
 +
<source lang="xml" highlight="13">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<extension type="component" version="3.2.0" method="upgrade">
  
<span id="helloworld.xml">
 
''helloworld.xml''
 
<source lang="xml">
 
<?xml version="1.0" encoding="utf-8"?>
 
<extension type="component" version="3.2" method="upgrade">
 
 
 
<name>Hello World!</name>
 
<name>Hello World!</name>
 
<!-- The following elements are optional and free of formatting constraints -->
 
<!-- The following elements are optional and free of formatting constraints -->
Line 55: Line 99:
 
<!-- The description is optional and defaults to the name -->
 
<!-- The description is optional and defaults to the name -->
 
<description>Description of the Hello World component ...</description>
 
<description>Description of the Hello World component ...</description>
+
 
 
<update> <!-- Runs on update; New since J2.5 -->
 
<update> <!-- Runs on update; New since J2.5 -->
 
<schemas>
 
<schemas>
<schemapath type="mysql">sql/updates/mysql/</schemapath>
+
<schemapath type="mysql">sql/updates/mysql</schemapath>
 
</schemas>
 
</schemas>
 
</update>
 
</update>
+
 
 
<!-- Site Main File Copy Section -->
 
<!-- Site Main File Copy Section -->
 
<!-- Note the folder attribute: This attribute describes the folder
 
<!-- Note the folder attribute: This attribute describes the folder
Line 72: Line 116:
 
<folder>views</folder>
 
<folder>views</folder>
 
</files>
 
</files>
+
 
 
<administration>
 
<administration>
 
<!-- Administration Menu Section -->
 
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
+
<menu link='index.php?option=com_helloworld'>Hello World!</menu>
 
<!-- Administration Main File Copy Section -->
 
<!-- Administration Main File Copy Section -->
 
<!-- Note the folder attribute: This attribute describes the folder
 
<!-- Note the folder attribute: This attribute describes the folder
Line 88: Line 132:
 
</files>
 
</files>
 
</administration>
 
</administration>
 
 
</extension>
 
</extension>
 
</source>
 
</source>
</span>
 
  
== Packaging the component ==
+
<translate>
 +
== Code Explanation == <!--T:41-->
 +
</translate>
 +
<translate><!--T:42-->
 +
In case you were curious as to why this works the way it does.</translate>
  
Content of your code directory
+
=== default.xml ===
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[#site/helloworld.php|site/helloworld.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
 
* ''[[#site/controller.php|site/controller.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 
* ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 
* ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 
* ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 
* ''[[J3.2:Developing_a_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 
[[File:Com_helloworld_v3-0.0.3.png|thumb|right|400px|Selecting the Menu Item Type]] Create a compressed file of this directory or directly download the [https://github.com/joomla/Joomla-3.2-Hello-World-Component/archive/step-3-adding-a-site-menu.zip archive] and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend. To do so select "Add New Menu Item" from the one of the menus in the "Menus" menu; then you can select COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE for Menu Item Type. Once selected you can see the Link information is populated with the URL for the view.
 
<br style="clear: both" />
 
  
{{notice|Please create a pull request or issue at https://github.com/joomla/Joomla-3.2-Hello-World-Component for any code descprepancies or if editing any of the source code on this page.}}
+
<translate><!--T:43-->
 +
Note - For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.</translate>
 +
 
 +
=== helloworld.xml===
 +
 
 +
<source lang="xml">
 +
<version>0.0.3</version>
 +
</source>
  
{{:J3.2:Developing a MVC Component/Navigate
+
<translate><!--T:44-->
|prev=Adding a view to the site part <!-- previous article subpage name -->
+
Updates the version number.</translate>
|next=Adding a model to the site part <!-- next article subpage name -->}}
 
  
== Contributors ==
+
<translate>
*[[User:cdemko|Christophe Demko]]
+
== Component Contents == <!--T:45-->
*[[User:oaksu|Ozgur Aksu]]
+
</translate>
*[[User:Jossnaz|Lukas Meier]]
+
<translate><!--T:46-->
*[[User:betweenbrain|Matt Thomas]]
+
At this point in the tutorial, your component should contain the following files:</translate>
 +
{| border=1
 +
| 1
 +
| ''[[#helloworld.xml|helloworld.xml]]''
 +
| <translate><!--T:47-->
 +
this is an XML (manifest) file that tells Joomla! how to install our component.</translate>
 +
|-
 +
| 2
 +
| ''[[#site/helloworld.php|site/helloworld.php]]''
 +
| <translate><!--T:48-->
 +
this is the site entry point to the Hello World! component</translate>
 +
|-
 +
| 3
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/index.html]]''
 +
| <translate><!--T:49-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 4
 +
| ''[[#site/controller.php|site/controller.php]]''
 +
| <translate><!--T:50-->
 +
file representing the controller</translate>
 +
|-
 +
| 5
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/index.html]]''
 +
| <translate><!--T:51-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 6
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/index.html]]''
 +
| <translate><!--T:52-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 7
 +
| ''[[#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 +
| <translate><!--T:53-->
 +
file representing the view</translate>
 +
|-
 +
| 8
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|site/views/helloworld/tmpl/index.html]]''
 +
| <translate><!--T:54-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 9
 +
| ''[[#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 +
| <translate><!--T:55-->
 +
the default view</translate>
 +
|-
 +
| 10
 +
| ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 +
| <translate><!--T:56-->
 +
file that adds menu item</translate>
 +
|-
 +
| 11
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/index.html]]''
 +
| <translate><!--T:57-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 12
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/helloworld.php|admin/helloworld.php]]''
 +
| <translate><!--T:58-->
 +
this is the administrator entry point to the Hello World! component</translate>
 +
|-
 +
| 13
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/index.html]]''
 +
| <translate><!--T:59-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 14
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/index.html]]''
 +
| <translate><!--T:60-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 15
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#index.html|admin/sql/updates/mysql/index.html]]''
 +
| <translate><!--T:61-->
 +
prevents web server from listing directory content</translate>
 +
|-
 +
| 16
 +
| ''[[S:MyLanguage/J3.2:Developing_an_MVC_Component/Developing_a_Basic_Component#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 +
| <translate><!--T:62-->
 +
file allowing to initialise schema version of the com_helloworld component.</translate>
 +
|}
  
 +
<div class="row">
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:3-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_view_to_the_site_part|Prev: Adding a view to the site part</translate>|class=expand success}}</div>
 +
<div class="large-6 columns">{{Basic button|<translate><!--T:4-->
 +
S:MyLanguage/J3.x:Developing_an_MVC_Component/Adding_a_model_to_the_site_part|Next: Adding a model to the site part</translate>|class=expand}}</div>
 +
</div>
 +
__NOTOC__
 +
<noinclude>
 +
<translate>
 +
<!--T:63-->
 +
[[Category:Joomla! 3.x]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.0]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.1]]
 
[[Category:Joomla! 3.2]]
 
[[Category:Joomla! 3.2]]
 +
[[Category:Joomla! 3.3]]
 +
[[Category:Joomla! 3.4]]
 
[[Category:Beginner Development]]
 
[[Category:Beginner Development]]
 
[[Category:Component Development]]
 
[[Category:Component Development]]
 +
[[Category:Tutorials]]
 +
[[Category:Tutorials in a Series]]
 +
</translate>
 +
</noinclude>

Revision as of 20:16, 23 January 2016

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
Tutorial
Developing an MVC Component



This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).



Note[edit]

  • You can follow the steps below to add a menu item to the Hello World! component, or you can directly download the archive

Adding a Menu Item to Hello World[edit]

In this article we will cover how to add a menu item to a basic Joomla! component. For this example we will be continuing our work on the Hello World! component.

There are several ways to update a Joomla! component. As with the last tutorial, we will focus option 2.

1 Manually add the files into <path_to_joomla>/
2 Update using the Joomla! Extension Manager and the original directory, non-compressed, used for the component install
3 Update using the Joomla! Extension Manager and an Update Server

To add a menu item you will need to navigate to com_helloworld, which is the original directory we made for our component. You must use the updated directory structure from the last tutorial. Using your preferred file manager, create or update the following files; as you create or update the files, add the source code for each file which is found in File Details.

1 Create: default.xml <path_to_com_helloworld/site/views/helloworld/tmpl/default.xml
2 Update: helloworld.xml <path_to_com_helloworld/helloworld.xml

Updating the Hello World! Component[edit]

To update the Hello World! Component in the Joomla! website, please follow the same steps for the original installation.

With the component successfully updated, we need to add the Menu Item. This will be done by using the Menu Manager of Joomla!.

  • Using your preferred web browser, navigate to the Administrator panel of your Joomla! site. The address would be <yoursite>/joomla/administrator/index.php. For this example we will navigate to localhost/joomla/administrator/index.php.
  • Click Menu    Manage    Menu Items    New    Select
  • Navigate and Choose Hello World!
  • Navigate and Choose COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLLE
  • Click Menu Title and enter Hello World!
  • Click Save & Close

File Details[edit]

site/views/helloworld/tmpl/default.xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
	<layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
		<message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
	</layout>
</metadata>

helloworld.xml

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

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2014</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 recorded in the components table -->
	<version>0.0.3</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>

	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- 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>
	</files>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld'>Hello World!</menu>
		<!-- Administration Main File Copy Section -->
		<!-- 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">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
		</files>
	</administration>
</extension>

Code Explanation[edit]

In case you were curious as to why this works the way it does.

default.xml[edit]

Note - For the moment the strings won't be translated in the administrator interface. We will see in a later article how translation is performed.

helloworld.xml[edit]

<version>0.0.3</version>

Updates the version number.

Component Contents[edit]

At this point in the tutorial, your component should contain the following files:

1 helloworld.xml this is an XML (manifest) file that tells Joomla! how to install our component.
2 site/helloworld.php this is the site entry point to the Hello World! component
3 site/index.html prevents web server from listing directory content
4 site/controller.php file representing the controller
5 site/views/index.html prevents web server from listing directory content
6 site/views/helloworld/index.html prevents web server from listing directory content
7 site/views/helloworld/view.html.php file representing the view
8 site/views/helloworld/tmpl/index.html prevents web server from listing directory content
9 site/views/helloworld/tmpl/default.php the default view
10 site/views/helloworld/tmpl/default.xml file that adds menu item
11 admin/index.html prevents web server from listing directory content
12 admin/helloworld.php this is the administrator entry point to the Hello World! component
13 admin/sql/index.html prevents web server from listing directory content
14 admin/sql/updates/index.html prevents web server from listing directory content
15 admin/sql/updates/mysql/index.html prevents web server from listing directory content
16 admin/sql/updates/mysql/0.0.1.sql file allowing to initialise schema version of the com_helloworld component.