无法使用load_node_multiple或load_node加载自定义内容类型节点

时间:2015-03-31 20:26:00

标签: php drupal drupal-7

我有一个名为" program"的自定义内容类型。我试图通过drupal模块加载。

.module文件包含一个名为Program的类,它有一个名为
的方法 getAllPrograms()使用include_once(drupal_get_path('module', 'progs') . '/progs.php');

当我尝试使用node_load()node_load_multiple()加载节点时,我会随机获得两个不同的错误之一。

或者:

  

致命错误:致命错误:在第1035行的/mypath/modules/filter/filter.module中调用未定义的函数user_access()

  

错误:在/mypath //sites/all/modules/contrib/token/token.tokens.inc,第767行调用未定义的函数token_get_entity_mapping()

注意:99%的次数是第一次出错,偶尔我会收到token_get_entity错误。

奇怪的是,虽然我一直在尝试不同的方法来解决错误,但我已经能够让这两个函数都工作一段时间但是一旦我清除了Drupal Cache,我就会再次收到错误。< / p>

我尝试了什么

  • 通过数据库禁用和启用用户模块。
  • 检查主要模块(系统,用户,块等)的路径和状态是否正确
  • 使用db_select获取节点ID列表,然后使用node_load()(带循环)和node_load_multiple()加载节点。这是在我清除缓存之前开始工作很短时间的事情之一。
  • 经过测试,看看我是否可以从.module文件中调用user_access()。这不起作用,并返回对未定义函数错误的相同调用。

这是我的代码(不是匿名名称)

progs.module

include_once(drupal_get_path('module', 'progs') . '/progs.php');

progs.php

if( !class_exists('progs') ):

    class progs 
    {
        //a bunch of properties

        function __construct()
        {
            // load partial includes and objects
            $this->load_partial_inclues();

            //retrieve all programs that are open
            $this->open_programs = Program::getAllOpenPrograms();
        }

        function load_partial_inclues()
        {
            //includes
            include_once(drupal_get_path('module', 'progs') . '/core/objects/program.php');
        }

    }

function progs()
{
    global $progs;

    if( !isset($progs) )
    {
        $progs = new progs();
    }

    return $progs;

}

// initialize
progs();

endif; 

注意:我将$ progs加载到全局空间中,以便我可以在我的模块中的其他位置调用它。

program.php

if( !class_exists('Program') ):

    class Program 
    {
        //a bunch of properties

        public static function getAllOpenPrograms() 
        {
             // This is the line that causes all of the issues. 
             $result = node_load_multiple('',array('type' => 'program')); 
             dpm($result);

        }

提前致谢!

1 个答案:

答案 0 :(得分:0)

就像Mike Vranckx提到的那样,如果你将它包含在progs.module中直接调用progs(),Drupal基本上没有引导,即尚未完全运行。建议您将progs()放在progs_init()或类似内容中,以便Drupal在合适的时间调用它。

这是一种非常接近你的初始结构的建议方法,下面你将看到一个更好地遵循Drupal惯例的替代方案。

新的progs.module

  /**
   * Implements hook_init().
   */
  function progs_init(){
    progs();
  }

并修改您的progs.php

    // Why are you doing this check? Are you defining this class elsewhere in your project? If not you can safely ignore this
    //if( !class_exists('progs') ):

    // Convention is to name classes with Pascal case btw.
    class progs 
    {
        //a bunch of properties

        function __construct()
        {
            // load partial includes and objects
            $this->load_partial_inclues();

            //retrieve all programs that are open
            $this->open_programs = Program::getAllOpenPrograms();
        }

        function load_partial_inclues()
        {
            //includes
            include_once(drupal_get_path('module', 'progs') . '/core/objects/program.php');
        }

    }

    function progs()
    {
        global $progs;

        if( !isset($progs) )
        {
            $progs = new progs();
        }

        return $progs;

    }

更多Drupal方式:

<强> progs.module

    /**
    * Implements hook_init().
    */
    function progs_init(){
        global $progs;

        // Consider using drupal_static to cache this
        if( !isset($progs) )
        {
            module_load_include('inc', 'progs', 'progs');
            $progs = new Progs();
        }
    }

progs.inc(惯例是使用.inc)

    class Progs 
    {
        //a bunch of properties

        function __construct()
        {
            // load partial includes and objects
            $this->load_partial_inclues();

            //retrieve all programs that are open
            $this->open_programs = Program::getAllOpenPrograms();
        }

        function load_partial_inclues()
        {
            //includes
            module_load_include('php', 'progs', 'core/objects/program');
        }

    }
相关问题