SimplesitemapSettings.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Drupal\simple_sitemap;
  3. use Drupal\Core\Config\ConfigFactory;
  4. /**
  5. * Class SimplesitemapSettings
  6. * @package Drupal\simple_sitemap
  7. */
  8. class SimplesitemapSettings {
  9. /**
  10. * @var \Drupal\Core\Config\ConfigFactory
  11. */
  12. protected $configFactory;
  13. /**
  14. * SimplesitemapSettings constructor.
  15. * @param \Drupal\Core\Config\ConfigFactory $config_factory
  16. */
  17. public function __construct(ConfigFactory $config_factory) {
  18. $this->configFactory = $config_factory;
  19. }
  20. /**
  21. * Returns a specific sitemap setting or a default value if setting does not
  22. * exist.
  23. *
  24. * @param string $name
  25. * Name of the setting, like 'max_links'.
  26. *
  27. * @param mixed $default
  28. * Value to be returned if the setting does not exist in the configuration.
  29. *
  30. * @return mixed
  31. * The current setting from configuration or a default value.
  32. */
  33. public function getSetting($name, $default = FALSE) {
  34. $setting = $this->configFactory
  35. ->get('simple_sitemap.settings')
  36. ->get($name);
  37. return NULL !== $setting ? $setting : $default;
  38. }
  39. public function getSettings() {
  40. return $this->configFactory
  41. ->get('simple_sitemap.settings')
  42. ->get();
  43. }
  44. /**
  45. * Stores a specific sitemap setting in configuration.
  46. *
  47. * @param string $name
  48. * Setting name, like 'max_links'.
  49. * @param mixed $setting
  50. * The setting to be saved.
  51. *
  52. * @return $this
  53. */
  54. public function saveSetting($name, $setting) {
  55. $this->configFactory->getEditable('simple_sitemap.settings')
  56. ->set($name, $setting)->save();
  57. return $this;
  58. }
  59. }