DBTNGExampleController.php
2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* @file
* Contains \Drupal\dbtng_example\DBTNGExampleController.
*/
namespace Drupal\dbtng_example;
use Drupal\Core\Controller\ControllerBase;
/**
* Controller for DBTNG Example.
*/
class DBTNGExampleController extends ControllerBase {
/**
* Render a list of entries in the database.
*/
public function entryList() {
$content = array();
$content['message'] = array(
'#markup' => $this->t('Generate a list of all entries in the database. There is no filter in the query.'),
);
$rows = array();
$headers = array(t('Id'), t('uid'), t('Name'), t('Surname'), t('Age'));
foreach ($entries = DBTNGExampleStorage::load() as $entry) {
// Sanitize each entry.
$rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', (array) $entry);
}
$content['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => t('No entries available.'),
);
// Don't cache this page.
$content['#cache']['max-age'] = 0;
return $content;
}
/**
* Render a filtered list of entries in the database.
*/
public function entryAdvancedList() {
$content = array();
$content['message'] = array(
'#markup' => $this->t('A more complex list of entries in the database.') . ' ' .
$this->t('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 = array(
t('Id'),
t('Created by'),
t('Name'),
t('Surname'),
t('Age'),
);
$rows = array();
foreach ($entries = DBTNGExampleStorage::advancedLoad() as $entry) {
// Sanitize each entry.
$rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', $entry);
}
$content['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#attributes' => array('id' => 'dbtng-example-advanced-list'),
'#empty' => t('No entries available.'),
);
// Don't cache this page.
$content['#cache']['max-age'] = 0;
return $content;
}
}