Hack: Hide any module for logged in users ONLY

Your code modifications and patches you want to share with others.
ewel
Joomla! Guru
Joomla! Guru
Posts: 522
Joined: Mon Oct 01, 2007 11:35 am

Re: Hack: Hide any module for logged in users ONLY

Post by ewel » Fri May 22, 2009 8:01 am

You could try looking at the javascript triggered by opening a tab, and then see if you can add to that trigger something that will hide the module using JPane. But I think it would be a lot of hassle.

da8iwr
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 190
Joined: Sat Jan 14, 2006 1:59 pm
Location: Sunderland UK
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by da8iwr » Fri May 22, 2009 12:54 pm

With great thanks to coolparth
http://forum.joomla.org/viewtopic.php?p ... 9#p1703749

Code: Select all

<?php
					$my =& JFactory::getUser();
					//Followed by your code..
					if($my->id>0){
					echo "<jdoc:include type=\"modules\" name=\"right\" headerLevel=\"3\" />";
					}else
					{
					echo "<jdoc:include type=\"modules\" name=\"top\" headerLevel=\"3\" />";
					}
				?>
I now have this working perfect,
Thanks :)

ryadavalli
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 114
Joined: Sat Nov 04, 2006 1:39 pm
Location: Hyderabad, India
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ryadavalli » Wed Jul 08, 2009 12:38 am

If any of you are still looking for this hack for 1.0.15, for a custom module (type USER), you would have to edit custom.xml. Took me a while to figure it out, and thought it might help someone.
Thanks for the hack.

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Tue Jul 28, 2009 8:44 pm

anyone get this working for 1.5 AND the hack for only certain users? There's no frontend.php file in 1.5...

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11312
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ooffick » Tue Jul 28, 2009 8:52 pm

Hi,

You can use MetaMod to do that.

Olaf
Olaf Offick - Global Moderator
learnskills.org

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Tue Jul 28, 2009 9:03 pm

and that module will allow me to hide other modules or just that one module?

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11312
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ooffick » Tue Jul 28, 2009 9:13 pm

You can show different modules and hide any module which is loaded by that module.

Olaf
Olaf Offick - Global Moderator
learnskills.org

User avatar
RustyJoomla
Joomla! Explorer
Joomla! Explorer
Posts: 395
Joined: Wed Aug 01, 2007 12:08 am
Location: UK
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by RustyJoomla » Fri Mar 19, 2010 12:20 pm

[quote="kevincam"][/quote]

Kevincam,thanks very much for the hack! Works' great!!! :D :D :D
Follow me on Twitter - http://twitter.com/RustyJoomla
Company Joomla Website - https://storm.agency

Xeonxz
Joomla! Intern
Joomla! Intern
Posts: 99
Joined: Sat Mar 25, 2006 1:01 pm

Re: Hack: Hide any module for logged in users ONLY

Post by Xeonxz » Thu Mar 25, 2010 5:58 am

Hi,

Here is a solution for Joomla 1.5.

1. Download and install Jumi: http://extensions.joomla.org/extensions/1023/details
2. Edit the module you want to hide for regestrated users on your site. In the combo box labelled "Position". Intead of using the drop down list, type in a name of your liking. For example user100.
3. Now you create a new Jumi module.

Settings: no title and choose a position where you want to show your module.

Insert this code to inside "Code written" in the Jumi module.

Code: Select all

<?php
$position = 'user100';
$user =& JFactory::getUser();
if($user->guest)
{
  $document	= &JFactory::getDocument();
  $renderer	= $document->loadRenderer('modules');
  $contents = '';
  echo $renderer->render($position, array('style'=>'xhtml'));
}
?>
 

Best Regards
Viktor
3on Webbyrå Stockholm http://www.3on.se
Flighton AB http://www.flighton.se

jgage
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Fri Dec 25, 2009 11:33 pm
Location: USA, Texas
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by jgage » Fri Mar 26, 2010 8:17 am

da8iwr wrote:With great thanks to coolparth
http://forum.joomla.org/viewtopic.php?p ... 9#p1703749

Code: Select all

<?php
					$my =& JFactory::getUser();
					//Followed by your code..
					if($my->id>0){
					echo "<jdoc:include type=\"modules\" name=\"right\" headerLevel=\"3\" />";
					}else
					{
					echo "<jdoc:include type=\"modules\" name=\"top\" headerLevel=\"3\" />";
					}
				?>
I now have this working perfect,
Thanks :)
Me a lika very much sir! Ita Saves my day!

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Mon May 03, 2010 5:13 pm

no longer working since 1.5.16 - I tried to redo hack with no luck. Did I do something wrong or are we going to have to find another way to do this?

jgage
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Fri Dec 25, 2009 11:33 pm
Location: USA, Texas
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by jgage » Mon May 03, 2010 9:03 pm

Code: Select all

<?php
               $my =& JFactory::getUser();
               //Followed by your code..
               if($my->id>0){
               echo "<jdoc:include type=\"modules\" name=\"right\" headerLevel=\"3\" />";
               }else
               {
               echo "<jdoc:include type=\"modules\" name=\"top\" headerLevel=\"3\" />";
               }
            ?>
You tried this code? This one worked flawless for me. However, am using 1.5.15 so if this one doesn't work then it would be good to know.

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Mon May 03, 2010 9:12 pm

jgage wrote:

Code: Select all

<?php
               $my =& JFactory::getUser();
               //Followed by your code..
               if($my->id>0){
               echo "<jdoc:include type=\"modules\" name=\"right\" headerLevel=\"3\" />";
               }else
               {
               echo "<jdoc:include type=\"modules\" name=\"top\" headerLevel=\"3\" />";
               }
            ?>
You tried this code? This one worked flawless for me. However, am using 1.5.15 so if this one doesn't work then it would be good to know.
where does that code go?

User avatar
ooffick
Joomla! Master
Joomla! Master
Posts: 11312
Joined: Thu Jul 17, 2008 3:10 pm
Location: Ireland
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ooffick » Tue May 04, 2010 9:58 am

You can add that into your template's index.php file, moreover you can use the metamod module as well.

Olaf
Olaf Offick - Global Moderator
learnskills.org

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Wed May 05, 2010 4:24 pm

ok, it actually does still work - I just had to redo it and I messed up first time. This is from kevincam's post on page 3:

Added this after the last parameter of the XML file for whatever module you want to hide for example the login module which is located here (modules\mod_login\mod_login.xml):

Code: Select all

<param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
<option value="0">No</option>
<option value="1">Yes</option>
</param>

Then replace the function render( $position, $params = array(), $content = null ) in \libraries\joomla\document\html\renderer\modules.php with the following

Code: Select all

function render( $position, $params = array(), $content = null )
{
$renderer =& $this->_doc->loadRenderer('module');

$contents = '';
foreach (JModuleHelper::getModules($position) as $mod) {
// HACK: added code to check if module should be shown when a user is logged in
$pattern = '/show_registered=0/';
$match = preg_match($pattern, $mod->params);
$userId = $_SESSION["__default"]['user']->id;
if ($match && $userId) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---
$contents .= $renderer->render($mod, $params, $content);
}
return $contents;
}
UPDATED: I noticed that modules that use the module count need an additional hack to the file:
\libraries\joomla\application\module\helper.php

after the line $modules = array();
and before the line $db->setQuery( $query );

Code: Select all

//HACK check in logged in then run query to exclude the correct modules
if($userId = $_SESSION["__default"]['user']->id >0){
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. " AND m.id NOT IN (SELECT id FROM #__modules WHERE params LIKE '%show_registered=0%')"
. $wheremenu
. ' ORDER BY position, ordering';
}else{
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
}
//HACK
/*ORIGINAL CODE
$wheremenu = isset( $Itemid ) ? ' AND ( mm.menuid = '. (int) $Itemid .' OR mm.menuid = 0 )' : '';
$query = 'SELECT id, title, module, position, content, showtitle, control, params'
. ' FROM #__modules AS m'
. ' LEFT JOIN #__modules_menu AS mm ON mm.moduleid = m.id'
. ' WHERE m.published = 1'
. ' AND m.access <= '. (int)$aid
. ' AND m.client_id = '. (int)$mainframe->getClientId()
. $wheremenu
. ' ORDER BY position, ordering';
ORIGINAL CODE*/

Josh_D
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Jun 02, 2010 6:32 pm

Re: Hack: Hide any module for logged in users ONLY

Post by Josh_D » Fri Jun 04, 2010 11:13 am

Okay, what's the best way to do this in Joomla 1.5.15?

I just want to hide one module. Not the entire position (e.g. top) just the one of multiple modules that appear in that position.

I used MetaMod.

Does anyone know how I can have a different css for logged in user than for non-logged in user?
Last edited by Josh_D on Fri Jun 04, 2010 11:39 am, edited 1 time in total.

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Fri Jun 04, 2010 11:21 am

this method is still valid, but a lot of people are using metamod instead. this is the post that explains how to do it in 1.5: http://forum.joomla.org/viewtopic.php?p ... 3#p1323633

Josh_D
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Wed Jun 02, 2010 6:32 pm

Re: Hack: Hide any module for logged in users ONLY

Post by Josh_D » Fri Jun 04, 2010 11:40 am

Cool, thanks.
I used MetaMod.

Does anyone know how I can have a different css for logged in user than for non-logged in user?
Can that also be done in metamod?

User avatar
Living Legend
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Tue Jun 16, 2009 7:33 pm
Location: UK
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by Living Legend » Sat Aug 07, 2010 8:38 pm

Ach, spent nearly 3 hours trying to get kevincam's hack to work (kept getting a parse error, unexpected "{" expecting T_FUNCTION on line 54 of the modules.php file) then I come across the post about MetaMod... D'oh! Live & leanr I guess coz it took a fraction of the time to get started with, however I added "if ( MM_LOGGED_IN ) return 17;" to the PHP box hoping it would show the User Menu (in place of the login form) but it doesn't show anything at all once I've logged in.

Also, the mod'd form doesn't show the Open ID links, I tried adding its ID after the login form's one (yeah I'm aware that it's a plugin and not a module, but as of yet I've not been able to find any Open ID modules so I figured it was at least worth a punt :p ) but it didn't work.

Anyone got an idea how to resolve these? ??? ??? ???

(fyi for anyone who decides to try it out themselves on my site, I've left the original login form next to the MetaMod'd one to make it easy for myself to compare differences in them :D )
It's not a signature, it's a dictatorship! :-(

yajra
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Mar 24, 2010 6:59 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by yajra » Fri Jan 14, 2011 3:03 am

Hi,

This is my solution for Joomla 1.5.xx

The hack will allow specific module to be displayed for a specific type of user(/s).

1. Replace render function with code below @ \libraries\joomla\document\html\renderer\modules.php

Code: Select all

    function render($position, $params = array(), $content = null) {
        $renderer = & $this->_doc->loadRenderer('module');

        $contents = '';
        foreach (JModuleHelper::getModules($position) as $mod) {
            // BEGIN HACK
            // added function to check if module is allowed to be displayed for a specific user typ
            $gId = JFactory::getUser()->gid;
            for ($i = 0; $i < 26; $i++) {
                if ($i == $gId) {
                    $pattern = '/show_' . $i . '=0/';
                    $hidemodule = preg_match($pattern, $mod->params);
                }
            }
            if ($hidemodule) {
                // simply continue the loop without outputting this module
                continue;
            }
            // END HACK
            $contents .= $renderer->render($mod, $params, $content);
        }
        return $contents;
    }
2. Add the paramaters below to your mod_{module}.xml file

Code: Select all

<param type="spacer" default="<b>Display Module For...</b>" />
<param name="show_0" type="radio" default="1" label="Public Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_18" type="radio" default="18" label="Registered Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="18">Yes</option>
</param>
<param name="show_19" type="radio" default="1" label="Author Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_20" type="radio" default="1" label="Editor Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_21" type="radio" default="1" label="Publisher Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_23" type="radio" default="1" label="Manager Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_24" type="radio" default="1" label="Administrator Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
<param name="show_25" type="radio" default="1" label="Super Administrator Users" description="Toggle Module Display for specific User Type">
	<option value="0">No</option>
	<option value="1">Yes</option>
</param>
You do not have the required permissions to view the files attached to this post.

ThePiston
Joomla! Guru
Joomla! Guru
Posts: 639
Joined: Mon Nov 07, 2005 3:45 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Post by ThePiston » Tue Apr 05, 2011 12:06 pm

yajra, that hack does make the module invisible, but it does not remove the module form the structure. In other words, the space for the module is still there. For example, mine is set to show on my front page. When I log in the module is gone, but the left column is empty.

So your hack is not really working as it should. I recommend going back to the hack on page 3.


Locked

Return to “Core Hacks and Patches”