dth42345 2018-08-10 13:43
浏览 138
已采纳

以编程方式从drupal 8获取所有节点

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
ode\Entity\Node::loadMultiple($nids); 
  • the first line returns an object of id's
  • the second line returns the nodes of those id's

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 ?

  • 写回答

1条回答 默认 最新

  • dsc80135 2018-08-11 09:31
    关注

    This actually returns node objects

    $nodes =  \Drupal
    ode\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
    ode\Entity\Node::load($nid); 
      $body = $node->body->value;
      $title = $node->title->value;
      //...
    }  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Java中import为灰色
    • ¥15 手机等其他智能设备被监听
    • ¥15 在win10 64位的vs打包MSI文件,放到win7 64位上安装成可执行exe文件后,点击程序不运行。
    • ¥50 大众点评用户浏览/消费记录爬虫
    • ¥15 求制作一个个人网页,
    • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
    • ¥15 蓝桥杯c51单片机问题
    • ¥15 ajax跨域问题请求修改代码
    • ¥15 python matplotlib
    • ¥15 短信测压+语音,有偿,必须用Python