How to do some basic code things in Drupal 8

Here goes a few snippets that will help you to start with Drupal 8.

Load a Node

use Drupal\node\Entity\Node;

$node = Node::load($nid);

Load Multiple Nodes

use Drupal\node\Entity\Node;

$nodes = Node::loadMultiple($nids);

Get the node from a request, previously menu_get_object

$node = Drupal::request()->attributes->get('node');

Get the title of a node

$node = \Drupal\node\Entity\Node::load($nid);
$title_field = $node->get('title');
$title = $title_field->value;

Or something more simple

use Drupal\node\Entity\Node;

$title = Node::load($nid)->get('title')->value;

Get the Node Type

$node = Node::load($nid);

Get the value of a field from the node

For a textfield will be something like this


For a link field something like this


For the body you have things like


In the case of a geo field


Depending on the structure of the field you will get the values in the right property, you can take a look to the field table definition to know what can you use after the get.

Render a node view, previously node_view

For a single node

$node = Node::load($nid); 
 $view_builder = \Drupal::entityTypeManager()->getViewBuilder($node->getEntityTypeId());
 $node_view = $view_builder->viewMultiple($nodes, 'teaser');

For multiple nodes at once

$nodes = Node::loadMultiple($nids); 
 $view_builder = \Drupal::entityTypeManager()->getViewBuilder(reset($nodes)->getEntityTypeId());
 $nodes_views = $view_builder->viewMultiple($nodes, 'teaser');

In both cases you can call directly to render to get the html output or in twig just use

{{ node_view }}

You can still do node_view and render but that is calling to deprecated flagged methods, will change some day.

Load a taxonomy term by the name

$terms = taxonomy_term_load_multiple_by_name(‘My Term’);

Note: will return an array of matched terms.

Get the term id

$tid = $term->get('tid')->value;

Entity Query

Basic usage to get all the nodes that are related to a taxonomy term

$nids = \Drupal::entityQuery('node')
 ->condition('status', 1)
 ->condition('type', 'blog_post')
 ->condition('field_blog_category', $tid)
 ->sort('nid', 'DESC')
 ->range(0, 10)

Get the alias of a path

$url_alias = \Drupal::service('path.alias_manager')->getAliasByPath('/taxonomy/term/111’);

Some Magic in Twig

Render the node title

 {{ node.title.value }}

Build the url of the node

{% set node_url = path('entity.node.canonical', {'node':}) %}

Or for other kinds of entities

// Link to the default frontpage content listing view:
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>

// Link to a specific user profile page:
<a href="{{ path('entity.user.canonical', {'user':}) }}">{{ 'View user profile'|t }}</a>

// Link to a view, and throw in some additional query string parameters:
<a href="{{ path('view.articles.page_1', {'page': 2}) }}">{{ 'Go to page 2'|t }}</a>

// Link to a view and pass in some arguments to the view:
<a href="{{ path('view.recent_articles_by_author.page_1', {'arg_0': user.field_display_name.value|drupal_escape }) }}">{{ 'See all the articles written by'|t }} {{ user.field_display_name.value }}</a>

Get the current Path Url

  $current_url = Url::fromRoute('<current>');
  $path = $current_url->toString();

Print the creation date formatted

 {{ node.createdtime|format_date('long') }}

This is nasty, don’t do it at home, print the body after some filters

 {% set body = node.body.value | striptags | raw %}
 {{ body | length > 1000 ? body | slice(0, 1000) | raw ~ '...' : body|raw }}

Have more useful snippets ? Leave me a comment.

Get the current user Object

In Drupal 8 the global variable $user is gone, now you have to do something like this to retrieve the User Object.

   //use Drupal\user\Entity\User;
    $userCurrent = \Drupal::currentUser();
    $user = \Drupal\user\Entity\User::load($userCurrent->id());
    $name = $user->getUsername();

0 comments on “How to do some basic code things in Drupal 8Add yours →

Leave a Reply

Your email address will not be published. Required fields are marked *