Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanLaufer/03e8d805894c38b32465d7106bfd5fc7 to your computer and use it in GitHub Desktop.
Save DanLaufer/03e8d805894c38b32465d7106bfd5fc7 to your computer and use it in GitHub Desktop.
Drupal 8 - Get Nodes, Many Ways (load, route, php, from db)
// Load a node
$listing_page_node = \Drupal\node\Entity\Node::load($node_id);
// Get current node from the route
\Drupal::routeMatch()->getParameter('node')
\Drupal::routeMatch()->getParameter('node')->getType()
// Get node in PHP and get field
if(\Drupal::routeMatch()->getParameters()->has('node')) {
$current_node = \Drupal::routeMatch()->getParameter('node');
if($current_node->getType() === 'campaign_page') {
$variables['show_limited_menu'] = $current_node->get('field_show_limited_menu')->value;
}
}
// Get nodes from database, sort, filter and prepare display mode render array
$query = \Drupal::entityQuery('node')
->condition('type', 'product_service_detail')
->condition('status', 1)
->sort('created', 'DESC')
->range(0, 4);
$nids = $query->execute();
$nodes = entity_load_multiple('node', $nids);
$variables['featured_nodes'] = \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple($nodes, 'card');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment