2

Not sure if this is a bug, but I wanted to add some debug into an HTML comment in my page.html.twig file. I added <!-- type={{node.type}} --> but I get:

Uncaught PHP Exception Exception: "Object of type Drupal\Core\Field\EntityReferenceFieldItemList cannot be printed." at /www/cn8/core/lib/Drupal/Core/Template/TwigExtension.php line 443

It took me a while to track down since I was sure this would work. Shouldn't EntityReferenceFieldItemList have a toString() function for this purpose?

1
  • I just worked out that I can write node.type.target_id and get the content type as a string, but still don't understand why node.type doesn't output something, but crashes... – Nic Sep 15 '16 at 8:23
3

node.type is a reference to the node type entity. You can get the id of the content type with target_id, as you have mentioned in the comment. For debugging this might be OK, but most times you want to get the label, which is nicely formatted and translatable:

{{ node.type.entity.label }}
3
  • Thanks again! How do you know this stuff? :) – Nic Sep 15 '16 at 9:54
  • @Nic You just read the source, it's all there :) – Clive Sep 15 '16 at 12:04
  • Which class though? Drupal\Core\Entity\Plugin\DataType\EntityReference ? – Nic Sep 15 '16 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.