0

这就是问题所在。

我有一个名为“用户收藏块”
的块,它位于“第一个侧边栏”
“第一个侧边栏”出现在首页上的区域。
该函数应该从 f25_favorites 表中获取数据并将它们列在块中。现在该表是一个空数组。
当 Ireturn $output时,我的 div 或任何东西都不会输出。
当我这样做时print($output),一切都会显示出来。

这是我的测试代码,表明我的“if”语句返回 true。http://d.pr/zDph

/*
 * f25_favorites_my_favorites theme
 */
function theme_f25_favorites_my_favorites($mypaths) {
  dsm($mypaths);
  print_r(count($mypaths));
  $output .= 'n<div id="f25-favorites">n';
    $output .= '<div id="f25-favorites-list">n';
      if (count($mypaths) == 0) {
        $output .= "No favorites have been added";
        print "No favorites have been added";
      }
      else {
        foreach ($mypaths as $indpath) {
          $output .= l($indpath->title, $indpath->path, $attributes = array());
        }
      }
    $output .= '</div>n';
    $output .= '<div id="f25-favorites-add">n';
    $output .= '</div>n';
  $output .= 'n</div>n';
  return $output;
}

这会输出: http: //d.pr/Uhrs
中频测试

请注意左上角的 0,这是 'count()'
的输出以及 'if' 中文本的打印

所以,这是我的主题:

/*
 * f25_favorites_my_favorites theme
 */
function theme_f25_favorites_my_favorites($mypaths) {
  /*dsm($mypaths);
  print_r(count($mypaths));*/
  $output .= '\n<div id="f25-favorites">\n';
    $output .= '<div id="f25-favorites-list">\n';
      if (count($mypaths) == 0) {
        $output .= "No favorites have been added";
      }
      else {
        foreach ($mypaths as $indpath) {
          $output .= l($indpath->title, $indpath->path, $attributes = array());
        }
      }
    $output .= '</div>\n';
    $output .= '<div id="f25-favorites-add">\n';
    $output .= '</div>\n';
  $output .= '\n</div>\n';
  return $output;
}


它是用这个 hook_theme() 函数调用的:

/*
 * Implentation of hook_theme().
 */
function f25_favorites_theme () {
  return array(
    'f25_favorites_my_favorites' => array (
    'arguments' => array ('mypaths' => array())
   ),
  );
}


用这个 hook_block() 函数调用它:

/*
 * Implementation of hook_block().
 *
 */
function f25_favorites_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks = array();
    $blocks['f25-favorites'] = array(
      'info' => t('User Favorites Block'),
      'cache' => BLOCK_NO_CACHE,
    );
    return $blocks;
  }

  if ($op == 'view') {
    switch ($delta) {
      case 0:
        $mypaths = f25_favorites_user_favorites();
        $block = array(
          'subject' => t('User Favorites Block'),
          'content' => theme_f25_favorites_my_favorites($mypaths)
        );
      return $block;
    };
  }
}

值得注意的

我的主题是一个名为“Zen”的主题的“子主题”
Zen 有一个 block.tpl.php,它看起来像这样:http ://d.pr/AaO1

这是我的模块的完整代码:http: //d.pr/cGqc

4

1 回答 1

1

这可能是与地区有关的问题。尝试切换到 Garland 并将块添加到一个普通的旧 Garland 区域,看看它是否出现。

如果您在 Garland 中看到它,请确保您的子主题确实定义了“第一个侧边栏”区域,然后在 tpl 文件中实际打印其变量。

(FWIW 我在 Garland 上试过你的代码,它显示的块很好。)

此外,您可能希望从以下位置更改函数调用:

theme_f25_favorites_my_favorites($mypaths)

至:

theme('f25_favorites_my_favorites', $mypaths)

...如果您想保持代码的灵活性(即让 Drupal 调用任何预处理函数并允许其他人或您将来覆盖模板的输出)

于 2011-08-24T04:50:19.527 回答