Skip to content

Instantly share code, notes, and snippets.

@JeromeChevalier
Created December 6, 2018 23:44
Show Gist options
  • Save JeromeChevalier/ea71f0cdd12dc67cb459db12cfb75bbf to your computer and use it in GitHub Desktop.
Save JeromeChevalier/ea71f0cdd12dc67cb459db12cfb75bbf to your computer and use it in GitHub Desktop.
Create a view table based on a query #drupal8
public function displayListDownloads()
{
$header = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('File ID'), 'field' => 'download_file__target_id'),
);
// Retrieve accessible location id by the user
$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();
}
}
$query = \Drupal::database()->select('download_forecast','download');
$query->distinct(true);
$query->leftJoin('download_forecast__physical_location','location','location.entity_id = download.id');
$query->fields('download',['name' , 'download_file__target_id']);
$query->condition('location.physical_location_target_id',$sitesId,'IN');
$fileManagedListArray = $query->execute()->fetchCol(1);
$table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
$pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(1);
$result = $pager->execute();
foreach($result as $row) {
$rows[] = array('data' => (array) $row);
}
$build = array(
'#markup' => t('List of All locations')
);
$build['location_table'] = array(
'#theme' => 'table', '#header' => $header,
'#rows' => $rows
);
$build['pager'] = array(
'#type' => 'pager'
);
return $build;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment