obtain the ID of a joomla module

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
bobmeetin
Joomla! Intern
Joomla! Intern
Posts: 80
Joined: Thu Sep 27, 2007 1:24 am

obtain the ID of a joomla module

Post by bobmeetin » Wed Oct 15, 2014 1:24 pm

I need to get the ID of a module that has been called into a variable so that I can run a test against it. The test will determine how the module is presented.

Ideally:

if ( $module_id == 123 )
{ do this; }
else
{ something else; }

I saw this Object code which prints out the ID along with a bunch of other stuff, but I don't understand how to extract the ID only and get it into a variable.

jimport( 'joomla.application.module.helper' );
$module = JModuleHelper::getModule( 'login' );
echo '<pre>';
print_r( $module );
echo '</pre>';
Last edited by imanickam on Thu Oct 16, 2014 2:21 am, edited 1 time in total.
Reason: Moved the topic from the forum General Questions/New to Joomla! 3.x to the forum Joomla! 3.x Coding

User avatar
jackrabbit
Joomla! Ace
Joomla! Ace
Posts: 1473
Joined: Thu May 21, 2009 3:12 am
Location: Florida
Contact:

Re: obtain the ID of a joomla module

Post by jackrabbit » Wed Oct 15, 2014 5:56 pm

If trying to get the ID in a template, use a database call

Code: Select all

$db	= JFactory::getDBO();
$query = 'SELECT id FROM #__modules WHERE id = 123';
$db->setQuery($query);
$moduleID = trim($db->loadResult());

if ( $moduleID ) { do stuff; } else { do other stuff; }
Reset Joomla super user password and username simply | http://cmsenergizer.com/website-energy- ... d-remotely

bobmeetin
Joomla! Intern
Joomla! Intern
Posts: 80
Joined: Thu Sep 27, 2007 1:24 am

Re: obtain the ID of a joomla module

Post by bobmeetin » Wed Oct 15, 2014 6:05 pm

$query = 'SELECT id FROM #__modules WHERE id = 123';

Thanks but unfortunately that does not help because I don't have the ID, 123.

I am using JUMI in a module to run a PHP script. In that script I need to identify the module ID (and some other module parameters, for instance alias or note).

User avatar
jackrabbit
Joomla! Ace
Joomla! Ace
Posts: 1473
Joined: Thu May 21, 2009 3:12 am
Location: Florida
Contact:

Re: obtain the ID of a joomla module

Post by jackrabbit » Wed Oct 15, 2014 6:59 pm

If working within a module, use $module->id;

This statement says you already know the module ID
Ideally:

if ( $module_id == 123 )
{ do this; }
else
{ something else; }
Reset Joomla super user password and username simply | http://cmsenergizer.com/website-energy- ... d-remotely

bobmeetin
Joomla! Intern
Joomla! Intern
Posts: 80
Joined: Thu Sep 27, 2007 1:24 am

Re: obtain the ID of a joomla module

Post by bobmeetin » Wed Oct 15, 2014 7:39 pm

$module_id = $module->id;

That works.

The if statement was hypothetical, not presumptive. i.e. If you educate me with the method to determine the module ID, I will run the if/else.

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: obtain the ID of a joomla module

Post by sovainfo » Thu Oct 16, 2014 1:27 am

The statement

Code: Select all

$module = JModuleHelper::getModule( 'login' );
makes the $module->id available. You don't need to assign it to a variable. You should use

Code: Select all

if ($module->id > 0 and $module->position == 'top') { echo $module->module; }
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!


Locked

Return to “Joomla! 3.x Coding”