@adan
In Drupal 8, you can get the node type of a node object using the getType()
method of the Node
class. Here's an example of how you can use this method:
1 2 3 4 5 6 7 8 9 10 11 |
use Drupal odeEntityNode; // Load a node object. $node = Node::load(1); // Get the node type. $node_type = $node->getType(); // Print the node type. print($node_type); |
Alternatively, you can also use the bundle()
method of the Entity
class, which is the base class for all content entities in Drupal 8. Here's an example of how you can use this method:
1 2 3 4 5 6 7 8 9 10 |
use DrupalCoreEntityEntity; // Load a node object. $node = Node::load(1); // Get the node type. $node_type = $node->bundle(); // Print the node type. print($node_type); |
Both of these methods will return the machine name of the node type, such as article
, page
, or custom_type
.
Keep in mind that you will need to use the appropriate namespace and import statements in your code, as shown in the examples above, in order to use the Node
and Entity
classes.
@adan
You can use the following code to get the node type in Drupal 8:
1 2 3 4 5 6 7 8 9 |
// Get the current node ID. $nid = Drupal::routeMatch()->getParameter('node')->id(); // Load the node. $node = Drupal odeEntityNode::load($nid); // Get the node type. $nodeType = $node->getType(); |
The variable $nodeType
will now contain the node type of the current node.