Commit cfd5070d authored by gbyte.co's avatar gbyte.co Committed by gbyte.co

Issue #3061963 by gbyte.co: Show variant publication time on settings page

parent 30ff8389
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\simple_sitemap\Form; namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Datetime\DateFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap; use Drupal\simple_sitemap\Simplesitemap;
...@@ -25,18 +26,25 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -25,18 +26,25 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
*/ */
protected $db; protected $db;
/**
* @var \Drupal\Core\Datetime\DateFormatter
*/
protected $dateFormatter;
/** /**
* SimplesitemapSettingsForm constructor. * SimplesitemapSettingsForm constructor.
* @param \Drupal\simple_sitemap\Simplesitemap $generator * @param \Drupal\simple_sitemap\Simplesitemap $generator
* @param \Drupal\simple_sitemap\Form\FormHelper $form_helper * @param \Drupal\simple_sitemap\Form\FormHelper $form_helper
* @param \Drupal\Core\Language\LanguageManager $language_manager * @param \Drupal\Core\Language\LanguageManager $language_manager
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Datetime\DateFormatter $date_formatter
*/ */
public function __construct( public function __construct(
Simplesitemap $generator, Simplesitemap $generator,
FormHelper $form_helper, FormHelper $form_helper,
LanguageManager $language_manager, LanguageManager $language_manager,
Connection $database Connection $database,
DateFormatter $date_formatter
) { ) {
parent::__construct( parent::__construct(
$generator, $generator,
...@@ -44,6 +52,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -44,6 +52,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
); );
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
$this->db = $database; $this->db = $database;
$this->dateFormatter = $date_formatter;
} }
/** /**
...@@ -54,7 +63,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -54,7 +63,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
$container->get('simple_sitemap.generator'), $container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.form_helper'), $container->get('simple_sitemap.form_helper'),
$container->get('language_manager'), $container->get('language_manager'),
$container->get('database') $container->get('database'),
$container->get('date.formatter')
); );
} }
...@@ -134,6 +144,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -134,6 +144,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'default_variant' => $this->generator->getSetting('default_variant', NULL), 'default_variant' => $this->generator->getSetting('default_variant', NULL),
]; ];
$sitemap_statuses = $this->fetchSitemapInstanceStatuses(); $sitemap_statuses = $this->fetchSitemapInstanceStatuses();
$published_timestamps = $this->fetchSitemapInstancePublishedTimestamps();
foreach ($sitemap_manager->getSitemapTypes() as $type_name => $type_definition) { foreach ($sitemap_manager->getSitemapTypes() as $type_name => $type_definition) {
if (!empty($variants = $sitemap_manager->getSitemapVariants($type_name, FALSE))) { if (!empty($variants = $sitemap_manager->getSitemapVariants($type_name, FALSE))) {
$sitemap_generator = $sitemap_manager $sitemap_generator = $sitemap_manager
...@@ -148,7 +159,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -148,7 +159,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
]; ];
$form['simple_sitemap_settings']['status']['types'][$type_name]['table'] = [ $form['simple_sitemap_settings']['status']['types'][$type_name]['table'] = [
'#type' => 'table', '#type' => 'table',
'#header' => [$this->t('Variant'), $this->t('Status'), /*$this->t('Actions')*/], '#header' => [$this->t('Variant'), $this->t('Status')],
'#attributes' => ['class' => ['form-item', 'clearfix']], '#attributes' => ['class' => ['form-item', 'clearfix']],
]; ];
foreach ($variants as $variant_name => $variant_definition) { foreach ($variants as $variant_name => $variant_definition) {
...@@ -163,19 +174,17 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -163,19 +174,17 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
$row['status'] = $this->t('generating'); $row['status'] = $this->t('generating');
break; break;
case 1: case 1:
$row['status']['data']['#markup'] = $this->t('<a href="@url" target="_blank">published</a>', $row['status']['data']['#markup'] = $this->t('<a href="@url" target="_blank">published on @time</a>',
['@url' => $sitemap_generator->setSitemapVariant($variant_name)->getSitemapUrl()] ['@url' => $sitemap_generator->setSitemapVariant($variant_name)->getSitemapUrl(), '@time' => $this->dateFormatter->format($published_timestamps[$variant_name])]
); );
break; break;
case 2: case 2:
$row['status'] = $this->t('<a href="@url" target="_blank">published</a>, regenerating', $row['status'] = $this->t('<a href="@url" target="_blank">published on @time</a>, regenerating',
['@url' => $sitemap_generator->setSitemapVariant($variant_name)->getSitemapUrl()] ['@url' => $sitemap_generator->setSitemapVariant($variant_name)->getSitemapUrl(), '@time' => $this->dateFormatter->format($published_timestamps[$variant_name])]
); );
break; break;
} }
} }
// $row['actions'] = '';
$form['simple_sitemap_settings']['status']['types'][$type_name]['table']['#rows'][$variant_name] = $row; $form['simple_sitemap_settings']['status']['types'][$type_name]['table']['#rows'][$variant_name] = $row;
unset($sitemap_statuses[$variant_name]); unset($sitemap_statuses[$variant_name]);
} }
...@@ -324,6 +333,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -324,6 +333,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* 0: Instance is unpublished * 0: Instance is unpublished
* 1: Instance is published * 1: Instance is published
* 2: Instance is published but is being regenerated * 2: Instance is published but is being regenerated
*
* @todo Move to SitemapGeneratorBase or DefaultSitemapGenerator so it can be overwritten by sitemap types with custom storages.
*/ */
protected function fetchSitemapInstanceStatuses() { protected function fetchSitemapInstanceStatuses() {
$results = $this->db $results = $this->db
...@@ -340,6 +351,17 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase { ...@@ -340,6 +351,17 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
return $instances; return $instances;
} }
/**
* @return array
*
* @todo Move to SitemapGeneratorBase or DefaultSitemapGenerator so it can be overwritten by sitemap types with custom storages.
*/
protected function fetchSitemapInstancePublishedTimestamps() {
return $this->db
->query('SELECT type, MAX(sitemap_created) FROM (SELECT sitemap_created, type FROM {simple_sitemap} WHERE status = :status) AS timestamps GROUP BY type', [':status' => 1])
->fetchAllKeyed(0, 1);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment