sitemap_status-1.0.0-alpha4/src/Controller/SitemapStatusController.php
src/Controller/SitemapStatusController.php
<?php
namespace Drupal\sitemap_status\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\sitemap_status\SitemapStatusInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class SitemapStatusController.
*/
class SitemapStatusController extends ControllerBase {
/**
* Drupal\Core\Cache\CacheBackendInterface definition.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Drupal\Core\Render\RendererInterface definition.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Drupal\sitemap_status\SitemapStatusInterface definition.
*
* @var \Drupal\sitemap_status\SitemapStatusInterface
*/
protected $sitemapStatus;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->cacheBackend = $container->get('cache.backend.sitemap_status');
$instance->sitemapStatus = $container->get('sitemap_status');
$instance->renderer = $container->get('renderer');
return $instance;
}
/**
* Sitemap status details.
*
* @return array
* Render array.
*/
public function status() {
// @todo translate 0 (unreachable) and 9 status (dom error).
$status = $this->cacheBackend->get(SitemapStatusInterface::CACHE_ID);
$build = [];
if (!empty($status)) {
$header = [
$this->t('Location'),
$this->t('Status'),
];
$groupedLocations = [];
$results = $status->data;
foreach ($results as $status => $locations) {
$statusGroup = substr($status, 0, 1);
if (!array_key_exists($statusGroup, $groupedLocations)) {
$groupedLocations[$statusGroup] = [];
}
foreach ($locations as $location) {
$link = Link::fromTextAndUrl($location, Url::fromUri($location))->toRenderable();
$groupedLocations[$statusGroup][] = [
'location' => $this->renderer->render($link),
'status' => $status . ' [' . $this->sitemapStatus->getStatusDescription((int) $status) . ']',
];
}
}
krsort($groupedLocations);
foreach ($groupedLocations as $group => $locations) {
$build[$group]['details'] = [
'#type' => 'details',
'#title' => $group . 'xx',
'#open' => $group !== 2,
];
$build[$group]['details']['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $locations,
];
}
}
else {
$build = [
'#markup' => $this->t('Not sitemap status yet.'),
];
}
return $build;
}
}
