I have a comment type that's associated to various content types.

I have the comment entity but I want to get which content type(s) it belongs to.

How can I do that? Given that I can in the first place.

| improve this question | | | | |

You can use Comment::getCommentedEntity() to get the entity, and if it's a node, grab the type:

$entity = $comment->getCommentedEntity();
if ($entity instanceof \Drupal\node\Entity\Node) {
  $type = $entity->getType();
| improve this answer | | | | |
  • 1
    Yeah D8 is a dream compared to D7 and before - there are so many convenience methods to make life easier – Clive Aug 16 '18 at 14:31

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.