Example 1. Taxonomy
Get list of all taxonomy terms
$query = \Drupal::entityQuery('taxonomy_term');
$tids = $query->execute();
Get term id of the vocabulary 'tags'
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', 'tags');
$tids = $query->execute();
Example 2. Node
Find all nodes where the title contains 'drupal'
$query = \Drupal::entityQuery('node');
$query->condition('title', '%drupal%','LIKE');
$nids = $query->execute();
Example 3. User
Fiend all users who's name contains 'admin'
$query = \Drupal::entityQuery('user');
$query->condition('name', '%admin%','LIKE');
$ids = $query->execute();
Example 4. Node with OR condition
$query = \Drupal::entityQuery('node');
$group = $query->orConditionGroup()
->condition('type', 'page')
->condition('type', 'article');
$entity_ids = $query
->condition('title', '%drupal%','LIKE')
->condition($group)
->execute();
Example 5. With multiple AND + OR conditions
$query->condition('type', $type);
$query_or = $query->orConditionGroup();
$query_and = $query->andConditionGroup()
->condition('end1', $end1)
->condition('end2', $end2);
$query_or->condition($query_and);
$query_and = $query->andConditionGroup()
->condition('end1', $end2)
->condition('end2', $end1);
$query_or->condition($query_and);
$query->condition($query_or);
Example 6. Check A field value is NULL or is Not Null
$query = \Drupal::entityQuery('the_entity');
$query->notExists('field_to_check');//Check A field value is NULL
//OR
$query->exists('field_to_check');//Check A field value is NOT NULL
ids = $query->execute();
$current_user = \Drupal::currentUser();
$user_roles = $current_user->getRoles();
$current_uid = $current_user->id();
$user = User::load($current_uid);
$user->{'field_my_def_permission_group'}->value = 'xxx';
$user->save();
drupal 数据处理
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...