diff --git a/config/schema/simple_sitemap.schema.yml b/config/schema/simple_sitemap.schema.yml index 01b3b18..5a6a4da 100644 --- a/config/schema/simple_sitemap.schema.yml +++ b/config/schema/simple_sitemap.schema.yml @@ -35,6 +35,9 @@ simple_sitemap.bundle_settings.*.*: priority: label: 'Priority' type: string + priority: + label: 'changefreq' + type: string simple_sitemap.custom: label: 'Custom links' @@ -52,3 +55,6 @@ simple_sitemap.custom: priority: label: 'Priority' type: string + priority: + label: 'changefreq' + type: string diff --git a/js/simple_sitemap.fieldsetSummaries.js b/js/simple_sitemap.fieldsetSummaries.js index 59081f0..b8e2e8f 100644 --- a/js/simple_sitemap.fieldsetSummaries.js +++ b/js/simple_sitemap.fieldsetSummaries.js @@ -13,6 +13,7 @@ if ($(context).find('#edit-simple-sitemap-index-content-1').is(':checked')) { vals.push(Drupal.t('Included in sitemap')); vals.push(Drupal.t('Priority') + ' ' + $('#edit-simple-sitemap-priority option:selected', context).text()); + vals.push(Drupal.t('Changfreq') + ' ' + $('#edit-simple-sitemap-changefreq option:selected', context).text()); } else { vals.push(Drupal.t('Excluded from sitemap')); diff --git a/js/simple_sitemap.form.js b/js/simple_sitemap.form.js index a13b456..f3d0cf4 100644 --- a/js/simple_sitemap.form.js +++ b/js/simple_sitemap.form.js @@ -15,11 +15,13 @@ // On load: Show or hide 'priority' setting dependant on 'enabled' setting. if ($('#edit-simple-sitemap-index-content-1').is(':checked')) { $('.form-item-simple-sitemap-priority').show(); + $('.form-item-simple-sitemap-changefreq').show(); } else { $('.form-item-simple-sitemap-priority').hide(); + $('.form-item-simple-sitemap-changefreq').hide(); } - + // On change: Show or hide 'priority' setting dependant on 'enabled' setting. $("#edit-simple-sitemap-index-content").change(function() { if ($('#edit-simple-sitemap-index-content-1').is(':checked')) { diff --git a/simple_sitemap.module b/simple_sitemap.module index 168f65a..5b12372 100644 --- a/simple_sitemap.module +++ b/simple_sitemap.module @@ -116,7 +116,8 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat !empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(), [ 'index' => $values['simple_sitemap_index_content'], - 'priority' => $values['simple_sitemap_priority'] + 'priority' => $values['simple_sitemap_priority'], + 'changefreq' => $values['simple_sitemap_changefreq'] ] ); break; @@ -127,7 +128,8 @@ function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_stat !empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(), [ 'index' => $values['simple_sitemap_index_content'], - 'priority' => $values['simple_sitemap_priority'] + 'priority' => $values['simple_sitemap_priority'], + 'changefreq' => $values['simple_sitemap_changefreq'] ] ); break; diff --git a/src/Batch/EntityUrlGenerator.php b/src/Batch/EntityUrlGenerator.php index fc0e072..5b2e3b2 100644 --- a/src/Batch/EntityUrlGenerator.php +++ b/src/Batch/EntityUrlGenerator.php @@ -63,6 +63,9 @@ class EntityUrlGenerator extends UrlGeneratorBase implements UrlGeneratorInterfa ? date_iso8601($entity->getChangedTime()) : NULL, 'priority' => $entity_settings['priority'], ]; + if($entity_settings['changefreq']){ + $path_data['changefreq'] = $entity_settings['changefreq']; + } $this->addUrlVariants($url_object, $path_data, $entity); } $this->processSegment(); diff --git a/src/Form/FormHelper.php b/src/Form/FormHelper.php index 4738ed2..f2c6c29 100644 --- a/src/Form/FormHelper.php +++ b/src/Form/FormHelper.php @@ -19,6 +19,10 @@ class FormHelper { const PRIORITY_HIGHEST = 10; const PRIORITY_DIVIDER = 10; + const CHANGEFREQ_DAILY = 'daily'; + const CHANGEFREQ_MONTHLY = 'monthly'; + const CHANGEFREQ_YEARLY = 'yearly'; + /** * @var \Drupal\simple_sitemap\Simplesitemap */ @@ -69,6 +73,7 @@ class FormHelper { protected static $valuesToCheck = [ 'simple_sitemap_index_content', 'simple_sitemap_priority', + 'simple_sitemap_changefreq', 'simple_sitemap_regenerate_now', ]; @@ -225,7 +230,7 @@ class FormHelper { $index = isset($settings['index']) ? $settings['index'] : 0; $priority = isset($settings['priority']) ? $settings['priority'] : self::PRIORITY_DEFAULT; $bundle_name = !empty($this->getBundleName()) ? $this->getBundleName() : $this->t('undefined'); - + $changefreq = isset($settings['changefreq']) ? $settings['changefreq'] : $this->getChangeFreqDefaultValue($this->getBundleName()); if (!$multiple) { $form_fragment[$prefix . 'simple_sitemap_index_content'] = [ '#type' => 'radios', @@ -253,9 +258,19 @@ class FormHelper { '#default_value' => $priority, '#options' => $this->getPrioritySelectValues(), ]; + $form_fragment[$prefix . 'simple_sitemap_changefreq'] = [ + '#type' => 'select', + '#title' => $this->t('Change frequency'), + '#description' => '', + '#default_value' => $changefreq, + '#options' => $this->getChangeFreqSelectValues(), + ]; if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['priority'])) { $form_fragment[$prefix . 'simple_sitemap_priority']['#options'][$this->formatPriority($bundle_settings['priority'])] .= ' (' . $this->t('Default') . ')'; } + if ($this->getEntityCategory() == 'instance' && isset($bundle_settings['changefreq'])) { + $form_fragment[$prefix . 'simple_sitemap_changefreq']['#options'][$bundle_settings['changefreq']] .= ' (' . $this->t('Default') . ')'; + } return $this; } @@ -373,6 +388,31 @@ class FormHelper { } /** + * Gets the values needed to display the changefreq dropdown setting. + * + * @return array + */ + public function getChangeFreqSelectValues() { + $options = []; + $options[self::CHANGEFREQ_DAILY] = ucfirst(self::CHANGEFREQ_DAILY); + $options[self::CHANGEFREQ_MONTHLY] = ucfirst(self::CHANGEFREQ_MONTHLY); + $options[self::CHANGEFREQ_YEARLY] = ucfirst(self::CHANGEFREQ_YEARLY); + return $options; + } + + /** + * Tries to get the best default value. + * + * @param string $bundle + * @return string + */ + public function getChangeFreqDefaultValue($bundle) { + // TODO: Get the default for the bundle if not get the daily one by default. + return self::CHANGEFREQ_DAILY; + } + + + /** * @param string $priority * @return string */ diff --git a/src/Simplesitemap.php b/src/Simplesitemap.php index d15a1cd..712f62d 100644 --- a/src/Simplesitemap.php +++ b/src/Simplesitemap.php @@ -55,7 +55,7 @@ class Simplesitemap { * @var array */ protected static $allowed_link_settings = [ - 'entity' => ['index', 'priority'], + 'entity' => ['index', 'priority', 'changefreq'], 'custom' => ['priority'], ]; diff --git a/src/SitemapGenerator.php b/src/SitemapGenerator.php index 55e02c9..3bc4dc3 100644 --- a/src/SitemapGenerator.php +++ b/src/SitemapGenerator.php @@ -149,6 +149,7 @@ class SitemapGenerator { foreach ($this->generator->getCustomLinks() as $i => $custom_path) { $paths[$i]['path'] = $custom_path['path']; $paths[$i]['priority'] = isset($custom_path['priority']) ? $custom_path['priority'] : NULL; + $paths[$i]['changefreq'] = isset($custom_path['changefreq']) ? $custom_path['changefreq'] : NULL; // todo: implement lastmod. $paths[$i]['lastmod'] = NULL; } @@ -269,7 +270,7 @@ class SitemapGenerator { } foreach ($links as $link) { - + // Add each translation variant URL as location to the sitemap. $writer->startElement('url'); $writer->writeElement('loc', $link['url']); @@ -292,7 +293,10 @@ class SitemapGenerator { $writer->writeElement('lastmod', $link['lastmod']); } - //todo: Implement changefreq here. + // Add changereq + if (isset($link['changefreq'])) { + $writer->writeElement('changefreq', $link['changefreq']); + } // Add priority if any. if (isset($link['priority'])) {