[英]Drupal 8 use suggested template when programmatically rendering node
I'm rendering a node programmatically from within a Block class in a Drupal 8 module and I would like to be able to use one of the suggested template names I get in the generated DOM to override how the node is rendered. 我正在Drupal 8模块的Block类中以编程方式渲染节点 ,我希望能够使用在生成的DOM中获得的建议模板名称之一来覆盖节点的渲染方式。
Unfortunately, any suggested name I use to create the template in my templates folder is ignored and Drupal renders the basic bartik node.html.twig template . 不幸的是,我用来在模板文件夹中创建模板的任何建议名称都会被忽略 ,Drupal会呈现基本的bartik node.html.twig template 。
Here are the relevant parts of the code : 以下是代码的相关部分:
<?php
/**
* @Block
*/
/**
* {@inheritdoc}
*/
public function build()
{
$events = [];
$eventNodes = $this->repository->findBy(['status' => 1], [ 'field_date' => 'ASC' ]);
$viewBuilder = $this->entityManager->getViewBuilder('node');
foreach ($eventNodes as $eventNode) {
$event = EventMapper::fromNodeToEvent($eventNode, [
'title' => 'field_titre',
'start' => 'field_date',
'end' => 'field_fin'
]);
$build = $viewBuilder->view($eventNode, 'full');
$event['html'] = $this->renderer->renderRoot($build);
$events[] = $event;
}
$months = [];
if (count($events) > 0) {
$indexer = new EventIndexer($events);
$months = $indexer
->indexByMonths()
->getResults()
;
}
return [
'#theme' => 'calendar_block',
'#events' => $events,
'#attached' => [
'library' => ['calendar/calendar'],
'drupalSettings' => [ 'events' => $events, 'monthsIndex' => $months ]
]
];
}
/**
* Custom finder in a repository service. get list of nids and load
* them.
*/
/**
* @param array $criteria
* @param array $orderBy
* @param int $limit
* @param int $offset
*
* @return array<Node>
*/
public function findBy(array $criteria = [], array $orderBy = [], $limit = null, $offset = 0)
{
$query = $this->queryFactory->get('node');
$query->condition('type', self::TYPE);
foreach ($criteria as $field => $value) {
$query->condition($field, $value);
}
if (!empty($orderBy)) {
foreach ($orderBy as $field => $direction) {
$query->sort($field, $direction);
}
}
if (!is_null($limit)) {
$query->range($offset, $limit);
}
$nids = $query->execute();
$events = $this->etm->getStorage('node')->loadMultiple($nids);
return $events;
}
/**
* Twig fragment where the rendered event is printed.
*
*/
{% for event in events %}
<div class="js-calendar-slider-event calendar-slider-event" data-start="{{events[loop.index0].start|date('Y-m-d') }}" data-end="{{events[loop.index0].end|date('Y-m-d') }}">{{ event.html }}</div>
{% endfor %}
You have to create a theme suggestions hook by hook_theme() 您必须通过hook_theme()创建主题建议钩子
like i run a test case for you, i have write a hook_theme() 就像我为您运行一个测试用例一样,我写了一个hook_theme()
in my module file here is code 在我的模块文件中,代码是
/**
* Implements hook_theme().
*/
function my_module_theme($existing, $type, $theme, $path) {
$theme = array();
$theme['node__contentTypeName'] = array(
'render element' => 'content',
'base hook' => 'node',
'template' => 'node--contentTypeName',
'path' => drupal_get_path('module', 'my_module') . '/templates',
);
return $theme;
}
then place your template twig file in my_module/templates folder 然后将模板树枝文件放置在my_module / templates文件夹中
here is my block code 这是我的代码
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'example node render block' Block
*
* @Block(
* id = "example_node_render_pulgin_block",
* admin_label = @Translation("example render node"),
* )
*/
class ExampleNodeRenderBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$nid = 36;
$entity_type = 'node';
$view_mode = 'full';
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$node = $storage->load($nid);
$build = $view_builder->view($node, $view_mode);
$output = render($build);
return array(
'#type' => 'markup',
'#markup' => $output,
);
}
}
i have add some extra code in twig file to make sure my template is rendering or not 我在树枝文件中添加了一些额外的代码,以确保我的模板是否渲染
this is the line i have add in my template.twig 这是我在我的template.twig中添加的行
<div><h1>hello its me</h1></div>
and finally i place my block some where and here is result 最后,我将块放置在结果所在的位置
Hope this solve your problem 希望这能解决您的问题
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.