Skip to content

Instantly share code, notes, and snippets.

@JeromeChevalier
Last active December 5, 2018 16:26
Show Gist options
  • Save JeromeChevalier/6ed881367e5de1bfad2ad52af966c551 to your computer and use it in GitHub Desktop.
Save JeromeChevalier/6ed881367e5de1bfad2ad52af966c551 to your computer and use it in GitHub Desktop.
Initialize a Select render array with the result of a Entity Query #drupal8
// Load user information
$user = User::load(\Drupal::currentUser()->id());
$tempList = $user->get('field_user_sites')->referencedEntities();
$sitesId = array();
if (count($tempList) > 0) {
foreach ($tempList as $site) {
$sitesId[] = $site->id();
}
}
$sitesEntities = SiteEntity::loadMultiple($sitesId);
$optionArray = array();
foreach ($sitesEntities as $site) {
$siteCode = $site->get('field_site_code')->getString();
$siteName = $site->get('name')->getString();
$optionArray[$siteCode] = $siteName;
}
$form['location'] = array(
'#type' => 'select',
'#title' => $this->t("Location"),
'#size' => 60,
'#multiple' => 'true',
'#search' => 'true',
'#options' => $optionArray
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment