Modules: Filtering behavior by content type

Hello,

I have something like this in a public custom module. The goal is to read the current group of the resource presented to the user, check its custom fields for a enable/disable checkbox, and include a script if enabled.

public function onOutput($buffer) { // on page output
  global $_LW;
  
  // make group id available, continue if defined
  $_LW->applyPageAndGroupVars('<xphp var="group_id"/>');
  if (!empty($GLOBALS['group_id'])) {
    // obtain custom fields for the current group
    $fields = $_LW->getCustomFields('groups', $GLOBALS['group_id']);
    	
    /* Check to include Feature */
    // get custom field value to determine if should include
    $enable = ($fields['enable_feature'] === "True") ? true : false;
    // if should show, then append 
    if ($enable) {
      $buffer=str_replace('</head>', '<script type="text/javascript" src="/feature.js" defer></script></head>', $buffer);
     }
  }
    
  return $buffer;
}

The intent of this resource was to only display on pages. Instead, I’ve found it appearing on stories and profiles as well.

How can I best filter behavior in the custom module based on the content type being shown? For instance, how can I narrow this down to only apply to pages, or to just stories, etc?

I know there’s $_LW->page, which I could try to work around kind of like this:

  • Check page for /details/profiles to know it’s a profile.
  • Check page for not having /details/ to know it’s (most likely) a page.

But that doesn’t feel certain enough. I’m hoping there’s something more direct like if (type == 'page') available to use.

Thanks,
Nick

Hi Nick,

I think, if it works in your testing, the most straightforward fix would be for your module to check

if (!empty($_LW->is_details_template)) {
 // on a details template
}

Or for the negation of that, you could remove the ! which should indicate that you’re on a “regular” page object and not a news/profile details page. If that doesn’t work as expected, you could also check against the server_path XPHP variable ($GLOBALS[‘server_path’]) to see if you’d rather be having behavior work differently on details and non-details pages.

Hello Karl,

$_LW->is_details_template appears to work for this fairly simple case of “page or not”.

Thanks,
Nick