1

I have a "facility" content type which I am creating programmatically, through a module called "facility". I would like to have a landing page, such that when you go to http://www.mysite.com/facilities, you are taken to a heavily styled/scripted page that displays facility information in a specific format. To do this, in my facility module, I implemented hook_menu to point to a callback that will return the necessary markup. Here's my code:

facility.module

<?php

function facility_menu() {
    $items['facilities'] = array(
        'title' => 'Facilities',
        'page callback' => 'facility_landing',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK
    );

    return $items;
}

function facility_landing() {
    return file_get_contents(drupal_get_path('module', 'facility') . "/facility_landing.php");
}

Due to the length and complexity of the markup, I would like to have it in a separate file. The issue is that this markup is not being evaluated in the Drupal context, since it is being read in with PHP's file_get_contents function. For instance, if my markup contained...

<h1><?php echo $user->uid; ?></h1>

...nothing would be displayed. Is there a function within Drupal that will parse a PHP file in the bootstrapped environment, and return the generated string? If not, what is the best/easiest way to handle this situation?

EDIT: This just occurred to me - I've already created an API within Drupal that would allow me to access the facility info I need via AJAX. I could just output the page without the facility data, then include a JS file which would get that. I'm still not convinced that's the best way, though. Thoughts?

3

If you want your code to live outside your module's .module file, hook_menu has a way to tell Drupal where to look for your page callback. A common name for include files containing page callbacks is MODULENAME.pages.inc. Place this file in the same folder as your .module file. Just add one line to your menu item definition:

function facility_menu() {
  $items['facilities'] = array(
    'title' => 'Facilities',
    'page callback' => 'facility_landing',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'facility.pages.inc',
  );
  return $items;
}

Now Drupal will load the facility.pages.inc file when example.com/facilities is requested. In facility.pages.inc you place the facility_landing() function. Inside facility_landing() you call other functions to generate the page and finally you return the markup (Drupal 6) or render array (Drupal 7) for your landing page.

| improve this answer | |
  • This is a good idea, but it would still require me to be writing markup as strings within my code...which I am really trying to avoid, if at all possible. – rybosome Nov 6 '11 at 20:45
  • 2
    Well, that's what the theme system is for - you are looking for template files. Implement hook_theme in your module, set up the variables for your theme and create a template file where those variables are printed. See Using the theme layer (Drupal 6.x) or Using the theme layer (Drupal 7.x) for detailed documentation. – marcvangend Nov 6 '11 at 21:18

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.