Commit 9f9e8dae authored by gbyte.co's avatar gbyte.co

Remove deprecated methods, clean up

parent 8f6a76ac
......@@ -5,7 +5,7 @@
* Main module file containing hooks.
*/
use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\system\MenuInterface;
......@@ -86,12 +86,18 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat
case 'bundle':
$f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
$generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
$generator->setBundleSettings($f->entityTypeId, $f->bundleName, [
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]);
break;
case 'instance':
$f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
$generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
$generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, [
'index' => $values['simple_sitemap_index_content'],
'priority' => $values['simple_sitemap_priority']
]);
break;
}
......
......@@ -10,6 +10,7 @@ services:
- '@entity_type.manager'
- '@path.validator'
- '@date.formatter'
- '@datetime.time'
simple_sitemap.sitemap_generator:
class: Drupal\simple_sitemap\SitemapGenerator
......@@ -19,6 +20,7 @@ services:
- '@database'
- '@module_handler'
- '@language_manager'
- '@datetime.time'
simple_sitemap.form_helper:
class: Drupal\simple_sitemap\Form\FormHelper
......
......@@ -10,6 +10,7 @@ use Drupal\Core\Path\PathValidator;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Component\Datetime\Time;
/**
* Class Simplesitemap.
......@@ -24,6 +25,7 @@ class Simplesitemap {
private $entityQuery;
private $entityTypeManager;
private $pathValidator;
private $time;
private static $allowed_link_settings = [
'entity' => ['index', 'priority'],
'custom' => ['priority'],
......@@ -32,13 +34,14 @@ class Simplesitemap {
/**
* Simplesitemap constructor.
*
* @param $sitemapGenerator
* @param $configFactory
* @param $database
* @param $entityQuery
* @param $entityTypeManager
* @param $pathValidator
* @param $dateFormatter
* @param \Drupal\simple_sitemap\SitemapGenerator $sitemapGenerator
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Entity\Query\QueryFactory $entityQuery
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Path\PathValidator $pathValidator
* @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
* @param \Drupal\Component\Datetime\Time $time
*/
public function __construct(
SitemapGenerator $sitemapGenerator,
......@@ -47,7 +50,8 @@ class Simplesitemap {
QueryFactory $entityQuery,
EntityTypeManagerInterface $entityTypeManager,
PathValidator $pathValidator,
DateFormatter $dateFormatter
DateFormatter $dateFormatter,
Time $time
) {
$this->sitemapGenerator = $sitemapGenerator;
$this->configFactory = $configFactory;
......@@ -56,6 +60,7 @@ class Simplesitemap {
$this->entityTypeManager = $entityTypeManager;
$this->pathValidator = $pathValidator;
$this->dateFormatter = $dateFormatter;
$this->time = $time;
}
/**
......@@ -503,7 +508,7 @@ class Simplesitemap {
*/
public function getSitemap($chunk_id = NULL) {
$chunks = $this->fetchSitemapChunks();
if (is_null($chunk_id) || !isset($chunks[$chunk_id])) {
if (null === $chunk_id || !isset($chunks[$chunk_id])) {
// Return sitemap index, if there are multiple sitemap chunks.
if (count($chunks) > 1) {
......@@ -598,7 +603,7 @@ class Simplesitemap {
$chunks = $this->fetchSitemapChunks();
if (isset($chunks[1]->sitemap_created)) {
return $this->dateFormatter
->formatInterval(REQUEST_TIME - $chunks[1]->sitemap_created);
->formatInterval($this->time->getRequestTime() - $chunks[1]->sitemap_created);
}
return FALSE;
}
......
......@@ -2,11 +2,12 @@
namespace Drupal\simple_sitemap;
use \XMLWriter;
use XMLWriter;
use Drupal\simple_sitemap\Batch\Batch;
use Drupal\Core\Database\Connection;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Component\Datetime\Time;
/**
* Class SitemapGenerator.
......@@ -28,39 +29,42 @@ class SitemapGenerator {
private $generateFrom = 'form';
private $isHreflangSitemap;
private $generator;
private $time;
/**
* SitemapGenerator constructor.
*
* @param $batch
* @param $database
* @param $module_handler
* @param $language_manager
* @param \Drupal\simple_sitemap\Batch\Batch $batch
* @param \Drupal\Core\Database\Connection $database
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* @param \Drupal\Component\Datetime\Time $time
*/
public function __construct(
Batch $batch,
Connection $database,
ModuleHandler $module_handler,
LanguageManagerInterface $language_manager
LanguageManagerInterface $language_manager,
Time $time
) {
$this->batch = $batch;
$this->db = $database;
$this->moduleHandler = $module_handler;
$this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
$this->isHreflangSitemap = count($language_manager->getLanguages()) > 1;
$this->time = $time;
}
/**
* @param $generator
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* @return $this
*/
public function setGenerator($generator) {
public function setGenerator(Simplesitemap $generator) {
$this->generator = $generator;
return $this;
}
/**
* @param $from
* @param string $from
* @return $this
*/
public function setGenerateFrom($from) {
......@@ -122,8 +126,10 @@ class SitemapGenerator {
foreach ($entity_types as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
// Menu fix.
$keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle'];
foreach ($bundles as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) {
$data_sets[] = [
......@@ -148,13 +154,14 @@ class SitemapGenerator {
* @param bool $remove_sitemap
* Remove old sitemap from database before inserting the new one.
*/
public function generateSitemap($links, $remove_sitemap = FALSE) {
public function generateSitemap(array $links, $remove_sitemap = FALSE) {
// Invoke alter hook.
$this->moduleHandler->alter('simple_sitemap_links', $links);
$values = [
'id' => $remove_sitemap ? 1 : $this->db->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
'sitemap_string' => $this->generateSitemapChunk($links),
'sitemap_created' => REQUEST_TIME,
'sitemap_created' => $this->time->getRequestTime(),
];
if ($remove_sitemap) {
$this->db->truncate('simple_sitemap')->execute();
......@@ -170,7 +177,7 @@ class SitemapGenerator {
*
* @return string sitemap index
*/
public function generateSitemapIndex($chunks) {
public function generateSitemapIndex(array $chunks) {
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(TRUE);
......@@ -204,7 +211,7 @@ class SitemapGenerator {
* @return string
* Sitemap chunk
*/
private function generateSitemapChunk($links) {
private function generateSitemapChunk(array $links) {
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(TRUE);
......
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