7

I have a content type 'news' and created a seperate template for this which is node--news.html.twig. I want to load the node like Drupal 7 so I can print the node title, body and other fields as well. In Drupal 7 we load the node by node_load($nid) but is there any way to load the node in Drupal 8?

1
  • you want to print the values of the current node that you're viewing?
    – No Sssweat
    Oct 10, 2016 at 5:32

3 Answers 3

3

When you are in a node template like node--news.html.twig, then the node is already loaded in the variable node and you can access the field values in twig:

{{ node.field_myfield.value }}

You get the raw values from the database. They are escaped by twig, to prevent hacks. But this escaping makes them useless for any html formatted content.

Most times you use the field values from node for logic, like checking a boolean.

If you want to display the fields, it is preferable to use content:

{{ content.field_myfield }}

The node title is preloaded in the variable label:

{{ label }}
13

Load the node using either \Drupal\node\Entity\Node::load() (whose first argument is the node ID) or \Drupal\node\Entity\Node::loadMultiple() (whose first argument is an array of node IDs, and which returns multiple nodes).

Once you have a node object in $node, you obtain its title from $node->getTitle().

3

One way is to get the title and body of your content type by using views. Create a 'news' view of content type news and select fields - title and body.

Now in your current theme's template folder, create a file :

views-view-table--news--block.html.twig

There you can fetch title and body field and style as you want:

{% set i = 1 %}

 <div id="tabs">
    <ul>
      {% for key,row in rows %}

        <li><a href="#tab-{{key+1}}">

         {% for column in row %} 

         {{ column.field_display_date_1 }} </a></li>


        {% endfor %}

      {% endfor %}
    </ul>

Also you can use below query in your custom module if you don't want to use views module. It will load node object of content type 'news':

$nids = \Drupal::entityQuery('node')
         ->condition('type', $content_type, '=')
         ->condition('langcode', $language, '=')
         ->condition('status', 1, '=')
         ->execute();

  $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

foreach ($nodes as $key => $value) {
  $title =  $value->title->value;
  $description =  strip_tags($value->body->value);
}
5
  • On which file I have to put the above code?
    – fanatic
    Oct 10, 2016 at 5:31
  • I have edited my answer. Oct 10, 2016 at 5:42
  • Thank you Sugandh for your answer but I want to know How to load the node in drupal 8 just like drupal 7. I want to do this by node load.
    – fanatic
    Oct 10, 2016 at 5:48
  • You can use the similar method the way you retrieved it in D7, write the above mentioned custom query in your custom module and you will get a node object. I just edited the query. Take a look. Oct 10, 2016 at 5:51
  • $node = \Drupal\node\Entity\Node::load($nid); try this. Oct 10, 2016 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.