Drupal8 常用API

Submitted by admin on Tue, 11/05/2019 - 10:18
##获取当前用户
$current_user = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($current_user->id());
// or just
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

## entity query

$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'MY_NODE_TYPE');
$query->condition('langcode', $language()->getId());
$ids = $query->execute();


$media = \Drupal::entityTypeManager()
          ->getListBuilder('media')
          ->getStorage()
          ->loadByProperties([
            'bundle' => 'media_bundle_name',
            'status' => 1,
        ]);
##参考
https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A%3AentityQuery/8.2.x
https://www.drupal.org/node/2849874

语言

Language can be obtained from other node, i.e.:
$language = $node->language();
or language manager:
$language = \Drupal::languageManager()->getCurrentLanguage();