tags:

views:

41

answers:

2

So I have created a custom module for Drupal 6.x and it works as I can see the desired results in the page.tpl.php page, but when I edit a page from the GUI (it allows php tags) the object is not accessible.

I can set the values in a SESSION which I can access from the GUI as well as the module but is this the correct way to do this?

Here is the error I get:

Fatal error: Call to a member function getEmail() on a non-object in /var/www/domain/includes/common.inc(1695) : eval()'d code on line 221

Call Stack
#   Time    Memory  Function    Location
1   0.0003  64108   {main}( )   ../index.php:0
2   0.0965  11659504    menu_execute_active_handler( )  ../index.php:18
3   0.1040  12626908    call_user_func_array ( )    ../menu.inc:348
4   0.1040  12627316    node_page_view( )   ../menu.inc:0
5   0.1040  12627532    node_show( )    ../node.module:1797
6   0.1040  12627848    node_view( )    ../node.module:1101
7   0.1040  12628192    node_build_content( )   ../node.module:1006
8   0.1041  12648832    node_prepare( ) ../node.module:1085
9   0.1041  12649112    check_markup( ) ../node.module:1041
10  0.1047  12671980    module_invoke( )    ../filter.module:457
11  0.1047  12693240    call_user_func_array ( )    ../module.inc:462
12  0.1047  12693900    php_filter( )   ../module.inc:0
13  0.1048  12694164    drupal_eval( )  ../php.module:82
14  0.1059  12883728    eval( ''?>

getEmail() is a function in a class that is in my custom module. I can call it from the page.tpl.php just fine, so why can't I call it from a page that I have edited in the Admin GUI?

EDIT:

Adding Code from Module:

//wrapperFunction() is calling the class and setting the values
// this is just a getter/setter class w/ 1 function that formats a phone number, nothing special
$custom = new CustomObj(); 
$custom->setEmail('blah@blah,com');

return $custom;

page.tpl.php

// calls the wrapper function and returns the object
$custom_obj = wrapperFunction();
echo $custom_obj->getEmail(); // this prints the email just fine

Edit page through the Admin GUI (Allows PHP tags) Adding this code to the page

<?php echo $custom_obj->getEmail(); ?> // throws the error

Sorry this is my first Drupal Module so any insight would be great as I'm also new to using Drupal, sigh...

+1  A: 

You should try putting the snippet

// calls the wrapper function and returns the object
$custom_obj = wrapperFunction();
echo $custom_obj->getEmail(); // this prints the email just fine

in node.tpl.php instead of page.tpl.php. node.tpl.php is executed before page.tpl.php so your error arises as $custom_obj does not exist as it is only created in page.tpl.php (by calling the wrapperFunction() which does new).

[I don't know what you are trying to achieve exactly. Its generally not a good idea to have any business logic in your tpl files which you seem to have here...]

Sid NoParrots
@phill: did it work?
Sid NoParrots
will be testing this today, but can you tell me where to add business logic the needs to be on every page? would it be better to use node.tpl.php instead of page.tpl.php? New at creating modules as well as Drupal so any help in the right direction
Phill Pafford
Also I'm using my own theme, just FYI if that changes you answer
Phill Pafford
I can't seem to find the node.tpl.php file, where would this be located or do I need to create this file?
Phill Pafford
So not finding node.tpl.php I moved all the logic code into my module, but still getting the same error when calling the objects from the GUI code. Is SESSION the only way to send/set these values?
Phill Pafford
Since you're creating your own theme you should copy node.tpl.php from YOUR_DRUPAL_ROOT/modules/node/ to your theme directory. After copying it you can modify it. Be sure to clear the theme registry after copying it so that its picked up by the theme system.
Sid NoParrots
BTW its really a bad idea to create your own theme in this day and age. You should use a nice base theme like fusion ( http://drupal.org/project/fusion ).
Sid NoParrots
I don't think that really make much sense. Don't use your own theme but choose another? What is the logic behind this?
Phill Pafford
It was a side comment -- I thought you had hand rolled your own theme from scratch. Pardon me if I misunderstood.
Sid NoParrots
Thanks for your help +1 for your efforts
Phill Pafford
A: 

Well to solve my problem I moved all the logic to the module and the fields I wanted to change on the fly I set to session. so in the page.tpl.php page I checked if the SESSION value was set, if it was use it else use a default value. By using SESSION I was able to pass all the desired values to any page regardless of where the page was made (GUI or Hard coded).

Phill Pafford