我有一个自定义模块,可以创建一个包含字段元素的自定义块。
这一切都很好,但我需要主题这个块。我已经检查过这里的其他帖子并试着没有运气。
我启用了twig调试并获得了主题建议。仍然没有运气。
任何人都可以指出我正确的方向。
这是我到目前为止所做的:
my_module / my_module.module
// nothing related in here
my_module / SRC /插件/砌块/ 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
移动到我的主题目录,然后就可以了。
如何在模块目录中使用它?
答案 0 :(得分:10)
更新:所以我得到了它的工作,但我仍然需要帮助。我感动了 模板块 - 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'
)
);
}
未经测试。这是我的自定义块的工作方式。
别忘了清除缓存。
答案 1 :(得分:6)
为了能够在模块中添加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