I have created a list of table records in admin section using the following code.

class userDownloadedFiles extends ControllerBase{ public function userList() {
$header = array(

  array('data' => $this->t('Username'), 'field' => 'name', 'sort' => 'asc'),
  array('data' => $this->t('Email'), 'field' => 'mail', 'sort' => 'asc'),

$db = \Drupal::database();
$query = $db->select('users','u');
$query->fields('ufd', array('name','mail'));
$table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')
// Limit the rows to 20 for each page.
$pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')
$result = $pager->execute();

// Populate the rows.
$rows = array();
foreach($result as $row) {
  //Get URL to user profile
  $profile_url = \Drupal::service('path.alias_manager')->getAliasByPath('/user/'.$row->uid);

  $rows[] = array('data' => array(
    'name' => "<a href='$profile_url'>$row->name</a>",
    'mail' => $row->mail,

// The table description.
$build = array(
  '#markup' => t('List of users who have downloaded model drawing or requested an account for downloading one.')

// Generate the table.
$build['user_info_table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#empty' =>t('There are no records yet.'),
  '#rows' => $rows,

// Finally add the pager.
$build['pager'] = array(
  '#type' => 'pager'
return $build;  }}

But the a tag used in username column is displayed as it is not appearing as link. This is how it appearing enter image description here

Please guide me how to make it likable and how would I do it for other columns too.

2 Answers 2


You can try using the render element syntax for a Link element, like this:

$rows[] = array(
  'name' => array(
    '#title' => $row->name,
    '#type' => 'link',
    '#url' => $profile_url
  'mail' => array(
    '#title' => $row->mail,
    '#type' => 'link',
    '#url' => 'mailto:' .  $row->mail,

That way you can stick to a more consistent coding style, i.e. render arrays for presentation.


You need to create your link using FormattableMarkup:

use Drupal\Component\Render\FormattableMarkup;

$rows[] = array('data' => array(
  'name' => new FormattableMarkup('<a href=":link">@name</a>', [':link' => $profile_url, '@name' => $row->name]),
  'mail' => $row->mail,
  • Any possible way to make the whole row clickable? Like we can wrap name and mail in the same hyperlink.
    – nit3ch
    Dec 12, 2017 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.