25526

programmatically get all nodes from drupal 8

Question:

I am trying to get all nodes of a certian type from drupal. I have tried many ways to achieve that, but maybe due to my lack of Drupal custom module programming experience I couldn't achieve my desire. the closest way that I found on the web, is this:

$nids = \Drupal::entityQuery('node')->condition('type','news')->execute(); $nodes = \Drupal\node\Entity\Node::loadMultiple($nids); <ul><li>the first line returns an object of id's</li> <li>the second line returns the nodes of those id's</li> </ul>

this looks easy and straight forward. but, this is the output! { "59": { "in_preview": null }, "61": { "in_preview": null } }

can someone please help, what is wrong? and is this the correct way to do it ?

I want to take the nodes then search every one of them ( I am making some sort of search engine) so I expect some kind of an object that I can then extract the heading, body ... etc, from. is this the correct way ?

Answer1:

This actually returns node objects

$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

perhaps your output gives minimal results and not objects. If you inspect with a proper debugger you will see the objects.

Perhaps its better to actually get one node object at a time if you expect too many nodes to be returned from the query

$query = \Drupal::entityQuery('node') ->condition('status', 1) //published or not ->condition('type', 'news') //content type ->pager(10); //specify results to return $nids = $query->execute(); foreach ($nids as $nid) { $node = \Drupal\node\Entity\Node::load($nid); $body = $node->body->value; $title = $node->title->value; //... }

Recommend

  • Pass Typescript function as a Javascript function
  • nasm issue relocation R_X86_64_PC32 shared library
  • Is there a better way to get data from two tables at once with Sphinx/MySQL?
  • Does pImpl fundamentally solve C++ DLL issue?
  • Take data from different tables and display it in View Index in mvc4
  • How to activate JS data-filter when page loads?
  • How should I start to implement RESTful web service?
  • auth.provider is not set to 'password' when user signs-in with email and password
  • How to display a tooltip according to mouse position? - JavaFX
  • How to resolve this packager error on react native Android
  • Doctrine/Symfony entity generator and generating entity from one table
  • In loopback documentation what does variable 'cb' stands for?
  • Simple linked list-C
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Security issues with PHP's Readfile method
  • CakePHP ACL tutorial initDB function warnings
  • Force show.bind execution
  • onBackPressed() not being executed
  • How to write order and limit within cakephp joins array
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deselecting radio buttons while keeping the View Model in synch
  • Getting last autonumber in access
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • MySQL WHERE-condition in procedure ignored
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • Delete MySQLi record without showing the id in the URL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?