0

I have installed ECK module in my website. Using it I have created a "report eck" entity in which I have different bundles like section, notes, etc., each containing different fields. I have my content type "Report" in which I have referred "report eck" entity using entity reference field. Now, in my nice template, I need to get various values including the bundle type, in which I am facing lots of trouble. My bundle type is coming up as following when I am doing kint() on my reference field, but I am not able to get this bundle type and many other values, please guide. enter image description here

Also, it seems to be a protected values, so I navigated to "Available Methods" tabs and found method 'getEntityKey()' which seems to be useful, but how can I use it in twig template??

enter image description here

0

2 Answers 2

0

These are protected. You need to use any available getters to access them. Example, for nodes, the method is getType(). $node->getType(). Most generic entities should have a bundle() method that returns type.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Entity.php/function/Entity%3A%3Abundle/8.4.x

Examples:

$bundle = $myEntity->bundle();

$variables['bundle'] = $bundle;

Twig: {{ bundle }}

4
  • I found some methods in the 'Available Methods' tab, but how to implement them in a twig template?
    – Dev
    Jun 6, 2017 at 13:15
  • You can't implement PHP methods or PHP code in a twig template. You can only inject new variables from the .theme file in a preprocess hook.
    – Kevin
    Jun 6, 2017 at 13:16
  • I know I can not implement PHP in twig template, but I thought there will be some way to access these variables in template, I have updated my question, please have a look
    – Dev
    Jun 6, 2017 at 13:22
  • His code example is for twig template not PHP one. May 26, 2021 at 0:17
0

I will go for render option and use the template instead of manually accessing values. Alternatively, you can go for custom twig functions or filters to access referenced field values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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