View source
<?php
use Drupal\Component\Utility\Html;
function record_shorten_theme($existing, $type, $theme, $path) {
return [
'record_shorten_records' => [
'variables' => [],
],
];
}
function record_shorten_shorten_create($old, $new, $service) {
$array = [
'original' => $old,
'short' => $new,
'service' => $service,
'uid' => \Drupal::currentUser()
->id(),
'hostname' => \Drupal::request()
->getClientIp(),
'created' => \Drupal::time()
->getRequestTime(),
];
\Drupal::database()
->merge('record_shorten')
->key($array)
->fields($array)
->execute();
}
function record_shorten_records_table() {
$header = [
t('Original'),
t('Short'),
t('Service'),
];
$rows = [];
$result = \Drupal::database()
->select('record_shorten', 'rs')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(10)
->fields('rs', [
'original',
'short',
'service',
])
->orderBy('rs.sid', 'DESC')
->execute();
foreach ($result as $row) {
$rows[] = [
Html::escape($row->original),
Html::escape($row->short),
Html::escape($row->service),
];
}
$table = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
];
$output = \Drupal::service('renderer')
->render($table);
$pager = [
'#type' => 'pager',
];
$output .= \Drupal::service('renderer')
->render($pager);
return $output;
}
function record_shorten_views_api() {
return [
'api' => 3,
];
}