drupal-7drupal-modulesphp-includedrupal-hooks

In Drupal 7 include a php file in block from a custom module


I am new to Drupal and PHP. I am trying to create a module that when activated creates a page, then creates a block, configures block to only show on the created page, and then display an included PHP file. The PHP file that is included is for accessing an off site database.

I have managed to code the module to create the page, and create the block. But the following is my issue.

In the hook_block_view, for content I have input a function name, below I defined the function to include testpage.inc. that page loads but the content does not load in the block's specified region. If I just input text for the content, it loads in the region no problem.

When I define the include as the output value, the block loads the file path, and the included file loads at the top of the page outside of the block.

The Included file is a PHP file that accesses an off site web-service to display a company's Inventory.

<?php

//////////////////////////////////////////////////////////////////
/** using hook_menu to create a page for the module - hook_menu**/
/////////////////////////////////////////////////////////////////
function sps_webconnect_menu() {
  $items['products']=array(
      'title' => 'Products',
      'type' => MENU_NORMAL_ITEM,
      'page callback' => 'sps_webconnect_products',
      'access callback' => TRUE,
      );
  return $items;
}

/** This function defines the page callback from above**/
function sps_webconnect_products() {
  return "&nbsp";
}

////////////////////////////////////////////////////////////////
/** Creates and Defines Block - hook_block_info**/
////////////////////////////////////////////////////////////////
function sps_webconnect_block_info() {
  $blocks['sps_webconnect_block'] = array(
      'info' => t('Products Page - SPS Web Connect'),
      'cache' => DRUPAL_NO_CACHE,
      'status' => TRUE,
      'region' => 'content',
      'visibility' => BLOCK_VISIBILITY_LISTED,
      'pages' => 'products',
  );
  return $blocks;
}


//////////////////////////////////////////////////////////////////////
/* This function defines the content of the block - hook_block_view */
/////////////////////////////////////////////////////////////////////
function sps_webconnect_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'sps_webconnect_block':
      $block['subject'] = t('SPS Product Page');
      $block['content'] = sps_webconnect_file();
      break;
  }
  return $block;
}


function sps_webconnect_file(){
  $output = '';
  module_load_include('inc', 'sps_webconnect', 'testpage');
  return $output;
}

Solution

  • Correct, The inc File did include an Echo. When I included a function the inc file and called that function for Output the inc file was loaded in the block. I found out that the reason for this is that Drupal uses Output Buffering, which compiles all of the markup before sending it to the browser. The File I was trying to include was not output buffer friendly.