【问题标题】:Drupal 8 custom module - custom headerDrupal 8 自定义模块 - 自定义标头
【发布时间】:2019-10-10 20:34:44
【问题描述】:

我在 Drupal 8 中创建了一个自定义模块。我希望自定义模块显示自定义标题,而不是显示在所有其他页面上的标准标题。

我使用此处找到的文档创建了一个自定义树枝模板:https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-for-custom-module。我也以此为参考:https://www.drupal.org/forum/support/module-development-and-code-questions/2017-05-15/drupal-8-custom-module-template

我能够创建模板,但是该模板似乎在页眉模板之后和页脚模板之前被调用和呈现。因此,使用文档我无法更改头文件中的任何内容。

关于如何为自定义模块的标题创建自定义树枝文件的任何想法?下面是我的代码的 sn-ps:

模块文件:

function calendar_theme($existing, $type, $theme, $path) {
    return array(
        'calendar_display' => array(
            'variables' => array('test' => NULL),
            'template' => 'calendar-template',
        ),
    );
}

在我的控制器文件中:

namespace Drupal\calendar\Controller;


class CivilCController {

    public function calendar(){

        $content[] = [
            //stuff here
        ];

        $content[] = [
            //stuff here
        ];

        return [
            '#theme' => 'calendar_display',
            '#test' => $content,
        ];

    }

}

现在我有一个名为calendar-template.html.twig 的树枝文件。但是,此文件在标题下方呈现。

【问题讨论】:

  • 您必须澄清“标题”的含义。如果标题只是您主题中的一个区域并且在每个页面上都相同,我个人只会更改主题的模板文件之一,不需要模块。我可能会这样做的另一种方法是创建一个提供块的自定义模块,然后将该块放在标题区域中。
  • 你还没有说你是如何把你的主题代码放到页面上的。

标签: php drupal twig drupal-8 drupal-modules


【解决方案1】:

在您的主题中,您应该有一个名为“标题”的区域,并且您应该在其中放置为该区域提供内容的块。对于每个块,您可以(在块首选项中)定义它应该出现的页面和不应该出现的页面。

对于更复杂的用例,您可以使用Context 模块:

您不需要该功能的自定义模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2020-05-06
    • 2015-02-21
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多