문제

I have a loader function which is called excessively whenever the user accesses the page. Naturally, I only need it to run once per page request, instead, it is called multiple times. Earlier the _load() ran only twice with MENU_CALLBACK, then I tested it with the default MENU_NORMAL_ITEM and it increased to four. Although I've changed it back, and cleared my cache, it still runs four times now, and I only want it to run once.

My question is, why is my loader function being called multiple times? Any insight would be greatly appreciated.

$items['daycare/%isValid/home'] = array(
    'title' => 'Daycare Admin Home',
    'page callback' => 'daycares_home_page',
    'load arguments' => array(0),//Passes 1st part of url 'daycare' as 2nd arg
    'page arguments' => array(0),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
);

function isValid_load($pageID, $pageType){
    /*Do stuff to validate wildcard part of URL. Will throw 404/403 if invalid*/
    return $pageId;
}
도움이 되었습니까?

해결책

You can use devel's ddebug_backtrace() function to see how/what is calling your load function.

function isValid_load($pageID, $pageType){
    ddebug_backtrace();
    ...
}

When you do, notice that the two calls can be traced back to the last two lines in index.php. The first bootstraps Drupal and initializes the theme while the second call actually does the work of rendering the page. Though both actions seem to call the same _menu_translate() which results in a call to your function.

As a side note, if you're concerned about performance, you might be interested in this Lullabot article about caching.

I realize that this is a partial answer as I'm still getting familiar with the deepest workings of Drupal myself. I welcome competing answers that can provide a clearer and more complete explanation of why the Drupal framework is structured in such a way that the _load function is called multiple times.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top