1

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->innerJoin('users_field_data','ufd','u.uid=ufd.uid');
$query->fields('ufd', array('name','mail'));
$table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')
                    ->orderByHeader($header);
// Limit the rows to 20 for each page.
$pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')
                    ->limit(20);
$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

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.

| improve this answer | |
1

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,
));
| improve this answer | |
  • Any possible way to make the whole row clickable? Like we can wrap name and mail in the same hyperlink. – nit3ch Dec 12 '17 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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