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
(Marked this version for translation)
 
(54 intermediate revisions by 17 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>}}
  
{{review}}
+
<translate>==Note== <!--T:19--></translate>
 +
<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>
  
This is a multiple-article series of tutorials on how to develop a Model-View-Contoller [[Component]] for Joomla! Version {{JVer|{{CurrentSTSVer|minor}}}}.
+
<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>
  
== Introduction ==
+
<translate><!--T:64--> * You can watch a video associated with this step in the tutorial at [https://www.youtube.com/watch?v=f51yt689pWk Step 3, Adding a Menu Part].</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.
 
  
== Abstract ==
+
{{#widget:YouTube|id=f51yt689pWk}}
  
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-->
 +
After the component has been updated, we will be able to add a Menu Item to it. This will allow us to access our component a menu rather than having to remember what to type into the address bar. We'll do this 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-->
 +
*Add a new menu item by clicking on Menu {{rarr}} <existing menu> {{rarr}} Add New Menu Item. A new screen will appear.</translate>
 +
<translate><!--T:36-->
 +
*In the "Menu Title" field, enter "Hello World!"</translate>
 +
<translate><!--T:37-->
 +
*In the "Menu Type" field, click on the "Select" button and choose Hello World! {{rarr}} Hello World from the selection screen.</translate>
 +
<translate><!--T:38-->
 +
*Click Save & Close</translate>
 +
<translate>
 +
<!--T:65-->
 +
You should now be able to access the component through the menu that you just created.</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">
<span id="helloworld.xml">
+
<?xml version="1.0" encoding="utf-8"?>
''helloworld.xml''
+
<extension type="component" version="3.0" method="upgrade">
<source lang="xml">
 
<extension type="component" version="2.5.0" 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 -->
<creationDate>November 2009</creationDate>
+
<creationDate>January 2018</creationDate>
 
<author>John Doe</author>
 
<author>John Doe</author>
 
<authorEmail>john.doe@example.org</authorEmail>
 
<authorEmail>john.doe@example.org</authorEmail>
Line 59: Line 105:
 
<description>Description of the Hello World component ...</description>
 
<description>Description of the Hello World component ...</description>
  
<update> <!-- Runs on update; New in 2.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>
Line 78: Line 124:
 
<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 91: Line 137:
 
</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 ===
Content of your code directory
 
* ''[[#helloworld.xml|helloworld.xml]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/helloworld.php|site/helloworld.php]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/controller.php|site/controller.php]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/helloworld/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/views/helloworld/view.html.php|site/views/helloworld/view.html.php]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|site/views/helloworld/tmpl/index.html]]''
 
* ''[[#site/views/helloworld/tmpl/default.xml|site/views/helloworld/tmpl/default.xml]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_02#site/views/helloworld/tmpl/default.php|site/views/helloworld/tmpl/default.php]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#admin/helloworld.php|admin/helloworld.php]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/updates/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#index.html|admin/sql/updates/mysql/index.html]]''
 
* ''[[J3.2:Developing a MVC Component_-_Part_01#admin/sql/updates/mysql/0.0.1.sql|admin/sql/updates/mysql/0.0.1.sql]]''
 
  
[[File:Com_helloworld-0.0.3.png|thumb|right|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-4-adding-a-site-model.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.
+
<translate><!--T:43-->
<br style="clear: both" />
+
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>
  
== Navigate ==
+
=== helloworld.xml===
  
[[Developing a Model-View-Controller (MVC) Component for Joomla!3.2 - Part 02|Prev: Adding a view to the site part]]
+
<source lang="xml">
 +
<version>0.0.3</version>
 +
</source>
  
[[Developing a Model-View-Controller (MVC) Component for Joomla!3.2 - Part 04|Next: Adding a model to the site part]]
+
<translate><!--T:44-->
 +
Updates the version number.</translate>
  
== 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>

Latest revision as of 00:30, 29 March 2018

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.

After the component has been updated, we will be able to add a Menu Item to it. This will allow us to access our component a menu rather than having to remember what to type into the address bar. We'll do this 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.
  • Add a new menu item by clicking on Menu    <existing menu>    Add New Menu Item. A new screen will appear.
  • In the "Menu Title" field, enter "Hello World!"
  • In the "Menu Type" field, click on the "Select" button and choose Hello World!    Hello World from the selection screen.
  • Click Save & Close

You should now be able to access the component through the menu that you just created.

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.0" method="upgrade">

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2018</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.