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 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器