Wednesday, February 9, 2011

Hiding Modules when a User Logs In

<?php // Get the user object $user = &JFactory::getUser(); 

// Now work out the User ID $userId = $user->get('id'); 

// Now hide the module if user is logged in if ($userId == 0) :   

// Now we break out of the PHP tags and display the JDOC tag to include the module 

?> 
<jdoc:include type="modules" name="public-right" style="xhtml" />

<?php endif; ?> 

Module Position in Joomla

Load One Module Position

 
<?php if ($this->countModules('user1')) : ?>
<div id="user1">
  <jdoc:include type="modules" name="user1" style="xhtml" />
</div>
<?php endif; ?>
 

Load Four Module Positions

 
<?php if ($this->countModules('user1 or user2 or user3 or user4')) : ?>
<div id="userModules">
 
  <?php if ($this->countModules('user1')) : ?>
  <div id="user1">
    <jdoc:include type="modules" name="user1" style="xhtml" />
  </div><!-- end user1 -->
  <?php endif; ?>
 
  <?php if ($this->countModules('user2')) : ?>
  <div id="user2">
    <jdoc:include type="modules" name="user2" style="xhtml" />
  </div><!-- end user2 -->
  <?php endif; ?>
 
  <?php if ($this->countModules('user3')) : ?>
  <div id="user3">
    <jdoc:include type="modules" name="user3" style="xhtml" />
  </div><!-- end user4 -->
  <?php endif; ?>
 
  <?php if ($this->countModules('user4')) : ?>
  <div id="user4">
    <jdoc:include type="modules" name="user4" style="xhtml" />
  </div><!-- end user5 -->
  <?php endif; ?>
 
</div>
<?php endif; ?>
 

3-Column Class Conditionals

 
//Put in Head section
 
  <?php 
  if($this->countModules('left and right') == 0) $contentwidth = "full";
  if($this->countModules('left or right') == 1) $contentwidth = "single";
  if($this->countModules('left and right') == 1) $contentwidth = "both";
  ?>
 
//Then put in Body
 
<div id="middleCol" class="<?php echo $contentwidth ?>">
 
//Then put in CSS
 
#middleCol.full {width: 100%;}
#middleCol.both {float: left; width: 50%;}
#middleCol.single {float: left; width: 75%;}
 

Detecting Logged In State in Joomla

 
<?php
$userattr = JFactory::getUser();
$thisuser = $userattr ->get('guest');
if($thisuser == 0) { ?>   // is this a guest user?
   //yes. do guest user stuff
<?php } else { ?>
   //no. do logged-in user stuff
<?php } ?> 
 

Detecting Frontpage in Joomla

 
<?php 
if ( JRequest::getVar('view') == 'frontpage' ) {
?>
<!-- display your home page HTML here -->
<?php
} else {
?>
<!-- display your internal page HTML here -->
<?php } ?>