无法从GUI PHP标记访问Drupal自定义模块对象

时间:2010-10-15 19:26:59

标签: php drupal scope

所以我为Drupal 6.x创建了一个自定义模块,它可以在page.tpl.php页面中看到所需的结果,但是当我从GUI编辑页面时(它允许使用php标签)对象无法访问。

我可以在SESSION中设置我可以从GUI和模块访问的值,但这是正确的方法吗?

这是我得到的错误:

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()是我的自定义模块中的类中的函数。我可以从page.tpl.php中调用它,那么为什么我不能从我在Admin GUI中编辑过的页面调用它呢?

编辑:

从模块添加代码:

//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

通过Admin GUI编辑页面(允许PHP标记) 将此代码添加到页面

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

对不起,这是我的第一个Drupal模块,所以任何见解都会很棒,因为我也是使用Drupal的新手,叹息......

2 个答案:

答案 0 :(得分:1)

您应该尝试将代码段放入

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

node.tpl.php而不是page.tpl.php。 node.tpl.php在 page.tpl.php之前执行,所以你的错误就出现了,因为$ custom_obj不存在,因为它只在page.tpl.php中创建(通过调用wrapperFunction()来实现new)。

[我不知道你想要达到的目标。在你的tpl文件中有任何业务逻辑通常不是一个好主意,你似乎在这里......]

答案 1 :(得分:0)

好了解决我的问题我将所有逻辑移动到模块和我想要动态更改的字段我设置为session。所以在page.tpl.php页面中,我检查了是否设置了SESSION值,如果是使用它,则使用默认值。通过使用SESSION,无论页面在何处(GUI或硬编码),我都可以将所有需要的值传递给任何页面。