Hello,

I am beginner in Drupal, I'd like to call some custom PHP functions from content. Where is the best place for them? I thought make a module ('custom.module'), enable it in modules section, write some functions there and call them from content. But I don't know, if this is a standard way?

Thanks guys!

Comments

tostinni’s picture

See my title, I think this is what you want...
Well, can you explain a little what you really want ?
I didn't understand " I'd like to call some custom PHP functions from content", do you want to display a php date inside a page ???

gryste’s picture

Is it possible to define PHP snippets somewhere outside of the node such that you could execute the same code on several nodes?

e.g. There's one snippet that displays the last n nodes of a certain taxonomy term. Is there somewhere that you could you create a function lastn and then simply call it from within you page?

So, you'd define the function like this:

function lastn($n,$tid)
{
  ...php snippet...
}

Then on every node you wanted to execute the code all you'd need to do would be something like this:

  $n=5;
  $tid=10;
  lastn($n, $tid) 

Thanks.

2ge’s picture

no, I have to declare my own functions, they are not simple as calling date function, I will have some ini values, and based on those content will be generated from some external files (html, pictures, flash...).
So I need one general own function and call it from PAGE content.

javanaut’s picture

I would suggest putting your code in a separate .inc file and including it from your settings.php file. The default settings file is in sites/default/settings.php. If you created the file sites/default/myfunctions.php, you could included it from settings.php as:

include('myfunctions.php');

This assumes that this directory is in PHP's include_path.

pobster’s picture

This module is exactly what you're looking for...

<a href="http://drupal.org/node/17571">Code Snippets</a>

Module offer functionality similar to standard PHP-execution filter, but in more flexible manner. Site admin or power users may create
named snippets of PHP-code (with access to Drupal API), then categorize and store them to the site code repository. Particular users can't view PHP-code - only snippet name and help. From user point of view snippets are macros, which may be used in postings - nodes and comments. Macros may accept parameters which transferred to snippet code. Example: [macro], [macro|param1|param2], [link|9165]. You may imitate many filters such manner, instead of usage of separate modules for each small filter and allow users to create dynamic pages.
Note: module in alpha stage now, but works. See *.txt files for details about current version.

Pobster

2ge’s picture

Will try this, I'm using include in as wrote javanaut, but this sounds to me more clear. Also thanks for nice reply - will try this for sure!

Just one question: when I am on root category (home), I have list of nodes (excerptions). How to display now to each node its own NID ? With this I'm able to call own function, what I need.

Thanks.

kiz_0987’s picture

Be careful with just using include. If you have 2 nodes that use the same function then viewing each separately will be OK, but anytime you view the two together (eg via taxonomy) or with cron.php you may get a redeclare error. Use include_once instead.

2ge’s picture

Ok, I changed that to include_once, but I guess "sites/default/settings.php" is always including only one time, and I'm including from this file.

DVV’s picture

how can I include it from settings.php file?

Putting in settings.php a simple line such as "include ('my_custom_functions.php')" doesn't work :-(

javanaut’s picture

The include() function docs are here:
http://www.php.net/include/

Those docs reference the include_path which is documented here:
http://www.php.net/manual/en/ini.core.php#ini.include-path

I hope this helps

javanaut’s picture

setting the include_path setting can be done with the following code:

ini_set('include_path', '.:/php/includes');

You'll want to make sure to include the directory containing your inc file.

DVV’s picture

thank you for your answer but what has the php manual to do with my question? All I needed was a short - as short as possible - example of the including line in 'settings.php'... Shall I put an 'include...' statement somewhere in that file or what?

Unfortunately I have no access to the php.ini file to the place where I host my Drupal site so I cannot mess around with it...

Till I find the mysterious way of using 'settings.php' I just pasted my humble custom functions in a Drupal core file and it works (till now :->).

Thanks anyway for your answers

CyberGhost636’s picture

Dunno if anyone solved the problem of includes and requires.

I have Drupal 6.12 installed. What I put into settings.php is simply "require_once('file.php')" and then create the "file.php" file in root folder where Drupal is installed. It gets picked up and works like a charm.

Of course, you can create a this file under any folder within Drupal folder structure and point the requiring function there.

Hope this helps.

pamphile’s picture

subscribing to thread

Sergio’s picture

I'm having the same problem and I'd like to know if you ever figure out where to put custom functions in other place other than the core inc files.

nevets’s picture

Simple approach is make a module, you will need the .module and .info files. You can then add functions to the .module file that can be called from elsewhere. To avoid name clashes your function names should start with your module name.

Sergio’s picture

I wanted to create a custom pager for my site, so the first thing that I did was to create a simple module to hold my custom function. I followed the instructions here http://drupal.org/node/416986
Then I put my pager function in the .module file and now this function can be called from any other module if I need to.

Since I'm new to Drupal it was a good exercise to learn more a little bit about this great software.

You can see the pager in action at the bottom of my home page: http://www.f1community.org/

In case you are wonder why I wanted to create a custom pager, it is because the core pager.inc make use of the theme_item_list that is based on (li) tags and when you style the list it would affect any other item_list around the site.

Thanks for the tip.

nevets’s picture

Another solution would be to override theme_pager().

Sergio’s picture

Yes, you can do that, as a matter of fact I tried it but did not work the way I wanted, because the override function would be use for every module that uses the ul li list.
I wanted my custom pager to be use for one module only.

The best option is to create your module to hold your functions, so you have more control over it.

ruin’s picture

Not sure about previous versions, but in 7 you can just place your custom functions directly in template.php in your theme folder. just make sure not to overwrite any core functions.