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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题