simple_sitemap.module in Simple XML sitemap 8.3
Same filename and directory in other branches
Main module file containing hooks.
File
simple_sitemap.moduleView source
<?php
/**
* @file
* Main module file containing hooks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\simple_sitemap\Queue\QueueWorker;
use Drupal\system\MenuInterface;
use Drupal\language\ConfigurableLanguageInterface;
/**
* Implements hook_help().
*
* @param $route_name
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* @return \Drupal\Component\Render\MarkupInterface|null
*/
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
return $route_name === 'help.page.simple_sitemap' ? check_markup(file_get_contents(__DIR__ . '/README.md')) : NULL;
}
/**
* Implements hook_form_alter().
*
* Adds sitemap settings to entity types that are supported via plugins.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param $form_id
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var Drupal\simple_sitemap\Form\FormHelper $f */
$f = \Drupal::service('simple_sitemap.form_helper');
if (!$f
->processForm($form_state)) {
return;
}
$form['simple_sitemap'] = [
'#type' => 'details',
'#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
'#title' => t('Simple XML Sitemap'),
'#description' => $f
->getEntityCategory() === 'instance' ? t('Settings for this entity can be overridden here.') : '',
'#weight' => 10,
];
// Only attach fieldset summary js to 'additional settings' vertical tabs.
if (isset($form['additional_settings'])) {
$form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$f
->displayEntitySettings($form['simple_sitemap'])
->displayRegenerateNow($form['simple_sitemap']);
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
foreach (array_keys($form['actions']) as $action) {
if ($action !== 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
}
}
}
else {
$form['#submit'][] = 'simple_sitemap_entity_form_submit';
}
}
/**
* Form submission handler called in hook_form_alter.
*
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
/** @var Drupal\simple_sitemap\Form\FormHelper $f */
$f = \Drupal::service('simple_sitemap.form_helper');
if (!$f
->processForm($form_state)) {
return;
}
$values = $form_state
->getValues();
// Fix for values appearing in a sub array on a commerce product entity.
$values = isset($values['simple_sitemap']) ? $values['simple_sitemap'] : $values;
if ($f
->valuesChanged($form, $values)) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
foreach ($generator
->getSitemapManager()
->getSitemapVariants(NULL, FALSE) as $variant => $definition) {
if (isset($values['index_' . $variant . '_' . $f
->getEntityTypeId() . '_settings'])) {
// Variants may have changed since form load.
$settings = [
'index' => (bool) $values['index_' . $variant . '_' . $f
->getEntityTypeId() . '_settings'],
'priority' => $values['priority_' . $variant . '_' . $f
->getEntityTypeId() . '_settings'],
'changefreq' => $values['changefreq_' . $variant . '_' . $f
->getEntityTypeId() . '_settings'],
'include_images' => (bool) $values['include_images_' . $variant . '_' . $f
->getEntityTypeId() . '_settings'],
];
$generator
->setVariants($variant);
switch ($f
->getEntityCategory()) {
case 'bundle':
$generator
->setBundleSettings($f
->getEntityTypeId(), $f
->getBundleName(), $settings);
break;
case 'instance':
if (!$f
->entityIsNew()) {
// Make sure the entity is saved first for multi-step forms, see https://www.drupal.org/project/simple_sitemap/issues/3080510.
$generator
->setEntityInstanceSettings($f
->getEntityTypeId(), $f
->getInstanceId(), $settings);
}
break;
}
}
}
// Regenerate sitemaps according to user setting.
if ($values['simple_sitemap_regenerate_now']) {
$generator
->setVariants(TRUE)
->rebuildQueue()
->generateSitemap();
}
}
}
/**
* Implements hook_cron().
*/
function simple_sitemap_cron() {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator
->getSetting('cron_generate')) {
$interval = (int) $generator
->getSetting('cron_generate_interval', 0) * 60 * 60;
$request_time = \Drupal::service('datetime.time')
->getRequestTime();
$generation_in_progress = $generator
->getQueueWorker()
->generationInProgress();
$state = \Drupal::state();
if ($interval === 0 || $generation_in_progress || $state
->get('simple_sitemap.last_cron_generate', 0) + $interval <= $request_time) {
if (!$generation_in_progress) {
$state
->set('simple_sitemap.last_cron_generate', $request_time);
}
$generator
->generateSitemap(QueueWorker::GENERATE_TYPE_CRON);
}
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*
* When a language is removed from the system remove it also from settings.
*/
function simple_sitemap_configurable_language_delete(ConfigurableLanguageInterface $language) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
$excluded_languages = $generator
->getSetting('excluded_languages');
if (isset($excluded_languages[$language
->id()])) {
unset($excluded_languages[$language
->id()]);
$generator
->saveSetting('excluded_languages', $excluded_languages);
}
}
/**
* Implements hook_entity_delete().
*
* Removes settings of the removed entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
*/
function simple_sitemap_entity_delete(EntityInterface $entity) {
/** @var \Drupal\simple_sitemap\EntityHelper $entity_helper */
$entity_helper = \Drupal::service('simple_sitemap.entity_helper');
if ($entity_helper
->supports($entity
->getEntityType())) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->setVariants(TRUE)
->removeEntityInstanceSettings($entity
->getEntityTypeId(), $entity
->id());
}
}
/**
* Implements hook_entity_bundle_delete().
*
* Removes settings of the removed bundle.
*
* @param string $entity_type_id
* @param string $bundle
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->setVariants(TRUE)
->removeBundleSettings($entity_type_id, $bundle);
}
/**
* Implements hook_menu_delete().
*
* Removes settings for the removed menu.
*
* @param \Drupal\system\MenuInterface $menu
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function simple_sitemap_menu_delete(MenuInterface $menu) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->setVariants(TRUE)
->removeBundleSettings('menu_link_content', $menu
->id());
}
/**
* Implements hook_page_attachments_alter().
*/
function simple_sitemap_page_attachments_alter(array &$attachments) {
if (!empty($attachments['#attached']['html_head_link'])) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator
->getSetting('disable_language_hreflang')) {
// @fixme https://www.drupal.org/project/drupal/issues/1255092
// Content Translation module normally adds identical hreflang tags, so
// executing its hook_page_attachments() implementation would be harmless,
// but if an entity page is configured as the front page, it attaches
// extraneous hreflang tags using the entity URL.
foreach ($attachments['#attached']['html_head_link'] as $key => $list) {
foreach ($list as $k => $element) {
if (!empty($element['hreflang']) && !empty($element['rel'])) {
unset($attachments['#attached']['html_head_link'][$key]);
}
}
}
}
}
}
Functions
Name | Description |
---|---|
simple_sitemap_configurable_language_delete | Implements hook_ENTITY_TYPE_delete(). |
simple_sitemap_cron | Implements hook_cron(). |
simple_sitemap_entity_bundle_delete | Implements hook_entity_bundle_delete(). |
simple_sitemap_entity_delete | Implements hook_entity_delete(). |
simple_sitemap_entity_form_submit | Form submission handler called in hook_form_alter. |
simple_sitemap_form_alter | Implements hook_form_alter(). |
simple_sitemap_help | Implements hook_help(). |
simple_sitemap_menu_delete | Implements hook_menu_delete(). |
simple_sitemap_page_attachments_alter | Implements hook_page_attachments_alter(). |