Commit 200dda67 authored by gbyte.co's avatar gbyte.co

Inject services into forms instead of creating them

parent c8633a94
......@@ -19,7 +19,7 @@ class Batch {
const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
const ANONYMOUS_USER_ID = 0;
function __construct() {
public function __construct() {
$this->batch = [
'title' => $this->t('Generating XML sitemap'),
'init_message' => $this->t(self::BATCH_INIT_MESSAGE),
......
......@@ -39,7 +39,7 @@ class Form {
/**
* Form constructor.
*/
function __construct($generator) {
public function __construct($generator) {
$this->generator = $generator;
}
......
......@@ -22,9 +22,8 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
$setting_string = '';
foreach ($generator->getConfig('custom') as $custom_link) {
foreach ($this->generator->getConfig('custom') as $custom_link) {
$setting_string .= isset($custom_link['priority'])
? $custom_link['path'] . ' ' . Form::formatPriority($custom_link['priority'])
: $custom_link['path'];
......@@ -35,7 +34,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
'#title' => $this->t('Custom links'),
'#type' => 'fieldset',
'#markup' => '<p>' . $this->t('Add custom internal drupal paths to the XML sitemap.') . '</p>',
'#prefix' => $this->getDonationLink(),
'#prefix' => $this->getDonationText(),
];
$form['simple_sitemap_custom']['custom_links'] = [
......@@ -45,7 +44,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
'#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'. You can optionally add a priority (0.0 - 1.0) by appending it to the path after a space. The home page with the highest priority would be <em>/ 1.0</em>, the contact page with the default priority would be <em>/contact 0.5</em>."),
];
\Drupal::service('simple_sitemap.form')->displayRegenerateNow($form['simple_sitemap_custom']);
$this->form->displayRegenerateNow($form['simple_sitemap_custom']);
return parent::buildForm($form, $form_state);
}
......@@ -58,7 +57,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
$placeholders = ['@line' => ++$i, '@path' => $link_config['path'], '@priority' => isset($link_config['priority']) ? $link_config['priority'] : ''];
// Checking if internal path exists.
if (!\Drupal::service('path.validator')->isValid($link_config['path'])
if (!$this->pathValidator->isValid($link_config['path'])
|| strpos($link_config['path'], '//') !== FALSE) { // Path validator does not see a double slash as an error. Catching this to prevent breaking path generation.
$form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> does not exist.", $placeholders));
}
......@@ -80,15 +79,15 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$custom_links = $this->getCustomLinks($form_state->getValue('custom_links'));
$generator = \Drupal::service('simple_sitemap.generator')->removeCustomLinks();
$this->generator->removeCustomLinks();
foreach ($custom_links as $link_config) {
$generator->addCustomLink($link_config['path'], $link_config);
$this->generator->addCustomLink($link_config['path'], $link_config);
}
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$generator->generateSitemap();
$this->generator->generateSitemap();
}
}
......
......@@ -21,9 +21,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
$form['simple_sitemap_entities']['#prefix'] = $this->getDonationLink();
$form['simple_sitemap_entities']['#prefix'] = $this->getDonationText();
$form['simple_sitemap_entities']['entities'] = [
'#title' => $this->t('Sitemap entities'),
......@@ -35,36 +33,36 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$form['#attached']['drupalSettings']['simple_sitemap'] = ['all_entities' => [], 'atomic_entities' => []];
$entity_type_labels = [];
foreach ($generator->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
foreach ($this->generator->getSitemapEntityTypes() as $entity_type_id => $entity_type) {
$entity_type_labels[$entity_type_id] = $entity_type->getLabel() ? : $entity_type_id;
}
asort($entity_type_labels);
$f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
$this->form->processForm($form_state);
foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
$form['simple_sitemap_entities']['entities'][$entity_type_id] = [
'#type' => 'details',
'#title' => $entity_type_label,
'#open' => $generator->entityTypeIsEnabled($entity_type_id),
'#open' => $this->generator->entityTypeIsEnabled($entity_type_id),
];
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable @entity_type_label <em>(@entity_type_id)</em> support', ['@entity_type_label' => strtolower($entity_type_label), '@entity_type_id' => $entity_type_id]),
'#description' => $this->t('Sitemap settings for this entity type can be set on its bundle pages and overridden on its entity pages.'),
'#default_value' => $generator->entityTypeIsEnabled($entity_type_id),
'#default_value' => $this->generator->entityTypeIsEnabled($entity_type_id),
];
$form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $entity_type_id);
if ($generator->entityTypeIsAtomic($entity_type_id)) {
if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
$form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_enabled']['#description'] = $this->t('Sitemap settings for this entity type can be set below and overridden on its entity pages.');
$f->setEntityCategory('bundle')
$this->form->setEntityCategory('bundle')
->setEntityTypeId($entity_type_id)
->setBundleName($entity_type_id)
->displayEntitySettings($form['simple_sitemap_entities']['entities'][$entity_type_id][$entity_type_id . '_settings'], TRUE);
$form['#attached']['drupalSettings']['simple_sitemap']['atomic_entities'][] = str_replace('_', '-', $entity_type_id);
}
}
$f->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
$this->form->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
return parent::buildForm($form, $form_state);
}
......@@ -72,29 +70,28 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
$values = $form_state->getValues();
foreach($values as $field_name => $value) {
if (substr($field_name, -strlen('_enabled')) == '_enabled') {
$entity_type_id = substr($field_name, 0, -8);
if ($value) {
$generator->enableEntityType($entity_type_id);
if ($generator->entityTypeIsAtomic($entity_type_id)) {
$generator->setBundleSettings($entity_type_id, $entity_type_id, [
$this->generator->enableEntityType($entity_type_id);
if ($this->generator->entityTypeIsAtomic($entity_type_id)) {
$this->generator->setBundleSettings($entity_type_id, $entity_type_id, [
'index' => TRUE,
'priority' => $values[$entity_type_id . '_simple_sitemap_priority']
]);
}
}
else
$generator->disableEntityType($entity_type_id);
$this->generator->disableEntityType($entity_type_id);
}
}
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$generator->generateSitemap();
$this->generator->generateSitemap();
}
}
}
......@@ -3,12 +3,34 @@
namespace Drupal\simple_sitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* SimplesitemapFormBase
*/
abstract class SimplesitemapFormBase extends ConfigFormBase {
protected $generator;
protected $form;
protected $pathValidator;
public function __construct($generator, $form, $path_validator) {
$this->generator = $generator;
$this->form = $form;
$this->pathValidator = $path_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.form'),
$container->get('path.validator')
);
}
/**
* {@inheritdoc}
*/
......@@ -16,7 +38,7 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
return ['simple_sitemap.settings'];
}
protected function getDonationLink() {
protected function getDonationText() {
return "<div class='description'>" . $this->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.", ['@url' => 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5AFYRSBLGSC3W']) . "</div>";
}
}
......@@ -29,9 +29,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
$form['simple_sitemap_settings']['#prefix'] = $this->getDonationLink();
$form['simple_sitemap_settings']['#prefix'] = $this->getDonationText();
$form['simple_sitemap_settings']['regenerate'] = [
'#title' => $this->t('Regenerate sitemap'),
......@@ -47,34 +45,35 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
];
$form['simple_sitemap_settings']['settings'] = [
'#title' => $this->t('Settings'),
'#type' => 'fieldset',
'#title' => $this->t('Settings'),
];
$form['simple_sitemap_settings']['settings']['cron_generate'] = [
'#type' => 'checkbox',
'#title' => $this->t('Regenerate the sitemap on every cron run'),
'#description' => $this->t('Uncheck this if you intend to only regenerate the sitemap manually or via drush.'),
'#default_value' => $generator->getSetting('cron_generate', TRUE),
'#default_value' => $this->generator->getSetting('cron_generate', TRUE),
];
$form['simple_sitemap_settings']['advanced'] = [
'#type' => 'details',
'#title' => $this->t('Advanced settings'),
'#type' => 'fieldset',
'#open' => TRUE,
];
$form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude duplicate links'),
'#description' => $this->t('Uncheck this to significantly speed up the sitemap generation process on a huge site (more than 20 000 indexed entities).'),
'#default_value' => $generator->getSetting('remove_duplicates', TRUE),
'#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
];
$form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
'#description' => $this->t('If checked, only links to the translated content will be included, otherwise the sitemap will include links to all content translation variants, even when the content has not been translated yet.'),
'#default_value' => $generator->getSetting('skip_untranslated', FALSE),
'#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
];
$form['simple_sitemap_settings']['advanced']['max_links'] = [
......@@ -83,7 +82,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#type' => 'textfield',
'#maxlength' => 5,
'#size' => 5,
'#default_value' => $generator->getSetting('max_links', 2000),
'#default_value' => $this->generator->getSetting('max_links', 2000),
];
$form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
......@@ -92,11 +91,11 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#type' => 'textfield',
'#maxlength' => 5,
'#size' => 5,
'#default_value' => $generator->getSetting('batch_process_limit', 1500),
'#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
'#required' => TRUE,
];
\Drupal::service('simple_sitemap.form')->displayRegenerateNow($form['simple_sitemap_settings']);
$this->form->displayRegenerateNow($form['simple_sitemap_settings']);
return parent::buildForm($form, $form_state);
}
......@@ -122,19 +121,18 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$generator = \Drupal::service('simple_sitemap.generator');
foreach($this->form_settings as $setting_name) {
$generator->saveSetting($setting_name, $form_state->getValue($setting_name));
$this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
}
parent::submitForm($form, $form_state);
// Regenerate sitemaps according to user setting.
if ($form_state->getValue('simple_sitemap_regenerate_now')) {
$generator->generateSitemap();
$this->generator->generateSitemap();
}
}
public function generateSitemap(array &$form, FormStateInterface $form_state) {
\Drupal::service('simple_sitemap.generator')->generateSitemap();
$this->generator->generateSitemap();
}
}
......@@ -26,7 +26,7 @@ class Simplesitemap {
* @param \Drupal\Core\Database\Database $database
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
*/
function __construct(
public function __construct(
\Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface,
$database,
\Drupal\Core\Entity\EntityTypeManager $entityTypeManager) {
......
......@@ -21,7 +21,7 @@ class SitemapGenerator {
private $defaultLanguageId;
private $generateFrom = 'form';
function __construct($generator, $database, $language_manager, $module_handler) {
public function __construct($generator, $database, $language_manager, $module_handler) {
$this->generator = $generator;
$this->db = $database;
$this->defaultLanguageId = $language_manager->getDefaultLanguage()->getId();
......
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