简体   繁体   English

如何在drupal 8中打印节点对象

[英]how to print node object in drupal 8

in drupal 7 we could use kpr() from devel module or php print_r() and var_dump() functions to print $node object in node.tpl file or hook preprocess node in purpose of debugging and finding a field or ..在 drupal 7 中,我们可以使用 devel 模块中的kpr()或 php print_r()var_dump()函数在 node.tpl 文件中打印 $node 对象或钩子预处理节点,以便调试和查找字段或..

but in drupal 8 we can't use php functions in twig and I tried kint() and dump() to print node in twig but no success.但是在drupal 8中,我们不能在kint()使用php函数,我尝试使用kint()dump()kint()中打印节点但没有成功。 ( I've already set debug: true in services.yml ) ( more detail: kint(node) in twig file makes an infinite loop and causes memory size exhausted in browser). (我已经在 services.yml 中设置了debug: true )(更多细节: kint(node)文件中的kint(node)造成无限循环并导致浏览器中的内存大小耗尽)。

so the question is how to print node object in drupal 8 using twig or hook preprocess node?所以问题是如何使用twig或hook预处理节点在drupal 8中打印节点对象?

Q update v 1.0: Q 更新 v 1.0:

I have custom twig template for article: node--article.html.twig and it works fine:我有文章的自定义树枝模板:node--article.html.twig,它工作正常:

<article>

<div>
    {{ content.body|render }} {# this works #}
</div>

<footer>
    {{ kint(node.field_custom.value) }} {# prints the custom field value without any problem #}
    {{ kint(node) }} {# this causes infinite loop and memory issue #}
    {{ content }} {# prints all content fields without any problem #}
    {{ kint(content) }} {# nothing happen or display with this! #}
    {{ kint(label) }} {# infinite like node #}
</footer>

I knew that the kint() function is not the problem because it displays custom arrays and objects that I created for test.我知道kint()函数不是问题,因为它显示了我为测试而创建的自定义数组和对象。 so problem was the node object itself.所以问题是节点对象本身。 it was very big and printing it using kint() made memory limit issues.它非常大,使用kint()打印会导致内存限制问题。 when I changed memory limit to -1 memory_limit= -1 in php.ini file for testing , it took all 16GB ram of my system and it wasn't enough!当我在php.ini文件中将内存限制更改为 -1 memory_limit= -1进行测试时,它占用了我系统的所有 16GB 内存,这还不够!

so I decreased the depth of kint() function from 7 to 4 in modules/devel/kint/kint/config.default.php ( $_kintSettings['maxLevels'] = 4; ) and memory_limit=128M in php.ini .所以我在modules/devel/kint/kint/config.default.php ( $_kintSettings['maxLevels'] = 4; ) 和memory_limit=128Mphp.ini kint()函数的深度从7减少到4。

now every thing works, hope it help someone.现在一切正常,希望对某人有所帮助。

Another option is to use the VarDumper module which isn't quite so memory intensive as Kint.另一种选择是使用VarDumper 模块,它不像 Kint 那样占用大量内存。 I find it a bit more user-friendly and better-looking!我觉得它更用户友好和更好看!

function YOURTHEME_preprocess_node(&$variables){
  vardumper($variables);
}

Drupal VarDumper 示例

1- install Devel + Twig VarDumper 1- 安装 Devel + Twig VarDumper

2- in www.example.com/admin/config/development/devel 2- 在www.example.com/admin/config/development/devel

enable Display $page array启用显示 $page 数组

3- in www.example.com/admin/config/development/devel 3- 在www.example.com/admin/config/development/devel

enable Symfony var-dumper启用Symfony var-dumper

4- in twig add 4-在树枝中添加

{{ dump() }} {# all #}
{{ dump(attributes) }} {# one #}
{{ dump(_charset) }} {# #}

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

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