【问题标题】:Drupal 8 custom block (module) create twig template fileDrupal 8 自定义块(模块)创建树枝模板文件
【发布时间】:2016-10-05 06:18:03
【问题描述】:

我有一个自定义模块,它创建一个具有字段元素的自定义块。

这一切都很好,但我需要为这个块设置主题。我已经检查了这里的其他帖子,但没有成功。

我已启用 Twig 调试并获得主题建议。还是没有运气。

谁能指点我正确的方向。

这是我目前所拥有的:

my_module/my_module.module

// nothing related in here

my_module/src/Plugin/Block/myModuleBlock.php

<?php

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'ModuleBlock' block.
 *
 * @Block(
 *  id = "module_block",
 *  admin_label = @Translation("My Module"),
 * )
 */
class ModuleBlock extends BlockBase {

  public function blockForm($form, FormStateInterface $form_state) {
    $form['test'] = array(
      '#type' => 'select',
      '#title' => $this->t('test'),
      '#description' => $this->t('test list'),
      '#options' => array(
        'Test' => $this->t('Test'), 
      ),
      '#default_value' => isset($this->configuration['test']) ? $this->configuration['test'] : 'Test',
      '#size' => 0,
      '#weight' => '10',
      '#required' => TRUE,
    );    
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['test'] = $form_state->getValue('test');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    $build['module_block_test']['#markup'] = '<p>' . $this->configuration['test'] . '</p>';
    return $build;
  }


}

my_module/templates/block--my-module.html.twig // 按照 twig debug 的建议

<h1>This is a test</h1>
<div id="test-widget">{{ content }}</div>

我还应该注意,在我的 my_theme.theme 我有这个,但我不认为它相关:

// Add content type suggestions.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($node = \Drupal::request()->attributes->get('node')) {
    array_splice($suggestions, 1, 0, 'page__node__' . $node->getType());
  }
}

至于我试过的是这样的:

public function build() {
    return array(
      '#theme' => 'block--my-module'
    );
}

但还是不行。

非常感谢这里的任何帮助。

更新:所以我刚刚开始工作,但我仍然需要帮助。我将模板 block--my-module.html.twig 移到了我的主题目录中,并且成功了。

如何让它在我的模块目录中工作?

【问题讨论】:

  • 文件是myModuleBlock.php,但类是ModuleBlock,没有my 是故意的吗?看起来不标准。

标签: php drupal-8


【解决方案1】:

更新:所以我刚刚开始工作,但我仍然需要帮助。我移动了 模板块--my-module.html.twig 到我的主题目录和它 工作。

如何让它在我的模块目录中工作?

您可以在模块根目录中创建一个名为templates/ 的目录。 将您的模板放在这里。

现在让 Drupal 知道您将模板存储在模块中。 在your_module.module添加这个函数:

function YOUR_MODULE_theme($existing, $type, $theme, $path) {
  return array(
    'block__my_module' => array(
      'render element' => 'elements',
      'template' => 'block--my-module',
      'base hook' => 'block'
    )
  );
}

这未经测试。这就是我的自定义块的工作方式。

不要忘记清除缓存。

【讨论】:

  • 感谢@Nicensin - 标记为已接受的答案,但请更正以下内容。 1) 将$pat 替换为$path 2) 从模板变量中删除.html.twig。这样其他人就可以从您的回答中受益。
  • 很高兴能为您提供帮助。感谢您的通知。我编辑了我的答案。
【解决方案2】:

为了能够在模块中添加 twig 文件,您需要确保模块定义了引用,而不是主题。

您仍然可以在模块的 .module 文件中实现 hook_theme(),如下所示:

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'mymodule_block'     => [
      'variables' => [
        // define defaults for any variables you want in the twig file
        'attributes' => [
           'class' => ['my-module-class'],
         ], //etc
      ],
    ],
  ];
}

然后在你的块的 build() 实现中,你可以添加对新主题函数的引用:

public function build() {
    // Load the configuration from the form
    $config = $this->getConfiguration();
    $test_value = isset($config['test']) ? $config['test'] : '';

    $build = [];
    $build['#theme'] = 'mymodule_block';

    // You would not do both of these things...
    $build['#test_value'] = $test_value;
    $build['module_block_test']['#markup'] = '<p>' . $test_value . '</p>';

    return $build;
}

最后要小心你放置你的树枝文件的位置和你的名字。在你的模块目录下创建templates目录,将主题函数名中的_替换为-mymodule-block.html.twig

【讨论】:

  • 非常感谢,这有点工作。如何在 _theme 函数中定义变量?
  • 主题函数允许​​你设置默认值(我从来没有给它 NULL 所以它可能没有传递给树枝文件,也就是说它可能是按设计“工作”和你在做什么)。在块的 build() 方法中设置渲染期间所需的变量。您还可能会发现 devel 的同名子模块中的 kint() 函数有助于检查模板中实际到达的内容。
  • 谢谢@acrosman - 变量应该来自通过块形式设置的数据库。还是我都搞错了?
  • 我更新了代码示例,更清楚地展示了从配置表单中获取值的几种方法。
  • 但是我在这部分要放什么:` // 为你想要在 twig 文件中的任何变量定义默认值`?请注意,当树枝文件位于我的主题文件夹中时,一切正常。它正在从表单中获取变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多