php - Cannot load custom content type nodes with load_node_multiple or load_node -
i have custom content type called "program" trying load via drupal module.
the .module file includes class called program has method called
getallprograms() using include_once(drupal_get_path('module', 'progs') . '/progs.php');
when try , load nodes using either node_load() or node_load_multiple() 1 of 2 different errors randomly.
either:
fatal error: fatal error: call undefined function user_access() in /mypath/modules/filter/filter.module on line 1035
or
error: call undefined function token_get_entity_mapping() in /mypath//sites/all/modules/contrib/token/token.tokens.inc, line 767
note: 99% of times first error, , recieve token_get_entity error.
the strange thing is, while have been trying different things resolve error have been able both of these functions work period clear drupal cache error again.
what have tried
- disabling , enabling user module via database.
- checking paths , status correct main modules (system, user, block etc)
- using db_select list of node ids , use
node_load()(with loop) ,node_load_multiple()load nodes. 1 of things started working short time until cleared cache. - tested see if can call
user_access().module file. not work , returns same call undefined function error.
here code have (not progs anonymized name)
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 , objects $this->load_partial_inclues(); //retrieve programs 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; note: load $progs global space can call elsewhere in module.
program.php
if( !class_exists('program') ): class program { //a bunch of properties public static function getallopenprograms() { // line causes of issues. $result = node_load_multiple('',array('type' => 'program')); dpm($result); } thanks in advance!
like mike vranckx mentioned, if call progs() directly when include in progs.module, drupal hasn't bootstrapped, i.e. hasn't started running yet. suggest put progs() in progs_init() or similar drupal invoke @ right time.
here's proposed way follows initial structure quite closely, , below see alternative better follows drupal's conventions.
new progs.module
/** * implements hook_init(). */ function progs_init(){ progs(); } and modify progs.php
// why doing check? defining class elsewhere in project? if not can safely ignore //if( !class_exists('progs') ): // convention name classes pascal case btw. class progs { //a bunch of properties function __construct() { // load partial includes , objects $this->load_partial_inclues(); //retrieve programs 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; } a more drupal way:
progs.module
/** * implements hook_init(). */ function progs_init(){ global $progs; // consider using drupal_static cache if( !isset($progs) ) { module_load_include('inc', 'progs', 'progs'); $progs = new progs(); } } progs.inc (convention use .inc)
class progs { //a bunch of properties function __construct() { // load partial includes , objects $this->load_partial_inclues(); //retrieve programs open $this->open_programs = program::getallopenprograms(); } function load_partial_inclues() { //includes module_load_include('php', 'progs', 'core/objects/program'); } }
Comments
Post a Comment