Commit 9c6bfc40 authored by gbyte.co's avatar gbyte.co

Refactoring some code.

parent e1987985
......@@ -4,7 +4,6 @@
* Main module file containing hooks.
*/
use Drupal\simple_sitemap\SitemapGenerator;
use Drupal\simple_sitemap\Form;
/**
......@@ -23,23 +22,23 @@ function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterfa
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$f = new Form($form, $form_state, $form_id);
if (empty($f->entityType)) {
// Do not alter the form if it is irrelevant to sitemap generation.
if (empty($f->entityType))
return;
}
$sitemap = \Drupal::service('simple_sitemap.generator');
// Get current entity type sitemap settings.
$entity_types = $sitemap->getConfig('entity_types');
// Do not add sitemap settings to entity form, if sitemap is disabled for
// entity type.
// Do not alter the form, if sitemap is disabled for entity type.
if ($f->entityType == 'bundle_instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
return;
// Setting default form values.
$index = 0;
$priority = SitemapGenerator::PRIORITY_DEFAULT;
$priority = Form::PRIORITY_DEFAULT;
// Overwriting defaults if settings found for bundle.
if (isset($entity_types[$f->entityTypeId][$f->bundleName]['index'])) {
......@@ -86,7 +85,7 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
'#title' => t('Priority'),
'#description' => $f->entityType == 'bundle_instance' ? t('The priority this entity will have in the eyes of search engine bots.') : t('The priority entities of this bundle will have in the eyes of search engine bots.'),
'#default_value' => $priority,
'#options' => SitemapGenerator::getPrioritySelectValues(),
'#options' => Form::getPrioritySelectValues(),
);
if ($f->entityType == 'bundle_instance' && isset($bundle_priority)) {
$form['simple_sitemap']['simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
......
......@@ -11,6 +11,10 @@ namespace Drupal\simple_sitemap;
*/
class Form {
const PRIORITY_DEFAULT = 0.5;
const PRIORITY_HIGHEST = 10;
const PRIORITY_DIVIDER = 10;
public $entityType;
public $entityTypeId;
public $bundleName;
......@@ -135,4 +139,18 @@ class Form {
}
return FALSE;
}
/**
* Gets the values needed to display the priority dropdown setting.
*
* @return array $options
*/
public static function getPrioritySelectValues() {
$options = array();
foreach(range(0, self::PRIORITY_HIGHEST) as $value) {
$value = $value / self::PRIORITY_DIVIDER;
$options[(string)$value] = (string)$value;
}
return $options;
}
}
......@@ -15,9 +15,6 @@ use \XMLWriter;
*/
class SitemapGenerator {
const PRIORITY_DEFAULT = 0.5;
const PRIORITY_HIGHEST = 10;
const PRIORITY_DIVIDER = 10;
const XML_VERSION = '1.0';
const ENCODING = 'UTF-8';
const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
......@@ -33,20 +30,6 @@ class SitemapGenerator {
$this->generatingFrom = $from;
}
/**
* Gets the values needed to display the priority dropdown setting.
*
* @return array $options
*/
public static function getPrioritySelectValues() {
$options = array();
foreach(range(0, self::PRIORITY_HIGHEST) as $value) {
$value = $value / self::PRIORITY_DIVIDER;
$options[(string)$value] = (string)$value;
}
return $options;
}
public function setEntityTypes($entityTypes) {
$this->entityTypes = is_array($entityTypes) ? $entityTypes : array();
}
......
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