Joomla!
http://forum.joomla.org/

Hack: Hide any module for logged in users ONLY
http://forum.joomla.org/viewtopic.php?f=178&t=114630
Page 1 of 4

Author:  phlux0r [ Thu Nov 16, 2006 5:54 am ]
Post subject:  Hack: Hide any module for logged in users ONLY

Updated 01. May 2007

This hack has moved from the mosLoadModules function to the &initModules function since that one is being called first by all the other functions. This will allow for column collapsing if the module is hidden and no other modules are published in the position.

Credits go to BradWaite for pointing out to modify &initModules to collapse modules.

You only need this hack in one place, in function: &initModules()!!!!

For a more elaborate hack allowing hiding of modules for certain user types see my post further down: http://forum.joomla.org/index.php/topic ... #msg795120


[This hack is done in J! 1.0.12]

I had a situation where i had a menu that I wanted to replace with the User menu once a user was logged in. In other words I wanted to hide that public menu when a user logged in.

This is such a simple hack that I don't see why this couldn't be part of the core. It involves adding a parameter to the module's xml file (in my case /modules/mod_mainmenu.xml but you can add this parameter to any module you want to hide.

Simply insert this in the position you want in the module.xml file:
Code:
      <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 open up: /includes/frontend.php and find the function &initModules() around line 72. In there you should see this code:
Code:
   foreach ($modules as $module) {
      $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
   }

Insert immediately before the line with $GLOBALS[... the following code:
Code:
               // HACK: added code to check if module should be shown when a user is logged in
      $params = new mosParameters( $module->params );
      if (!$params->get('show_registered', 1) && $my->id) {
         // simply continue the loop without outputting this module
         continue;
      }
      // --- end HACK ---

This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.

Author:  steve4j [ Thu Nov 16, 2006 10:16 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

you sir, deserve a medal ( or a swift kick in the pants for not doing this earlier!) good job!

very elegantly done!

Author:  Robin [ Sat Nov 18, 2006 6:11 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Moderator note; moved from FAQ >> 3rd Party, Hacks/Patches

Author:  QuickSync [ Sat Nov 25, 2006 10:38 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D

Author:  phlux0r [ Sun Nov 26, 2006 1:49 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hi quickwolf,

sorry, but I didn't need that kind of functionality so I haven't looked at hacking that.

Author:  steve4j [ Sun Nov 26, 2006 6:08 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

QuickSync wrote:
Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D


it would seem to me, that if you place all your menu items that should be visible for PUBLIC but invisible for LOGGED IN, you could (should) put all those menu items together in one menu module, then apply the Logged In/out hack to that module.

Author:  toddrip [ Wed Jan 24, 2007 12:57 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

This works perfect!!!!!!!!!  Thanks  ;D

Author:  eskimex [ Sun Jan 28, 2007 8:37 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

can i use this for 1.0.12 ?

Author:  eskimex [ Sun Jan 28, 2007 8:38 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

toddrip wrote:
This works perfect!!!!!!!!!  Thanks  ;D


toddrip,

are you using 1.0.12 ?

Author:  Chinaman [ Sun Jan 28, 2007 11:42 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

I am using it for 1.0.12 and works fine so far for menus. Have not tried it for other modules

Author:  eskimex [ Sun Jan 28, 2007 12:31 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

thanks for the response.

what i need this function for is i have a "become a member" link, and i didnt want it to show to people logged in... cause thats kinda dumb. AND, i could use the space for something else.
so this is what i did.

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


very simple. i didnt use the hack. this was good enough. and i am more comfortable writing it this way.

Author:  phlux0r [ Sun Jan 28, 2007 9:42 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code:
if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }

Author:  eskimex [ Mon Jan 29, 2007 7:32 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

phlux0r wrote:
You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code:
if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code:
if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }




thanks for the tip. i put this code in my template. and im not sure why but i cant get your code to work.
when your code is in my template, i get an error.
unexpected "
so i replaced " with '
but now the php doesnt get read. how do i write this for my template?
thanks!
nate

Author:  audioindonesia [ Wed Mar 28, 2007 5:15 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/mysite/public_html/smunsa/templates/themeku/index.php on line 11

Author:  Comenius [ Thu Mar 29, 2007 10:47 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

The module hack is just perfect.  Thanks so much!!  :D

Author:  josgall [ Sat Mar 31, 2007 12:11 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

i've got another good one, to which I have to give phlux0r the credit  8).

How about a hack for "Hide a module when your site is in SSL or on the secure SSL site for payments systems" - here ya go...


Simply insert this in the position you want in the module.xml file:


Code:
      <param name="hide_for_ssl" type="radio" default="0" label="Hide Menu When SSL is active" description="Hide the menu when the SSL is active">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>


Then open up: /includes/frontend.php and go to line 155. You should see this code:
Code:
   foreach ($modules as $module) {
      $params = new mosParameters( $module->params );


Insert immediately after that the following code:
Code:
      // HACK: added code to check if module should be shown when the user is in SSL
      if ($params->get('hide_for_ssl', 0) && substr( $mosConfig_live_site, 0, 8 ) == 'https://' ) {
      // simply continue the loop without outputting this module
      continue;
      }
      // --- end HACK ---



This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.

Author:  bdcomp [ Sat Mar 31, 2007 8:12 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hi,

I slightly changed the second piece of code, because josgall's version didn't worked for me:
Code:
// HACK: added code to check if module should be shown when the user is in SSL
if ($params->get('hide_for_ssl', 0) && ($_SERVER['HTTPS']=='on')) {
// simply continue the loop without outputting this module
continue;
}
// end HACK


Thank you josgall and phlux0r  :)

Boaz

Author:  BradWaite [ Sun Apr 08, 2007 9:31 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Excellent hack, Phlux0r!

I've made some modifications to the placement so it works with templates that collapse when a module isn't present.

Replace the following code at line 94 of frontend.php

Code:
                foreach ($modules as $module) {
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      



with this:


Code:
                foreach ($modules as $module) {
                        // HACK: added code to check if module should be shown when a user is logged in
                        $params = new mosParameters( $module->params );
                        if (!$params->get('show_registered', 1) && $my->id) {
                                // simply continue the loop without counting this module
                                continue;
                        }      
                        // --- end HACK --
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      


Combine this hack with the mosviewonly/moshideonly mambots from http://www.troozers.com/ and you can really customize your site for registered users.

Author:  somos [ Sat Apr 21, 2007 6:08 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

This is so usefull that should be in every module and in the default installation. I expected to found it without hacking anything ... it seems a common need for me.

Author:  BradWaite [ Sat Apr 21, 2007 5:59 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

For you unix guys, here's a quick perl script to add this parameter to every module.  Just put this into a file called "addparm" and run "addparm *.xml" from your modules/ dir.

Code:
#!/usr/bin/perl -pi

$addparm = qq|\t\t<param name="@spacer" type="spacer" default="" label="" description="" />\n|;
$addparm .= qq|\t\t<param name="show_registered" type="radio" default="1" label="Show to registered users" description="Show this module when a registered user is logged in">\n|;
$addparm .= qq|\t\t\t<option value="0">No</option>\n|;
$addparm .= qq|\t\t\t<option value="1">Yes</option>\n|;
$addparm .= qq|\t\t</param>\n|;

if( /<\/params>/){
        print $addparm;
}

Author:  meekochan [ Mon Apr 30, 2007 9:31 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hi !

thanks for the great hack! it was EXACTLY what i was looking for, both the first hack and the last hack before the unix one

could you add one more "feature"? I was just wondering if it was possible to hide a certain module only if a super administrator was logged in, instead of any registered user? Or it could technically be any specific level of a user, and when that user is logged in, that module will dissapear and the spot on the template will collapse?

If you could do it, it would be great, if not, it works great anyways!

So thanks in advance!

Miyuki

Author:  phlux0r [ Tue May 01, 2007 1:52 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

a) I've updated my original hack so check it out please to see what has changed.

b) Ok, nice challenge meekochan!

This is for Joomla! 1.0.12

Here we go...

To add the possibility of hiding modules for a certain user group (i.e. Authors, Publishers, Managers etc.) this is what is needed:

1) In your /modules/[whatever_module].xml you need to add the following somewhere within the section:

Code:
      <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>
      <param name="hide_for_gid" type="list" default="0" label="Hide Module for this User Type Only" description="If above is set to 'No', you can specify which registered user type you want to hide this module for. Default hides the module for any logged in user.">
         <option value="0">Default</option>
         <option value="18">Registered</option>
         <option value="19">Author</option>
         <option value="20">Editor</option>
         <option value="21">Publisher</option>
         <option value="23">Manager</option>
         <option value="24">Administrator</option>
         <option value="25">Super Administrator</option>
         <option value=">19">Author and above</option>
         <option value=">20">Editor and above</option>
         <option value=">21">Publisher and above</option>
         <option value=">23">Manager and above</option>
         <option value=">24">Administrator and above</option>
         <option value="<19">Author and below</option>
         <option value="<20">Editor and below</option>
         <option value="<21">Publisher and below</option>
         <option value="<23">Manager and below</option>
         <option value="<24">Administrator and below</option>
      </param>


2) Then in /includes/frontend.php in function &initModules() on line 73 change the global line to:

Code:
   global $database, $my, $Itemid, $acl;

(added $acl to the variables)

3) Further down in the same function, replace this code:
Code:
      foreach ($modules as $module) {
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


with this code:
Code:
      // HACK: to hide modules for certain groups of logged in users
      $myRealGid = intval($acl->get_group_id($my->usertype));
      // --- end HACK ---
      foreach ($modules as $module) {
         // HACK: added code to check if module should be shown when a user is logged in and is in a particular group
         $params = new mosParameters( $module->params );
         if (!$params->get('show_registered', 1) && $my->id) {
            // check the gid if param set
            if ($params->get('hide_for_gid', 0) != '0') {
               if (strlen($params->get('hide_for_gid')) > 2) {
                  // if we're looking at ranges, extract the comparator and the gid to compare
                  $comparator = substr($params->get('hide_for_gid'), 0, 1);
                  $hideGid = intval(substr($params->get('hide_for_gid'), 1));

                  if (($comparator == ">" && $myRealGid >= $hideGid) || ($comparator == "<" && $myRealGid <= $hideGid)) {
                     continue;
                  }

               } elseif (intval($params->get('hide_for_gid')) == $myRealGid) {
                  // if the user's gid is equal to the parameter, hide the module
                  continue;
               }
            } else {
               // simply continue the loop without outputting this module
               continue;
            }
         }
         // --- end HACK ---
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


And it's done. You can then set for which kinds of registered users you want the module to be hidden. The parameter list allows you to specify the exact level (i.e. Authors only) or a range like "hide for Editors and above" or "hide for Managers and below" which hides the module for that level user and above/below.

Hope this is useful to some of you, as always, enjoy.

Edit: Just realised that the option: "Registered and below" doesn't make sense (cos it's the same as "Default") so I removed it :).

Author:  Robin [ Tue May 01, 2007 7:04 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Looks very interesting, I'm sure going to check this out!

Author:  meekochan [ Tue May 01, 2007 7:40 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!

And I tried it, and it works great. Thank you so much!

Author:  phlux0r [ Tue May 01, 2007 10:05 am ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Quote:
Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!


Not a problem... it was a nice addition to the hack and not really all to difficult.

If one wanted to be really fancy, one could create a new parameter type for the section say type="acl_group_list" and get it populated with the ACL groups from the database. At the same time making the list multi select (dunno if you could even have multiple values for a parameter - may need some thought) so you could pick which groups to hide the module for. That way this would work even with the JACLPlus extension/hack...

Author:  nluk100 [ Wed May 09, 2007 6:17 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

This is just what I have been trying to sort out for over 2 weeks now since installing my SSL!

Mucho thanks!

Author:  jce108 [ Mon May 14, 2007 7:47 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Hey, I am having problems implementing this hack, I can add the radio button to the XML fine but in the frontend.php whenever i change it from
This:
Code:

foreach ($modules as $module) {
$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



To this:

Code:

foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



The website just displays a blank white screen.
I'm kind of new to coding so any help is greatly appreciated

Author:  phlux0r [ Mon May 14, 2007 9:38 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

Yep, there's a mistake in the hack snippet in the irst post (I fixed it now).

You need to add:

Code:
$params = new mosParameters( $module->params );

before the if statement. So your replacement should look like:
Code:
foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
$params = new mosParameters( $module->params );
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }

Author:  quizzer [ Tue May 15, 2007 3:28 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

I am developing a site that uses "Tabs & Slides In Content Items Plugin" from Joomlaworks and I would like to be able to hide a module when one of the tabs is opened.

I was just wondering if a variation of this hack would do the trick?

Author:  matrix845 [ Sat Jun 23, 2007 2:29 pm ]
Post subject:  Re: Hack: Hide any module for logged in users ONLY

@quizzer
tabs and slides is for a content not for a module  ;)

Page 1 of 4 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/