I'm using hook_ENTITY_TYPE_presave() to alter the values of nodes upon saving, but I only want to do this for a specific content type.

How do I check the content type of the $entity object? I looked at the documentation for class node and working with Entity API, but all I found was how to get the entity type, not the content type.

Given a node $entity object, how can I check the content type?


If you know that you have a node, then you can use getType(). If you don't know that, you can use he generic method ->bundle(). Note that the second will return the entity type in case of an entity type that doesn't have bundles, like users.

Again, my standard reference to http://wizzlern.nl/sites/wizzlern.nl/files/artikel/drupal-content-entity-8.0.pdf, it contains both methods.

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.