DbtngExampleController.php in Examples for Developers 8
File
dbtng_example/src/Controller/DbtngExampleController.php
View source
<?php
namespace Drupal\dbtng_example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\dbtng_example\DbtngExampleRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DbtngExampleController extends ControllerBase {
protected $repository;
public static function create(ContainerInterface $container) {
$controller = new static($container
->get('dbtng_example.repository'));
$controller
->setStringTranslation($container
->get('string_translation'));
return $controller;
}
public function __construct(DbtngExampleRepository $repository) {
$this->repository = $repository;
}
public function entryList() {
$content = [];
$content['message'] = [
'#markup' => $this
->t('Generate a list of all entries in the database. There is no filter in the query.'),
];
$rows = [];
$headers = [
$this
->t('Id'),
$this
->t('uid'),
$this
->t('Name'),
$this
->t('Surname'),
$this
->t('Age'),
];
$entries = $this->repository
->load();
foreach ($entries as $entry) {
$rows[] = array_map('Drupal\\Component\\Utility\\Html::escape', (array) $entry);
}
$content['table'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this
->t('No entries available.'),
];
$content['#cache']['max-age'] = 0;
return $content;
}
public function entryAdvancedList() {
$content = [];
$content['message'] = [
'#markup' => $this
->t('A more complex list of entries in the database. Only the entries with name = "John" and age older than 18 years are shown, the username of the person who created the entry is also shown.'),
];
$headers = [
$this
->t('Id'),
$this
->t('Created by'),
$this
->t('Name'),
$this
->t('Surname'),
$this
->t('Age'),
];
$rows = [];
$entries = $this->repository
->advancedLoad();
foreach ($entries as $entry) {
$rows[] = array_map('Drupal\\Component\\Utility\\Html::escape', $entry);
}
$content['table'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#attributes' => [
'id' => 'dbtng-example-advanced-list',
],
'#empty' => $this
->t('No entries available.'),
];
$content['#cache']['max-age'] = 0;
return $content;
}
}