繁体   English   中英

Drupal 8自定义块(模块)创建twig模板文件

[英]Drupal 8 custom block (module) create twig template file

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

这一切都很好,但我需要主题这个块。 我已经检查过这里的其他帖子,并试着没有运气。

我已启用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到我的主题目录并且它有效。

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

更新:所以我只是让它工作,但我仍然需要帮助。 我将模板块 - 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'
    )
  );
}

这没有经过测试。 这是我的自定义块的工作方式。

别忘了清除缓存。

为了能够在模块中添加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;
}

最后要小心放置twig文件的位置以及你的名字。 在模块目录中创建一个templates目录,并用-mymodule-block.html.twig替换主题函数名中的_

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM