Show a module on every page that another module does not appear

(6 votes, average 4.50 out of 5)

In this convenient recipe we tell MetaMod to display a module on every page that another particular module is not displaying on. It checks to see if the module has been published on “all” pages, or onto the specific one being displayed right now. If it can’t find it, MetaMod displays an alternative.

When might you want to use this? You might have a particular banner that gets put onto most pages on your site, but you want a different module to go onto all the pages that the first banner doesn’t go onto. This recipe makes it easy to manage this situation.

All you have to tell it is the module that it has to check for (23 in this example), and the module id to display if module 23 is not found on the page.

$moduleid = 23; /* the module we are checking for. Customise this! */
$sql = 'SELECT count(*) as c from #__modules m, #__modules_menu mm ' .
  'WHERE mm.moduleid = m.id ' .
  'and m.published = 1 ' .
  'and (mm.menuid = ' .
  (int)$Itemid . ' or mm.menuid = 0) and mm.moduleid = ' . (int)$moduleid;
 
$db->setQuery( $sql, 0, 1 );
$row = $db->loadObject();
 
$not_there = ($row == null or $row->c == 0);
 
/* replace 101 with module id to display when the other module is not assigned to this page */
if ($not_there) return 101;