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