1.使用Entity::load
,每种实体都有自己的load方法, 比如:
use Drupal\node\Entity\Node;
$node_id = 1;
$node = Node::load($node_id);
2.使用EntityTypeManagerInterface
use Drupal\Core\Entity\EntityTypeManagerInterface;
public function test(EntityTypeManagerInterface $entity_type_manager) {
$netm = $entity_type_manager->getStorage('node');//node为实体类型
$node_id = 1;
$node = $netm->load($node_id);//根据id获取Node
//根据属性获取Nodes
$nodes = $netm->loadByProperties([
'type' => 'article',
'field_test' => 'testtest',
]);
}