简体   繁体   English

如何使用来自另一个自定义块树枝模板的变量呈现自定义块

[英]How to render a custom block with variables from another custom block twig template

I have a custom block with some values I can access in twig template of that block.我有一个自定义块,其中包含一些可以在该块的树枝模板中访问的值。 Let's say variable product_type and in same twig file I'm adding another view block like假设变量product_type并在同一个树枝文件中添加另一个视图块,例如

{{ drupal_block('views_block:my-view-block_1', {product_type: 'clothes'}) }}

and for that block's hook和那个街区的钩子

function myModule_views_pre_view(&$view, &$display_id, &$args){
// I want that "product_type" variable here in this hook
// because I want to add a filter here like this:
$view->display_handler->display['display_options']['filters']['field_product_type_target_id']['default_value'] = $product_type;
}

Above hook is being invoked but I cannot get product_type in hook function.上面的钩子正在被调用,但我无法在钩子函数中获取product_type I'm not sure if that is the correct way to do it but I really need to pass that value from first block to second block and use that value in second block to filter out results.我不确定这是否是正确的方法,但我确实需要将该值从第一个块传递到第二个块,并在第二个块中使用该值来过滤掉结果。 It's been 2 days I'm trying to figure this out.已经2天了,我试图弄清楚这一点。 Any help would be highly appreciated :)任何帮助将不胜感激:)

Thanks & Regards.感谢和问候。

The second parameter you pass to drupal_block() is block configuration, so you won't be able to get it in the hook_views_pre_view() .您传递给drupal_block()的第二个参数是块配置,因此您将无法在hook_views_pre_view()中获取它。

Use drupal_view() instead, you will get product_type in $args :改用drupal_view() ,您将在$args中获得product_type

{{ drupal_view(<view_id>, <display_id>, {product_type: 'clothes'}) }}

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

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