Posted by wannianchuan on September 11, 2012 at 8:33am
最近在看代码的时候,遇到一个问题不大明白,向大家请教。
modules/block/block.module, line 254
<?php
function block_page_build(&$page) {
global $theme;
// The theme system might not yet be initialized. We need $theme.
drupal_theme_initialize();
// Fetch a list of regions for the current theme.
$all_regions = system_region_list($theme);
$item = menu_get_item();
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {
// Load all region content assigned via blocks.
foreach (array_keys($all_regions) as $region) {
// Assign blocks to region.
if ($blocks = block_get_blocks_by_region($region)) {
$page[$region] = $blocks;
}
}
// Once we've finished attaching all blocks to the page, clear the static
// cache to allow modules to alter the block list differently in different
// contexts. For example, any code that triggers hook_page_build() more
// than once in the same page request may need to alter the block list
// differently each time, so that only certain parts of the page are
// actually built. We do not clear the cache any earlier than this, though,
// because it is used each time block_get_blocks_by_region() gets called
// above.
drupal_static_reset('block_list');
}
else {
// Append region description if we are rendering the regions demo page.
$item = menu_get_item();
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
$visible_regions = array_keys(system_region_list($theme, REGIONS_VISIBLE));
foreach ($visible_regions as $region) {
$description = '<div class="block-region">' . $all_regions[$region] . '</div>';
$page[$region]['block_description'] = array(
'#markup' => $description,
'#weight' => 15,
);
}
$page['page_top']['backlink'] = array(
'#type' => 'link',
'#title' => t('Exit block region demonstration'),
'#href' => 'admin/structure/block' . (variable_get('theme_default', 'bartik') == $theme ? '' : '/list/' . $theme),
// Add the "overlay-restore" class to indicate this link should restore
// the context in which the region demonstration page was opened.
'#options' => array('attributes' => array('class' => array('block-demo-backlink', 'overlay-restore'))),
'#weight' => -10,
);
}
}
}
?>前面已经调用 menu_get_item() 给 $item 赋值了,并且判断了当前是不是在区块预览页了
// Fetch a list of regions for the current theme.
$all_regions = system_region_list($theme);
$item = menu_get_item();
if ($item['path'] != 'admin/structure/block/demo/' . $theme) {到了后面为什么有来了一遍呢
else {
// Append region description if we are rendering the regions demo page.
$item = menu_get_item();
if ($item['path'] == 'admin/structure/block/demo/' . $theme) {
Comments
很明显,多调用了一次,后面的这个是多余的 $item =
很明显,多调用了一次,后面的这个是多余的
$item = menu_get_item();
如果非要给出解释的话:
(1), menu_get_item(),这个函数使用了static缓存,对它的调用,不会有任何性能开销。所以多调用一次,也无所谓。
(2), "!="的对里面,不一定是“==” ,所以else里面又加了一个if判断,有了这个if判断,为了增加代码的可读性,多调用了一次 menu_get_item。
(3),'admin/structure/block/demo,这个页面,很少被访问到,只有开发的时候,才会用到。
我的drupal博客Think in Drupal
这样一解释就清楚一些了
我也觉得后面这个是多余的,测试了一下,去掉之后对页面的显示好像也没什么影响。
在第二条中您说:"!="的对里面,不一定是“==” ,是不是指$item['path']还有可能是不存在的呢?
代码这样写乍看不是很精练,感觉有些别扭,能否优化一下呢
万年船软件