1

I am creating a module that is outputting a static php page into my Drupal install. Here is my module file:

<?php

function mymodule_menu() {
$items = array();

$items['live_amplified/hello_world'] = array(
'title' => 'Live Amplified',
// describe the page for the menu system.  site visitors will not see this
'description' => 'Simple custom hook_menu() implementation.',
// function that is called when visiting the new path
'page callback' => 'drupal_get_form',
'page arguments' => array('static_page_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
 );

return $items;
}

/**
* Implements hook_form().
*/
function static_page_form($form, &$form_state) {
// Add CSS and JS
drupal_add_css(drupal_get_path('module', 'mymodule') . '/static_page.css');
// Import your static HTML page
$form['html'] = array(
'#type' => 'item',
'#markup' => file_get_contents(drupal_get_path('module', 'mymodule') .'/static_page.php'),
);
return $form;
}

In my static_page.php I have written my HTML, but when I try to add any PHP the browser doesn't recognize it. It doesn't output anything. I have tried just echo test and still nothing.

  • You're misusing file_get_contents() which simply reads a file into a string (it won't evaluate any PHP contained within it). Look into Drupal's hook_theme. That will allow you to create a custom template file. – Adam Balsam Nov 6 '14 at 22:11
2

You don't need to use a form items markup attribute just to print some HTML. Instead, change your menu item to a simple page function, not a drupal_get_form eg

$items['live_amplified/hello_world'] = array(
  'title' => 'Live Amplified',
  'description' => 'Simple custom hook_menu() implementation.',
  'page callback' => 'hello_world_page',
  'page arguments' => array('static_page_form'),
  'access arguments' => array('access content'),
  'type' => MENU_NORMAL_ITEM,
);

and add a simple function to return some markup

function hello_world_page() {
  return "Hello World";
}

But as @Adam Balsam described, if you have a lot of content and want to parse another PHP file, you would do it using file_get_contents() but Drupal already has a way of dealing with this that uses it (although it may be convoluted)

You would use hook_theme.

First you need to declare your implementation, this does two things: tells Drupal to look for a template file (the file you want to render) at some location, and it exposes your template to prerocessing for adding variables.

Declare your theme

/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'helloworld' => array(
      'template'  => 'hello-world',
      'path'      => drupal_get_path('module', 'mymodule') . '/templates',
      'arguments' => array('node' => NULL),
    ),
  );
}

Create a preprocess function to give it some variables

function mymodule_preprocess_helloworld(&$vars) {
  $vars['who'] = "World";
}

Create a template file hello-world.tpl.php in /mymodule/templates/ with the content

<h2>Hello <?php print $who; ?> </h2>

Call it from where ever you like, in this case a menu page.

function hello_world_page() {
  return theme('helloworld');
}

At first it may seem a bit convoluted but it allows you to create templates with variables and exposes theme to themeing so other modules and themes can change both the variables and the markup.

2

This is just a slightly different way to do it than @user1081275's approach.

My situation was that I wanted to turn off the PHP Filter module for security reasons and I had several pages that had PHP in the body field.

I created a module that I put at sites/all/modules called "customphppages'. It had the basic .info and a directory inside called 'pages' where there would be one tpl.php file for every page on the web site that I wanted to create this way.

The tpl.php files are just for the content. You don't need to copy page.tpl.php (like I did the first time)... just your content goes in the tpl.php file.

The example shows the declaration of two pages for the site.

The following is from customphppages.module

<?php
/* 
 *Implementation of hook_menu().
 */

function customphppages_menu() {


  $items['movies/the-wizard-of-oz'] = array( //this is the URL, what comes after example.com/
    'title' => 'The Wizard of Oz', // this is the page title
    'page callback' => 'page_oz', // function name... can be anything
    'access callback' => TRUE, // this means no access control for page
    'type' => MENU_CALLBACK, // the same for every new page
  );
   $items['books/brave-new-world'] = array(
    'title' => 'Brave New World',
    'page callback' => 'page_brave',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/*
 * Implementation of hook_theme().
 */
function customphppages_theme(){
  return array(
    'woz_template' => array( //function name called below
      'template' => 'woz', // what comes before '-tpl.php' in the file name for the file with the content. So the file would be at sites/modules/customphppages/pages/woz.tpl.php
      'path' => 'sites/all/modules/customphppages/pages' //path to folder containing tpl.php.
    ),
    'brave_template' => array(
      'template' => 'brave',
      'path' => 'sites/all/modules/customphppages/pages'
    ),
  );
}
/* 
 * One function here for each page. Function names are 
 * derived from the 'page callback' value in the 
 * $items array. The quoted argument for theme()
 * is the key in in the custom array in customphppages_theme() function.
 */
function page_oz() {
  return theme('woz_template');
}

function page_brave() {
  return theme('brave_template');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.