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

Cannot load custom content type nodes with load_node_multiple or load_node

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

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

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

任一:

Fatal error: Fatal error: Call to undefined function user_access() in /mypath/modules/filter/filter.module on line 1035

Error: Call to undefined function token_get_entity_mapping() in /mypath//sites/all/modules/contrib/token/token.tokens.inc, line 767

注意:99% 的时间是第一个错误,偶尔我会收到 token_get_entity 错误。

奇怪的是,虽然我一直在尝试不同的方法来解决错误,但我已经能够让这两个函数工作一段时间,但是一旦我清除 Drupal 缓存,我就会再次遇到错误.

我试过的

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

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 加载到全局 space 中,这样我就可以在模块的其他地方调用它。

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);

        }

提前致谢!

就像 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');
        }

    }