php – Drupal 8自定义块(模块)创建twig模板文件
我有一个自定义模块,可以创建一个具有字段元素的自定义块.
这一切都很好,但我需要主题这个块.我已经检查过这里的其他帖子,并试着没有运气. 我启用了twig调试并获得了主题建议.仍然没有运气. 任何人都可以指出我正确的方向. 这是我到目前为止: my_module / my_module.module //这里没什么关系 my_module / SRC /插件/块/ myModuleBlock.php <?php namespace Drupalmy_modulePluginBlock; use DrupalCoreBlockBlockBase; use DrupalCoreFormFormStateInterface; /** * 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,'page__node__' . $node->getType()); } } 至于我试过的是这个: public function build() { return array( '#theme' => 'block--my-module' ); } 但仍然没有去. 这里的任何帮助非常感谢. 更新:所以我只是让它工作,但我仍然需要帮助.我将模板块 – my-module.html.twig移动到我的主题目录并且它工作正常. 如何让它在我的模块目录中工作?
您可以在模块根目录中创建名为templates /的目录. 现在让Drupal知道您将模板存储在模块中. function YOUR_MODULE_theme($existing,$type,$theme,$path) { return array( 'block__my_module' => array( 'render element' => 'elements','template' => 'block--my-module','base hook' => 'block' ) ); } 这没有经过测试.这是我的自定义块的工作方式. 别忘了清除缓存. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |