include PHP inside custom Drupal modules

Drupal

In an attempt to make my sites more dynamic and easier to manage, I use PHP includes quite extensively. I like the idea of make a change in a single file and have it propagated to other sections of the site that call upon it.

However, when you want to have multiple custom modules in Drupal to include PHP… it gets tricky. Depending on how your modules are written, it is usually not within the same scope as the Drupal core… therefore your variables should be global if you want to call them throughout the site.

Secondly,

include_once() and  require_once() do not work so well in these situations.

I’ll give you an example. I have written a very large and complex custom theme for a client with ecommerce needs. I’m including a file called sales_price.inc.php that defines a bunch of variables for my ecommerce module. This file has been included in numerous modules with include_once().

I’m also using a module called node_content themehook to theme only on a node level.

When I try to include in my node_content templates using the *_once() functions… Drupal thinks the file has been included once even when it has not. This problem is not specific to the node_content module. When you have multiple modules calling the same file… using *_once() is not reliable at all.

The solution…? simple as hell.  Use include() and  require().

Popularity: 1% [?]

  • pavani

    Hi
    I was also including php file into a module block but it appears above the theme irrespective of its location can you please help me to make the display normal.

    Regards,
    Pavani

  • pavaniakella

    Hi
    I was also including php file into a module block but it appears above the theme irrespective of its location can you please help me to make the display normal.

    Regards,
    Pavani

  • frankleng

    I'm not sure what you are asking… can you explain in more detail?

  • frankleng

    I'm not sure what you are asking… can you explain in more detail?

See also: