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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou 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();
}