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.


1 Answer 1


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();
  • 1
    Yeah D8 is a dream compared to D7 and before - there are so many convenience methods to make life easier
    – Clive
    Aug 16, 2018 at 14:31

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.