Programmatically Remove Content from Panels (Drupal 7)

At work I was tasked with altering an article node for Nativo. What is Nativo?

Nativo is the leader in true native advertising: brand content served within publisher editorial streams, matched to the look and feel of each publication, fully integrated and consumed on the publisher site, across screens to deliver: Higher engagement rates for marketers. Premium monetization for publishers.

The short version of my task, remove all ads from the page. Some of the ads are placed on to the page by assigning the block to a region the block, while others are in panels or in this case mini-panels.

First off lets take care of the ads in regions. If you’re using the DFP module you can pretty much just copy and paste this code. This goes in your template.php file.


 /**
 * Implements hook_block_list_alter().
 */
 function MY_THEME_block_list_alter(&$blocks) {
   $node = menu_get_object();
 // is_nativo is set in a helper module.
 // nativo - remove DFP ads
 // *this does not work on panels just those placed via structure->blocks
   if (!empty($node) && !empty($node->is_nativo)) {
     foreach ($blocks as $id => $block) {
       if (trim($block->module) == 'dfp') {
         unset($blocks[$id]);
       }
     }
   }
 }
 

How we do this for panels though?  I searched for what hooks panels has and tried 3 different ones. I finally settled on using HOOK_panels_pane_content_alter. In the code I check to see that it’s the DFP module block or I mean ‘content’ and then null it out.

This code goes in your custom module.

function CUSTOM_MODULE_panels_pane_content_alter($content, $pane, $args, $contexts) {
  if ($content->module == 'dfp') {
    $content->content = '';
    $content->module = null;
  }
}

Now if this makes the entire region to disappear you can fix it putting something in to the content. I chose to put an empty <div/>.

Leave a Reply