块在区域之间移动时会更改其 ID?

发布于 2024-12-07 12:38:37 字数 993 浏览 0 评论 0原文

在 Drupal 6 下,我有一个自定义块,我已将其命名并放置到自定义的右侧边栏区域中。该块的 ID(我从 block.tpl.php$block_id 辅助变量中发现的)是 7。

我正在覆盖该块显示的输出一个视图,我需要更改标记;我在 template.php 中有一个名为 myTheme_preprocess_block() 的预处理函数,它会搜索该块的唯一 ID:

myTheme_preprocess_block(&$vars) {

    $this_block_id = $vars['id']; /* region-independent ID (for reliability) */
    $vars['template_files'] = array();

    switch ($this_block_id) {

        case 7:
            $vars['template_files'][] = 'block-my-override-template';
            break;
        default:
            /* take no action */
    }
}

现在,我已将该块从右侧边栏移动了区域(这是一个自定义区域,而不是 Garland 附带的默认区域)到页脚区域,该区域也有一个自定义名称。突然间,我的重写模板文件 block-my-override-template.tpl.php 不再被引用。

我做了一些挖掘,并从 block.tpl.php 中输出了唯一的块 ID,神奇的是,该块的 ID 从 7 更改 到了 13!板着脸,丝毫不逊色!将此块返回到右侧边栏区域也会将该块返回到 ID 7(并且我的所有代码再次开始工作)。

我的问题是:如果一个块的“唯一”ID 在从一个区域移动到另一个区域时发生变化,我们如何唯一地标识该块?

Under Drupal 6 I have a custom block, which I have named and placed into a custom, right-hand sidebar region. This block's ID (which I have discovered from block.tpl.php, from the $block_id helper variable) is 7.

I am overriding the output of this block as it displays a View and I need to change the markup; I have a preprocess function in template.php called myTheme_preprocess_block() which searches for the block's unique ID thus:

myTheme_preprocess_block(&$vars) {

    $this_block_id = $vars['id']; /* region-independent ID (for reliability) */
    $vars['template_files'] = array();

    switch ($this_block_id) {

        case 7:
            $vars['template_files'][] = 'block-my-override-template';
            break;
        default:
            /* take no action */
    }
}

Now, I've moved this block from the right-hand sidebar region (which is a custom region and not the default one which comes with Garland) to a footer region, which also has a custom name. And suddenly, my overriding template file, block-my-override-template.tpl.php, is no longer referenced.

I do a little digging and output the unique block ID from within block.tpl.php, and magically this block has changed its ID from 7 to 13! With a straight face, no less! Returning this block to the right-hand sidebar region also returns the block to ID 7 (and all my code starts working again).

My question is this: How can we uniquely identify a block if its "unique" ID changes when it moves from one region to another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-12-14 12:38:37

如果您使用的是视图,为什么不重写视图的块显示而不是修改实际的块呢?

您也可以简单地在模块中声明您的自定义块?这应该使您可以更轻松地管理块的主题方面。

If you are using a View, why don't you instead override the block display of the View instead of mucking around with the actual block?

You could alternatively simply declare your custom block in a module? That should make it easier for you to manage theming aspects of the block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文