问题描述
我有一个自定义模块,该模块创建一个具有字段元素的自定义块。
I have a custom Module that creates a custom Block which has field elements.
这一切都很好,但是我需要为此块设置主题。我检查了这里的其他帖子,并没有运气尝试。
This all works fine but I need to theme this block. I have checked the other posts on here and tried with no luck.
我启用了树枝调试并获得了主题建议。仍然没有运气。
I have enabled twig debug and got theme suggestions. Still no luck.
有人可以指出正确的方向吗?
Can anyone please point me in the right direction.
远:
my_module / my_module.module
my_module/my_module.module
//这里没有任何相关内容
my_module / src / Plugin / Block / myModuleBlock.php
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建议
my_module/templates/block--my-module.html.twig // as suggested by twig debug
<h1>This is a test</h1>
<div id="test-widget">{{ content }}</div>
我还应该注意,在my_theme.theme中,我有这个但我不认为这与它的相关性:
I should also note that in my my_theme.theme I have this but I don;t think its relevant:
// 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());
}
}
至于我尝试过的是这样的: / p>
As for what I've tried is this:
public function build() {
return array(
'#theme' => 'block--my-module'
);
}
但还是没有。
这里的任何帮助都非常感激。
Any help here is very much appreciated.
更新:所以我可以使用它,但是我仍然需要帮忙。我将模板 block--my-module.html.twig
移到了主题目录,它开始工作。
UPDATE: So I just got it to work but I still need help. I moved the template block--my-module.html.twig
to my theme directory and it worked.
如何在我的模块目录中使用它?
How do I get it to work in my module directory?
推荐答案
如何获取它可以在我的模块目录中工作?
How do I get it to work in my module directory?
您可以创建一个名为 templates /
在模块根目录中。
将模板放在这里。
You can create a directory called templates/
in your modules root.Place your template here.
现在,让Drupal知道您将模板存储在模块中。
在 your_module.module
中添加此功能:
Now let Drupal know you store the template in your module.in your_module.module
add this function:
function YOUR_MODULE_theme($existing, $type, $theme, $path) {
return array(
'block__my_module' => array(
'render element' => 'elements',
'template' => 'block--my-module',
'base hook' => 'block'
)
);
}
未经测试。这是我的自定义块的工作方式。
This is not tested. It´s the way it worked for my custom block.
别忘了清除缓存。
这篇关于Drupal 8自定义块(模块)创建树枝模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!