Drupal how to set up path for library and include files

In Drupal 7, I am writing PHP from inside the Front Page module, say:

$page = include_once "templates/page.php";

I put the "templates/page.php" under /sites/all/modules/front and Drupal complains about file missing. Any suggestion where best to put up the files to be included?

Then, after installing the PHP Simple HTML DOM Parser, I wrote the following

$html = new simple_html_dom();

and was complained for Class 'simple_html_dom' not found. I have tried duplicating the library file to the same folder and home, but all cases not working. How to set up the correct path?

Many thanks.

Answers


How to include some file?

Method 1

You could place file anywhere under web-root folder which is folder with index.php file (home folder).

$type = 'module';
$name_of_module = 'custom_module';
$path = drupal_get_path($type, $name_of_module);
include_once($path . 'custom_file.inc.php');

The valid types are as listed:

  • module
  • theme
  • profile
  • theme_engine

Module name is the filename of .info file in modules folder. Read more about drupal_get_path()

Method 2

To include file from existing module you should use: module_load_include('inc', 'content', 'includes/content.node_form');

How to include template file?

Better place for template files is theme folder because others will expect templates files there.

Read more about template naming suggestions.

But if this template has no sense without your custom module you could place this template file within custom module folder.

To attach template in theme you should use:
function MYTHEME_preprocess_page(&$variables, $hook)     {
    if (array_key_exists('node', $variables)) {

         if ($variables['node']->type == 'article'){
            $variables['theme_hook_suggestions'][] = 'page__article';
            }
    }
}
To attach template in module:
/**
 * Implements hook_theme().
 */
function MODULE_theme($existing, $type, $theme,    $path) {
  return array (
    'node__CONTENTTYPE' => array (
      'variables' => array( . . . ),
      'template' => 'node--CONTENTTYPE' ,
      'base hook' => 'node',
      'path' => drupal_get_path('module', 'CUSTOM_MODULE'),
    ),
  );
}

Template file should be named node--CONTENTTYPE.tpl.php and placed under CUSTOM_MODULE folder.

Read more about hook_theme()

Do not forget to clear cache.


You should use drupal_get_path to get the full path of your file:

$page = include_once(drupal_get_path('module', 'front') . 'templates/page.php');

Here is the documentation.


The Libraries API provides this function.


Need Your Help

Can Ruby + Crate + Windows work?

ruby

I've got a project for work I'd like to do in Ruby that will have to run on Windows, but perturbing the filesystem for a Ruby install or RubyScript2Exe unpack isn't an option (this is supposed to ...

Problem with Binding Close Command to Button in a Tabcontrol datatemplate

wpf prism command tabcontrol binding

Im Using Composite Aplication Guidiance Pattern for building my WPF application. In my Shell i have a tabcontrol wich contains a region for dynamicly load Views into the region. The views is loaded...