代码之家  ›  专栏  ›  技术社区  ›  Phill Pafford

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

  •  1
  • Phill Pafford  · 技术社区  · 14 年前

    所以我为Drupal6.x创建了一个自定义模块,它的工作方式就像我在page.tpl.php页面中看到的那样,但是当我从GUI编辑页面(它允许使用php标记)时,对象是不可访问的。

    我可以在会话中设置我可以从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调用它,这很好,那么为什么我不能从我在管理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;
    

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

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

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

    抱歉,这是我的第一个Drupal模块,所以任何洞察都会很好,因为我也是新使用Drupal的,叹息…

    2 回复  |  直到 14 年前
        1
  •  1
  •   Sid Kshatriya    14 年前

    你应该试着把这个片段

    // 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文件中包含任何业务逻辑都不是一个好主意,您在这里似乎拥有这些逻辑…]

        2
  •  0
  •   Phill Pafford    14 年前

    为了解决我的问题,我把所有的逻辑都转移到模块上,并且在我设置为会话的过程中,动态地改变我想要改变的字段。所以在page.tpl.php页面中,我检查了会话值是否设置,如果设置了会话值,则使用默认值。通过使用会话,我能够将所有所需的值传递到任何页面,而不管页面是在何处生成的(GUI或硬编码)。

    推荐文章