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

Issue #2968133 by gbyte.co: List all existing sitemap instances in the UI

parent b7aeda24
......@@ -123,7 +123,7 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
switch ($f->getEntityCategory()) {
case 'bundle':
$generator->setVariants([$values['simple_sitemap_variant']])
$generator->setVariants($values['simple_sitemap_variant'])
->setBundleSettings($f->getEntityTypeId(),
!empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
$settings
......@@ -131,7 +131,7 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
break;
case 'instance':
$generator->setVariants([$values['simple_sitemap_variant']])
$generator->setVariants($values['simple_sitemap_variant'])
->setEntityInstanceSettings($f->getEntityTypeId(),
!empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
$settings
......
......@@ -113,6 +113,32 @@ class EntityHelper {
: NULL;
}
/**
* @param string $entity_type_id
* @param string|null $bundle_name
* @return array|int
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getEntityInstanceIds($entity_type_id, $bundle_name = NULL) {
$sitemap_entity_types = $this->getSupportedEntityTypes();
if (!isset($sitemap_entity_types[$entity_type_id])) {
return [];
}
$entity_query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
if (!$this->entityTypeIsAtomic($entity_type_id) && NULL !== $bundle_name) {
$keys = $sitemap_entity_types[$entity_type_id]->getKeys();
// Menu fix.
$keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
$entity_query->condition($keys['bundle'], $bundle_name);
}
return $entity_query->execute();
}
/**
* @param $entity_type_name
* @param $entity_id
......
......@@ -4,7 +4,7 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\simple_sitemap\SimplesitemapManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
/**
* Class SimplesitemapSettingsForm
......@@ -26,33 +26,39 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
$form['simple_sitemap_settings']['#prefix'] = $this->getDonationText();
$form['simple_sitemap_settings']['regenerate'] = [
$form['simple_sitemap_settings']['status'] = [
'#type' => 'fieldset',
'#title' => $this->t('Generate sitemaps'),
'#title' => $this->t('Sitemap status'),
'#markup' => '<p>' . $this->t('Sitemaps can be regenerated on demand here.') . '</p>',
'#description' => $this->t('Variants can be configured <a href="@url">here</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap/variants']),
];
$form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
$form['simple_sitemap_settings']['status']['regenerate_submit'] = [
'#type' => 'submit',
'#value' => $this->t('Generate from queue'),
'#submit' => ['::generateSitemap'],
'#validate' => [],
];
// $form['simple_sitemap_settings']['regenerate']['regenerate_backend_submit'] = [
// $form['simple_sitemap_settings']['status']['regenerate_backend_submit'] = [
// '#type' => 'submit',
// '#value' => $this->t('Generate from queue (background)'),
// '#submit' => ['::generateSitemapBackend'],
// '#validate' => [],
// ];
$form['simple_sitemap_settings']['regenerate']['rebuild_queue_submit'] = [
$form['simple_sitemap_settings']['status']['rebuild_queue_submit'] = [
'#type' => 'submit',
'#value' => $this->t('Rebuild queue'),
'#submit' => ['::rebuildQueue'],
'#validate' => [],
];
$form['simple_sitemap_settings']['status']['progress'] = [
'#prefix' => '<div class="simple-sitemap-progress form-item clearfix">',
'#suffix' => '</div>',
];
$queue_worker = $this->generator->getQueueWorker();
$total_count = $queue_worker->getInitialElementCount();
if (!empty($total_count)) {
......@@ -67,12 +73,73 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#percent' => $percent,
'#message' => t('@indexed out of @total items have been processed.', ['@indexed' => $indexed_count, '@total' => $total_count]),
];
$form['simple_sitemap_settings']['progress'] = [
'#markup' => render($index_progress),
'#prefix' => '<div class="simple-sitemap-progress clearfix">',
'#suffix' => '</div>',
];
$form['simple_sitemap_settings']['status']['progress']['#markup'] = render($index_progress);
}
else {
$form['simple_sitemap_settings']['status']['progress']['#markup'] = $this->t('There are no items to be indexed.');
}
$sitemap_manager = $this->generator->getSitemapManager();
$sitemap_statuses = $this->fetchSitemapInstanceStatuses();
foreach ($sitemap_manager->getSitemapTypes() as $type_name => $type_definition) {
if (!empty($variants = $sitemap_manager->getSitemapVariants($type_name, FALSE))) {
$form['simple_sitemap_settings']['status']['types'][$type_name] = [
'#type' => 'details',
'#title' => '<em>' . $type_definition['label'] . '</em> ' . $this->t('sitemaps'),
'#open' => !empty($variants) && count($variants) <= 5,
];
$form['simple_sitemap_settings']['status']['types'][$type_name]['table'] = [
'#type' => 'table',
'#header' => [$this->t('Variant'), $this->t('Status'), /*$this->t('Actions')*/],
];
foreach ($variants as $variant_name => $variant_definition) {
$row = [];
$row['name'] = $variant_definition['label'];
if (!isset($sitemap_statuses[$variant_name])) {
$row['status'] = $this->t('pending');
}
else {
$url = $GLOBALS['base_url'] . '/' . $variant_name . '/sitemap.xml';
switch ($sitemap_statuses[$variant_name]) {
case 0:
$row['status'] = $this->t('generating');
break;
case 1:
$row['status']['data']['#markup'] = $this->t('<a href="@url" target="_blank">published</a>', ['@url' => $url]);
break;
case 2:
$row['status'] = $this->t('<a href="@url" target="_blank">published</a> | regenerating', ['@url' => $url]);
break;
}
}
// $row['actions'] = '';
$form['simple_sitemap_settings']['status']['types'][$type_name]['table']['#rows'][$variant_name] = $row;
unset($sitemap_statuses[$variant_name]);
}
}
}
/* if (!empty($sitemap_statuses)) {
$form['simple_sitemap_settings']['status']['types']['&orphans'] = [
'#type' => 'details',
'#title' => $this->t('Orphans'),
'#open' => TRUE,
];
$form['simple_sitemap_settings']['status']['types']['&orphans']['table'] = [
'#type' => 'table',
'#header' => [$this->t('Variant'), $this->t('Status'), $this->t('Actions')],
];
foreach ($sitemap_statuses as $orphan_name => $orphan_info) {
$form['simple_sitemap_settings']['status']['types']['&orphans']['table']['#rows'][$orphan_name] = [
'name' => $orphan_name,
'status' => $this->t('orphaned'),
'actions' => '',
];
}
}*/
$form['simple_sitemap_settings']['settings'] = [
'#type' => 'fieldset',
......@@ -149,8 +216,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
];
$variants = [];
foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $name => $info) {
$variants[$name] = $this->t($info['label']);
foreach ($this->generator->getSitemapManager()->getSitemapVariants(NULL, FALSE) as $variant_name => $variant_definition) {
$variants[$variant_name] = $this->t($variant_definition['label']);
}
$default_variant = $this->generator->getSetting('default_variant');
......@@ -199,6 +266,29 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
return parent::buildForm($form, $form_state);
}
/**
* @return array
* Array of sitemap statuses keyed by variant name.
* Status values:
* 0: Instance is unpublished
* 1: Instance is published
* 2: Instance is published but is being regenerated
*/
protected function fetchSitemapInstanceStatuses() {
$results = \Drupal::database() //todo DI
->query('SELECT type, status FROM {simple_sitemap} GROUP BY type, status')
->fetchAll();
$instances = [];
foreach ($results as $i => $result) {
$instances[$result->type] = isset($instances[$result->type])
? $result->status + 1
: (int) $result->status;
}
return $instances;
}
/**
* {@inheritdoc}
*/
......
......@@ -102,7 +102,9 @@ class SimplesitemapVariantsForm extends SimplesitemapFormBase {
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$this->generator->rebuildQueue()->generateSitemap();
$this->generator->setVariants(TRUE)
->rebuildQueue()
->generateSitemap();
}
}
......
......@@ -465,7 +465,7 @@ class Simplesitemap {
$bundle_settings->save();
// Delete entity overrides which are identical to new bundle settings.
$entity_ids = $this->getEntityInstanceIds($entity_type_id, $bundle_name);
$entity_ids = $this->entityHelper->getEntityInstanceIds($entity_type_id, $bundle_name);
$query = $this->db->select('simple_sitemap_entity_overrides', 'o')
->fields('o', ['id', 'inclusion_settings'])
->condition('o.entity_type', $entity_type_id)
......@@ -498,28 +498,6 @@ class Simplesitemap {
return $this;
}
/**
* @todo Possibly move to EntityHelper
*/
protected function getEntityInstanceIds($entity_type_id, $bundle_name = NULL) {
$sitemap_entity_types = $this->entityHelper->getSupportedEntityTypes();
if (!isset($sitemap_entity_types[$entity_type_id])) {
return [];
}
$entity_query = $this->entityTypeManager->getStorage($entity_type_id)->getQuery();
if (!$this->entityHelper->entityTypeIsAtomic($entity_type_id) && NULL !== $bundle_name) {
$keys = $sitemap_entity_types[$entity_type_id]->getKeys();
// Menu fix.
$keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
$entity_query->condition($keys['bundle'], $bundle_name);
}
return $entity_query->execute();
}
/**
* Gets sitemap settings for an entity bundle, a non-bundle entity type or for
* all entity types and their bundles.
......@@ -609,8 +587,11 @@ class Simplesitemap {
->getEditable("simple_sitemap.bundle_settings.$variant.$entity_type_id.$bundle_name")->delete();
}
$ids = $this->getEntityInstanceIds($entity_type_id, $bundle_name);
$this->removeEntityInstanceSettings($entity_type_id, (empty($ids) ? NULL : $ids));
$this->removeEntityInstanceSettings($entity_type_id, (
empty($ids)
? NULL
: $this->entityHelper->getEntityInstanceIds($entity_type_id, $bundle_name)
));
}
else {
foreach ($variants as $variant) {
......@@ -747,7 +728,7 @@ class Simplesitemap {
* @return $this
*/
public function removeEntityInstanceSettings($entity_type_id = NULL, $entity_ids = NULL) {
if (empty($variants = $this->getVariants())) {
if (empty($variants = $this->getVariants(FALSE))) {
return $this;
}
......
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