📜  Drupal 9 使用实体类型管理器选择具有查询条件的节点数据 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:19.745000             🧑  作者: Mango

代码示例1
use Drupal\node\Entity\Node;

// Various entity type manager query conditions and options
// that you can use to select node data from a drupal database.
$query = \Drupal::entityTypeManager()->getStorage('node')->getQuery();
// get only nodes of this content type
$query->condition('type', 'my_content_type_machine_name');
// that are "published" nodes
$query->condition('status', 1);
// and UUID equals..
$query->condition('uuid', $my_input_uuid);
// Sort by a value in a given field
$query->sort('field_my_custom_field', 'some value');
// To return a subset of the result
// This gives you only the first 10
$query->range(1, 10);
// execute query, it returns an array of matching node ids
$nids = $query->execute();

// LOAD THE RESULTING NODE OBJECTS
if (!empty($nids)) {
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadMultiple($nids);
}