Drupal 8 - Get a list of content types

By xngo on March 12, 2019

Here are 2 ways to get a list of content types.

// 1st way
$content_types = \Drupal::entityTypeManager()->getStorage('node_type')->loadMultiple();
// If you need them to disply in a drop down.
$content_type_options=[];
foreach ($content_types as $node_type) {
    $content_type_options[$node_type->id()] = $node_type->label();
}
 
// 2nd way
$content_types = \Drupal\node\Entity\NodeType::loadMultiple();
// If you need them to disply in a drop down.
foreach ($content_types as $node_type) {
    $content_type_options[$node_type->id()] = $node_type->label();
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.