【问题标题】:How to organize Drupal 8 Block templates in module?如何在模块中组织 Drupal 8 Block 模板?
【发布时间】:2016-10-14 15:28:29
【问题描述】:

我想设置一个 Drupal 8 模块,它允许我为每个块使用不同的树枝模板定义不同的块类型(有些相关)。

目前,我的文件夹结构如下所示:

modules
  custom
    templater
      src
        Plugin
          Block
            TemplaterBaseBlock.php
            TemplaterTwoColumnBlock.php
            TemplaterThreeColumnBlock.php
      templates
        block--templater-two-column.html.twig
        block--templater-three-column.html.twig

看看我的 TwoColumn 类:

<?php
/**
 * @file
 * Contains \Drupal\templater\Plugin\Block\TemplaterBlock.
 */

namespace Drupal\templater\Plugin\Block;

/**
 * Provides a 'Templater Two Column' Block
 *
 * @Block(
 *   id = "templater_two_column",
 *   admin_label = @Translation("Templater Two Column"),
 * )
 */
class TemplaterTwoColumnBlock extends TemplaterBaseBlock  {

    /**
     * {@inheritdoc}
     */
    public function build() {
        return [
            '#title' => $this->t('test'),
        ];

    }

}

而我的block--templater-two-column.html.twig 只是有{{ title }}

我的第一个问题 模板文件实际上在我上面指定的位置不起作用。实际上,我必须将它们移至我的主题才能正常工作。我真的很想将模板文件保留在模块本身中。有谁知道我需要做什么才能启用它?

我的第二个问题 我知道当我第一次开始时,我得到了{{ title }} 的渲染出现在页面上。但是,{{ title }} 似乎不再呈现任何内容。不知道我做了什么改变才能做到这一点。

最后,看看我的templater.module 文件:

<?php
/**
 * @file
 * Code for the example module.
 */

/**
 * Theme hook
 */
function templater_theme($existing, $type, $theme, $path) {
    return [
        'templater' => [
            'variables' => [
                'title' => null,
            ],
        ],
    ];
}

function templater_preprocess_page(&$variables) {
    $variables['#attached']['library'][] = 'templater/global-styling';
}

【问题讨论】:

  • 我能够弄清楚所有事情。

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


【解决方案1】:

我想通了:

我的templater.module 文件。

/**
 * Theme hook
 */
function templater_theme($existing, $type, $theme, $path) {
    return [
        'templater_two_column' => [
            'variables' => [
                'text' => null,
            ],
        ],
        'templater_three_column' => [
            'variables' => [
                'text' => null,
            ],
        ],
    ];
}

function templater_preprocess_page(&$variables) {
    $variables['#attached']['library'][] = 'templater/global-styling';
}

我的TemplaterTwoColumnBlock.php 文件:

<?php
/**
 * @file
 * Contains \Drupal\templater\Plugin\Block\TemplaterBlock.
 */

namespace Drupal\templater\Plugin\Block;

/**
 * Provides a 'Templater Two Column' Block
 *
 * @Block(
 *   id = "templater_two_column",
 *   admin_label = @Translation("Templater Two Column"),
 * )
 */
class TemplaterTwoColumnBlock extends TemplaterBaseBlock {

    /**
     * {@inheritdoc}
     */
    public function build() {
        return [
            '#theme' => 'templater_two_column',
            '#text' => $this->t('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'),
        ];

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    相关资源
    最近更新 更多