Commit 22f0e398 authored by gbyte.co's avatar gbyte.co

Refactor BatchUrlGenerator class, rename simple_sitemap.batch service to...

Refactor BatchUrlGenerator class, rename simple_sitemap.batch service to simple_sitemap.batch_helper
parent 267033ce
......@@ -4,8 +4,6 @@
* Main module file containing hooks.
*/
use Drupal\simple_sitemap\Form\Form;
/**
* Implements hook_help.
*/
......@@ -20,7 +18,7 @@ function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterfa
* Adds sitemap settings to entity types that are supported via plugins.
*/
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
if (!$f->alteringForm)
return;
......@@ -58,7 +56,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
*/
function simple_sitemap_entity_form_submit($form, &$form_state) {
$f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
$f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
$values = $form_state->getValues();
......
......@@ -19,8 +19,8 @@ services:
- @module_handler
- @language_manager
simple_sitemap.form:
class: Drupal\simple_sitemap\Form\Form
simple_sitemap.form_helper:
class: Drupal\simple_sitemap\Form\FormHelper
public: true
arguments:
- @simple_sitemap.generator
......
......@@ -99,7 +99,9 @@ class Batch {
*/
public static function generateBundleUrls($entity_info, $batch_info, &$context) {
BatchUrlGenerator::service()
->generateBundleUrls($entity_info, $batch_info, $context);
->setContext($context)
->setBatchInfo($batch_info)
->generateBundleUrls($entity_info);
}
/**
......@@ -108,10 +110,14 @@ class Batch {
* @param array $custom_paths
* @param array $batch_info
* @param array &$context
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateCustomUrls($custom_paths, $batch_info, &$context) {
BatchUrlGenerator::service()
->generateCustomUrls($custom_paths, $batch_info, $context);
->setContext($context)
->setBatchInfo($batch_info)
->generateCustomUrls($custom_paths);
}
/**
......@@ -120,6 +126,8 @@ class Batch {
* @param $success
* @param $results
* @param $operations
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function finishGeneration($success, $results, $operations) {
BatchUrlGenerator::service()
......
This diff is collapsed.
......@@ -5,10 +5,10 @@ namespace Drupal\simple_sitemap\Form;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Class Form
* Class FormHelper
* @package Drupal\simple_sitemap\Form
*/
class Form {
class FormHelper {
use StringTranslationTrait;
const PRIORITY_DEFAULT = 0.5;
......@@ -269,7 +269,7 @@ class Form {
* @return bool
* TRUE if simple_sitemap form values have been altered by the user.
*/
public function valuesChanged($form, $values) { //todo make non-static
public function valuesChanged($form, $values) {
foreach (self::$valuesToCheck as $field_name) {
if (isset($values[$field_name]) && $values[$field_name] != $form['simple_sitemap'][$field_name]['#default_value']) {
return TRUE;
......
......@@ -25,7 +25,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
$setting_string = '';
foreach ($this->generator->getConfig('custom') as $custom_link) {
$setting_string .= isset($custom_link['priority'])
? $custom_link['path'] . ' ' . $this->form->formatPriority($custom_link['priority'])
? $custom_link['path'] . ' ' . $this->formHelper->formatPriority($custom_link['priority'])
: $custom_link['path'];
$setting_string .= "\r\n";
}
......@@ -44,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>."),
];
$this->form->displayRegenerateNow($form['simple_sitemap_custom']);
$this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
return parent::buildForm($form, $form_state);
}
......@@ -68,7 +68,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
}
// Making sure the priority is formatted correctly.
if (isset($link_config['priority']) && !Form::isValidPriority($link_config['priority'])) {
if (isset($link_config['priority']) && !FormHelper::isValidPriority($link_config['priority'])) {
$form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.", $placeholders));
}
}
......
......@@ -39,7 +39,7 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
}
asort($entity_type_labels);
$this->form->processForm($form_state);
$this->formHelper->processForm($form_state);
foreach ($entity_type_labels as $entity_type_id => $entity_type_label) {
$form['simple_sitemap_entities']['entities'][$entity_type_id] = [
......@@ -56,14 +56,14 @@ class SimplesitemapEntitiesForm extends SimplesitemapFormBase {
$form['#attached']['drupalSettings']['simple_sitemap']['all_entities'][] = str_replace('_', '-', $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.');
$this->form->setEntityCategory('bundle')
$this->formHelper->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);
}
}
$this->form->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
$this->formHelper->displayRegenerateNow($form['simple_sitemap_entities']['entities']);
return parent::buildForm($form, $form_state);
}
......
......@@ -12,19 +12,19 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SimplesitemapFormBase extends ConfigFormBase {
protected $generator;
protected $form;
protected $formHelper;
protected $pathValidator;
/**
* SimplesitemapFormBase constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $generator
* @param $form
* @param $form_helper
* @param $path_validator
*/
public function __construct($generator, $form, $path_validator) {
public function __construct($generator, $form_helper, $path_validator) {
$this->generator = $generator;
$this->form = $form;
$this->formHelper = $form_helper;
$this->pathValidator = $path_validator;
}
......@@ -34,7 +34,7 @@ abstract class SimplesitemapFormBase extends ConfigFormBase {
public static function create(ContainerInterface $container) {
return new static(
$container->get('simple_sitemap.generator'),
$container->get('simple_sitemap.form'),
$container->get('simple_sitemap.form_helper'),
$container->get('path.validator')
);
}
......
......@@ -10,7 +10,7 @@ use Drupal\Core\Form\FormStateInterface;
*/
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
private $form_settings = [
private $formSettings = [
'max_links',
'cron_generate',
'remove_duplicates',
......@@ -94,7 +94,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#required' => TRUE,
];
$this->form->displayRegenerateNow($form['simple_sitemap_settings']);
$this->formHelper->displayRegenerateNow($form['simple_sitemap_settings']);
return parent::buildForm($form, $form_state);
}
......@@ -108,7 +108,7 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach($this->form_settings as $setting_name) {
foreach($this->formSettings as $setting_name) {
$this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
}
parent::submitForm($form, $form_state);
......
......@@ -3,7 +3,7 @@
namespace Drupal\simple_sitemap;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\simple_sitemap\Form\Form;
use Drupal\simple_sitemap\Form\FormHelper;
/**
* Class Simplesitemap
......@@ -24,17 +24,17 @@ class Simplesitemap {
/**
* Simplesitemap constructor.
* @param $sitemapGenerator
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface
* @param $configFactoryInterface
* @param $database
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* @param $entityTypeManager
* @param $pathValidator
* @param $dateFormatter
*/
public function __construct(
$sitemapGenerator,
\Drupal\Core\Config\ConfigFactoryInterface $configFactoryInterface,
$configFactoryInterface,
$database,
\Drupal\Core\Entity\EntityTypeManager $entityTypeManager,
$entityTypeManager,
$pathValidator,
$dateFormatter
) {
......@@ -313,7 +313,7 @@ class Simplesitemap {
if (in_array($setting_key, self::$allowed_link_settings[$type])) {
switch($setting_key) {
case 'priority':
if (!Form::isValidPriority($setting)) {
if (!FormHelper::isValidPriority($setting)) {
// todo: register error
continue;
}
......
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