Commit d6600e45 authored by Sam152's avatar Sam152 Committed by gbyte.co

Issue #2684757 by Sam152: Convert Simplesitemap class into a service

parent 2e83a6c5
......@@ -4,8 +4,6 @@
* Drush integration.
*/
use Drupal\simple_sitemap\Simplesitemap;
/**
* Implements hook_drush_command().
*/
......@@ -24,6 +22,6 @@ function simple_sitemap_drush_command() {
* Regenerate sitemap for all languages.
*/
function drush_simple_sitemap_generate() {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generate_sitemap('drush');
}
......@@ -4,8 +4,6 @@
* Module install and update procedures.
*/
use Drupal\simple_sitemap\Simplesitemap;
/**
* Implements hook_requirements().
*/
......@@ -23,7 +21,7 @@ function simple_sitemap_requirements($phase) {
switch($phase) {
case 'runtime':
$sitemap = new \Drupal\simple_sitemap\Simplesitemap();
$sitemap = \Drupal::service('simple_sitemap.generator');
$generated_ago = $sitemap->get_generated_ago();
if (!$generated_ago) {
$value = t('Not available');
......@@ -81,7 +79,7 @@ function simple_sitemap_schema() {
* Implements hook_install().
*/
function simple_sitemap_install() {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generate_sitemap('cron');
$base_url = $GLOBALS['base_url'];
drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</a> pages.)<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom")));
......
......@@ -27,7 +27,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$entity_type_id = $sitemap_form_entity_data['entity_type_id'];
$bundle_name = $sitemap_form_entity_data['bundle_name'];
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_config('entity_types');
......@@ -88,7 +88,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
*/
function simple_sitemap_entity_form_submit($form, &$form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$values = $form_state->getValues();
// Only make changes in DB if sitemap settings actually changed.
......@@ -118,7 +118,7 @@ function simple_sitemap_entity_form_submit($form, &$form_state) {
* Implements hook_cron.
*/
function simple_sitemap_cron() {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
if ($sitemap->get_setting('cron_generate')) {
// Regenerate sitemap for all languages.
$sitemap->generate_sitemap('cron');
......@@ -131,7 +131,7 @@ function simple_sitemap_cron() {
* Removes settings of the removed bundle.
*/
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$entity_types = $sitemap->get_config('entity_types');
$bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();
......
......@@ -2,3 +2,6 @@ services:
plugin.manager.simple_sitemap:
class: Drupal\simple_sitemap\SimplesitemapManager
parent: default_plugin_manager
simple_sitemap.generator:
class: Drupal\simple_sitemap\Simplesitemap
arguments: ['@config.factory']
......@@ -8,7 +8,6 @@ namespace Drupal\simple_sitemap\Controller;
use Drupal\Core\Cache\CacheableResponse;
use Symfony\Component\HttpFoundation\Response;
use Drupal\simple_sitemap\Simplesitemap;
/**
* SimplesitemapController.
......@@ -26,7 +25,7 @@ class SimplesitemapController {
* Returns an XML response.
*/
public function get_sitemap($sitemap_id = NULL) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$output = $sitemap->get_sitemap($sitemap_id);
$output = !$output ? '' : $output;
......
......@@ -9,7 +9,6 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
/**
* SimplesitemapCustomLinksFrom
......@@ -35,7 +34,7 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$setting_string = '';
foreach ($sitemap->get_config('custom') as $custom_link) {
......@@ -100,7 +99,7 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$custom_link_config = $this->get_custom_links($form_state->getValue('custom_links'));
foreach($custom_link_config as &$link_config) {
if (isset($link_config['priority'])) {
......
......@@ -9,7 +9,6 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simple_sitemap\Simplesitemap;
/**
* SimplesitemapSettingsFrom
......@@ -35,7 +34,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$form['simple_sitemap_settings']['#prefix'] = "<div class='description'>" .t("If you would like to say thanks and support the development of this module, a <a target='_blank' href='@url'>donation</a> is always appreciated.", array('@url' => 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5AFYRSBLGSC3W')) . "</div>";
......@@ -120,7 +119,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->save_setting('max_links', $form_state->getValue('max_links'));
$sitemap->save_setting('cron_generate', $form_state->getValue('cron_generate'));
$sitemap->save_setting('remove_duplicates', $form_state->getValue('remove_duplicates'));
......@@ -129,7 +128,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
}
public function generate_sitemap(array &$form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generate_sitemap();
}
}
......@@ -5,6 +5,7 @@
*/
namespace Drupal\simple_sitemap;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Cache\Cache;
......@@ -18,13 +19,15 @@ class Simplesitemap {
private $config;
private $sitemap;
function __construct() {
$this->initialize();
}
private function initialize() {
$this->get_config_from_db();
$this->get_sitemap_from_db();
/**
* Simplesitemap constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory from the container.
*/
function __construct(ConfigFactoryInterface $config_factory) {
$this->config = $config_factory->get('simple_sitemap.settings');
$this->sitemap = db_query("SELECT * FROM {simple_sitemap}")->fetchAllAssoc('id');
}
/**
......
......@@ -43,7 +43,7 @@ class SimplesitemapTest extends WebTestBase {
* Test Simple sitemap integration.
*/
public function testSimplesitemap() {
$sitemap = new Simplesitemap;
$sitemap = \Drupal::service('simple_sitemap.generator');
$sitemap->generate_sitemap();
// Verify sitemap.xml can be cached.
......
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