Created
October 19, 2018 17:27
-
-
Save DanLaufer/03e8d805894c38b32465d7106bfd5fc7 to your computer and use it in GitHub Desktop.
Drupal 8 - Get Nodes, Many Ways (load, route, php, from db)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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