我想从节点类型中获取字段值并对其进行更改。
我有这个代码,我不知道如何使用它:
$query = \Drupal::entityQuery('node')
->condition('type', 'article');
$nids = $query->execute();
$nodes = $node_storage->loadMultiple($nids);
最佳答案
EntityQuery输出是一个数组,必须在foreach循环中运行。
$query = \Drupal::entityQuery('node')
->condition('type', 'article'),
->condition('field_foo', 42); // some condition
$nids = $query->execute();
$nodes = $node_storage->loadMultiple($nids);
foreach ($nodes as $n) {
echo $n->title->value;
// do whatever you would do with a node object (set fields, save, etc.)
$n->set('title', "this is a test");
$n->save();
}