Actions

Difference between revisions of "JModuleHelper/getModule"

From Joomla! Documentation

m (Methods may be called statically.)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
Returns a stdClass object containing information about the module requested.  A given module will only be returned if it meets the following criteria:
 +
* it is enabled;
 +
* it is assigned to the current menu item or is assigned to all menu items;
 +
* the user meets the access level requirements.
 +
 
===Syntax===
 
===Syntax===
''object'' getModule( $name[, $title= null])
+
''object'' getModule( $name, $title )
  
 
where:
 
where:
Line 11: Line 16:
 
|$name
 
|$name
 
|string
 
|string
|The name of the module
+
|The name/type of the module.  For example, 'login' for the login form module.  If more than one module of this name/type exists and no title has been specified then the first module of the given name/type is returned.  If the module is not found and the 'mod_' prefix has been used then a dummy stdClass object will be returned.
 
|
 
|
 
|-
 
|-
 
|$title
 
|$title
 
|string
 
|string
|The title of the module, optional
+
|The title of the module.  If '''null''' the first module of the given name/type is returned.
 
|null
 
|null
 
|}
 
|}
===Example===
+
===Example 1===
To get an installed Module, you can use the following example. This way the method checks if the module has been published, if the module is supposed to be displayed in this page, and if the user meets the access level expectations:
+
Obtain the login form module.
 
<source lang="php">
 
<source lang="php">
jimport('joomla.application.module.helper');
+
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('login');
+
$module = JModuleHelper::getModule( 'login' );
 
echo '<pre>';
 
echo '<pre>';
print_r($module);
+
print_r( $module );
 
echo '</pre>';
 
echo '</pre>';
/* RESULT:
+
</source>
 +
which will output:
 +
<pre>
 
stdClass Object
 
stdClass Object
 
(
 
(
Line 37: Line 44:
 
     [showtitle] => 1
 
     [showtitle] => 1
 
     [control] =>  
 
     [control] =>  
     [params] => cache=0
+
     [params] => greeting=1
                moduleclass_sfx=_login
+
                 name=0
                pretext=
+
                posttext=
+
                login=
+
                logout=
+
                greeting=1
+
                 name=1
+
                usesecure=0
+
 
     [user] => 0
 
     [user] => 0
 
     [name] => login
 
     [name] => login
 
     [style] =>  
 
     [style] =>  
)
+
)</pre>
*/
+
===Example 2===
</source>
+
If more than one instance of a module exists then you can select the correct one by specifying its title. For example, there are usually many modules of type 'mod_mainmenu', so here we select the one with title "Resources":
You can also call for a custom module, that has not been installed via the Joomla Backend. $name has start with "mod_" in this case. The method then returns a standard object. It doesn't check if the module is published or not, it doesn't check if the user has the neccessary rights to see the module.
+
<source lang="php">
 +
jimport( 'joomla.application.module.helper' );
 +
$module = JModuleHelper::getModule( 'mainmenu', 'Resources' );
 +
echo '<pre>';
 +
print_r( $module );
 +
echo '</pre>';</source>
 +
which will output:
 +
<pre>stdClass Object
 +
(
 +
    [id] => 31
 +
    [title] => Resources
 +
    [module] => mod_mainmenu
 +
    [position] => left
 +
    [content] =>
 +
    [showtitle] => 0
 +
    [control] =>
 +
    [params] => menutype=othermenu
 +
menu_style=list
 +
startLevel=0
 +
endLevel=0
 +
showAllChildren=0
 +
window_open=
 +
show_whitespace=0
 +
cache=1
 +
tag_id=
 +
class_sfx=
 +
moduleclass_sfx=_menu
 +
maxdepth=10
 +
menu_images=0
 +
menu_images_align=0
 +
menu_images_link=0
 +
expand_menu=0
 +
activate_parent=0
 +
full_active_id=0
 +
indent_image=0
 +
indent_image1=
 +
indent_image2=
 +
indent_image3=
 +
indent_image4=
 +
indent_image5=
 +
indent_image6=
 +
spacer=
 +
end_spacer=
  
 +
 +
    [user] => 0
 +
    [name] => mainmenu
 +
    [style] =>
 +
)
 +
</pre>
 +
===Example 3===
 +
If the module does not exist and it has the prefix 'mod_' then a dummy stdClass module object will be returned.
 
<source lang="php">
 
<source lang="php">
jimport('joomla.application.module.helper');
+
jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule('mod_examplemodule');
+
$module = JModuleHelper::getModule( 'mod_examplemodule' );
 
echo '<pre>';
 
echo '<pre>';
print_r($module);
+
print_r( $module );
 
echo '</pre>';
 
echo '</pre>';
/*RESULT:
+
</source>
stdClass Object
+
which will output:
 +
<pre>stdClass Object
 
(
 
(
 
     [id] => 0
 
     [id] => 0
Line 73: Line 124:
 
     [user] => 0
 
     [user] => 0
 
)
 
)
*/
+
</pre>
</source>
+
 
 
===See also===
 
===See also===
* [[JModuleHelper/renderModule]]
+
* [http://api.joomla.org/Joomla-Framework/Application/JModuleHelper.html#getModule JModuleHelper::getModule on api.joomla.org]
 +
* [[JModuleHelper/getModules|JModuleHelper::getModules]]
 +
* [[JModuleHelper/renderModule|JModuleHelper::renderModule]]
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JModuleHelper]]</noinclude>
 
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JModuleHelper]]</noinclude>

Latest revision as of 14:54, 7 February 2009

Returns a stdClass object containing information about the module requested. A given module will only be returned if it meets the following criteria:

  • it is enabled;
  • it is assigned to the current menu item or is assigned to all menu items;
  • the user meets the access level requirements.

Contents

Syntax

object getModule( $name, $title )

where:

Argument Data type Description Default
$name string The name/type of the module. For example, 'login' for the login form module. If more than one module of this name/type exists and no title has been specified then the first module of the given name/type is returned. If the module is not found and the 'mod_' prefix has been used then a dummy stdClass object will be returned.
$title string The title of the module. If null the first module of the given name/type is returned. null

Example 1

Obtain the login form module.

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'login' );
echo '<pre>';
print_r( $module );
echo '</pre>';

which will output:

stdClass Object
(
    [id] => 18
    [title] => Login Form
    [module] => mod_login
    [position] => left
    [content] => 
    [showtitle] => 1
    [control] => 
    [params] => greeting=1
                name=0
    [user] => 0
    [name] => login
    [style] => 
)

Example 2

If more than one instance of a module exists then you can select the correct one by specifying its title. For example, there are usually many modules of type 'mod_mainmenu', so here we select the one with title "Resources":

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'mainmenu', 'Resources' );
echo '<pre>';
print_r( $module );
echo '</pre>';

which will output:

stdClass Object
(
    [id] => 31
    [title] => Resources
    [module] => mod_mainmenu
    [position] => left
    [content] => 
    [showtitle] => 0
    [control] => 
    [params] => menutype=othermenu
menu_style=list
startLevel=0
endLevel=0
showAllChildren=0
window_open=
show_whitespace=0
cache=1
tag_id=
class_sfx=
moduleclass_sfx=_menu
maxdepth=10
menu_images=0
menu_images_align=0
menu_images_link=0
expand_menu=0
activate_parent=0
full_active_id=0
indent_image=0
indent_image1=
indent_image2=
indent_image3=
indent_image4=
indent_image5=
indent_image6=
spacer=
end_spacer=


    [user] => 0
    [name] => mainmenu
    [style] => 
)

Example 3

If the module does not exist and it has the prefix 'mod_' then a dummy stdClass module object will be returned.

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'mod_examplemodule' );
echo '<pre>';
print_r( $module );
echo '</pre>';

which will output:

stdClass Object
(
    [id] => 0
    [title] => 
    [module] => mod_examplemodule
    [position] => 
    [content] => 
    [showtitle] => 0
    [control] => 
    [params] => 
    [user] => 0
)

See also