simple_sitemap.module 7.45 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1
<?php
gbyte.co's avatar
gbyte.co committed
2

3 4 5 6
/**
 * @file
 * Main module file containing hooks.
 */
gbyte.co's avatar
gbyte.co committed
7

8
use Drupal\Core\Form\FormStateInterface;
gbyte.co's avatar
gbyte.co committed
9 10
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
11
use Drupal\system\MenuInterface;
12
use Drupal\language\ConfigurableLanguageInterface;
13

14
/**
15
 *Implements hook_help().
gbyte.co's avatar
gbyte.co committed
16 17 18 19
 *
 * @param $route_name
 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
 * @return \Drupal\Component\Render\MarkupInterface|null
20
 */
gbyte.co's avatar
gbyte.co committed
21
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
22
  return $route_name === 'help.page.simple_sitemap' ?
23
    check_markup(file_get_contents(dirname(__FILE__) . "/README.md")) : NULL;
24 25
}

26
/**
27
 * Implements hook_form_alter().
28 29
 *
 * Adds sitemap settings to entity types that are supported via plugins.
gbyte.co's avatar
gbyte.co committed
30 31 32 33
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
34
 */
gbyte.co's avatar
gbyte.co committed
35
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
36

37
  /** @var Drupal\simple_sitemap\Form\FormHelper $f */
gbyte.co's avatar
gbyte.co committed
38 39
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f->processForm($form_state)) {
40
    return;
gbyte.co's avatar
gbyte.co committed
41
  }
gbyte.co's avatar
gbyte.co committed
42 43 44 45

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
46
    '#title' => t('Simple XML Sitemap'),
gbyte.co's avatar
gbyte.co committed
47
    '#description' => $f->getEntityCategory() === 'instance' ? t('Settings for this entity can be overridden here.') : '',
48
    '#weight' => 10,
gbyte.co's avatar
gbyte.co committed
49
  ];
50

gbyte.co's avatar
gbyte.co committed
51 52 53
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
54
  }
55

56
  $f->displayEntitySettings($form['simple_sitemap'])
gbyte.co's avatar
gbyte.co committed
57 58
  // todo: do not show setting when creating new bundle.
    ->displayRegenerateNow($form['simple_sitemap']);
59

60
  // Add submission handler.
61
  if (isset($form['actions']['submit']['#submit'])) {
62
    foreach (array_keys($form['actions']) as $action) {
gbyte.co's avatar
gbyte.co committed
63
      if ($action !== 'preview'
64 65
        && isset($form['actions'][$action]['#type'])
        && $form['actions'][$action]['#type'] === 'submit') {
66 67 68
        $form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
      }
    }
69
  }
gbyte.co's avatar
gbyte.co committed
70 71
  // Fix for account page rendering other submit handlers not usable.
  else {
gbyte.co's avatar
gbyte.co committed
72
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
73
  }
gbyte.co's avatar
gbyte.co committed
74 75
}

76
/**
77
 * Form submission handler called in hook_form_alter.
gbyte.co's avatar
gbyte.co committed
78 79 80
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
81
 */
gbyte.co's avatar
gbyte.co committed
82
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
83

84
  /** @var Drupal\simple_sitemap\Form\FormHelper $f */
gbyte.co's avatar
gbyte.co committed
85 86 87 88
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f->processForm($form_state)) {
    return;
  }
89

90 91 92 93
  $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;
94

95
  // Only make changes in DB if sitemap settings actually changed.
96
  if ($f->valuesChanged($form, $values)) {
97

98
    /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
99
    $generator = \Drupal::service('simple_sitemap.generator');
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    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(),
              !empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
              $settings
            );

            if (empty($settings['index'])) {
              $generator->removeEntityInstanceSettings($f->getEntityTypeId(), $f->getInstanceId());
            }
            break;

          case 'instance':
            $generator->setEntityInstanceSettings($f->getEntityTypeId(),
              !empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
              $settings
            );
            break;
        }
133 134 135
      }
    }

136 137
    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
138 139 140
      $generator->setVariants(TRUE)
        ->rebuildQueue()
        ->generateSitemap();
141
    }
142 143 144 145
  }
}

/**
146
 * Implements hook_cron().
147
 */
gbyte.co's avatar
gbyte.co committed
148
function simple_sitemap_cron() {
149

150
  /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
151
  $generator = \Drupal::service('simple_sitemap.generator');
152

153
  if ($generator->getSetting('cron_generate')) {
154
    $interval = (int) $generator->getSetting('cron_generate_interval', 0) * 60 * 60;
155 156 157 158 159 160 161 162 163 164 165 166
    $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);
      }

gbyte.co's avatar
gbyte.co committed
167
      $generator->generateSitemap('cron');
168
    }
169
  }
gbyte.co's avatar
gbyte.co committed
170
}
171

172 173 174 175 176 177 178
/**
 * 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) {

179
  /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
180
  $generator = \Drupal::service('simple_sitemap.generator');
gbyte.co's avatar
gbyte.co committed
181

182 183 184 185 186 187 188
  $excluded_languages = $generator->getSetting('excluded_languages');
  if (isset($excluded_languages[$language->id()])) {
    unset($excluded_languages[$language->id()]);
    $generator->saveSetting('excluded_languages', $excluded_languages);
  }
}

189 190 191 192
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
193 194
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
195
 */
gbyte.co's avatar
gbyte.co committed
196
function simple_sitemap_entity_delete(EntityInterface $entity) {
197

198
  /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
199
  $generator = \Drupal::service('simple_sitemap.generator');
200
  $generator->setVariants(TRUE)->removeEntityInstanceSettings(
201 202
    $entity->getEntityTypeId(), $entity->id()
  );
203
}
204 205 206 207 208

/**
 * Implements hook_entity_bundle_delete().
 *
 * Removes settings of the removed bundle.
gbyte.co's avatar
gbyte.co committed
209 210 211
 *
 * @param string $entity_type_id
 * @param string $bundle
212 213
 */
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
214 215 216 217

  /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
  $generator = \Drupal::service('simple_sitemap.generator');
  $generator->setVariants(TRUE)->removeBundleSettings($entity_type_id, $bundle);
218 219 220 221 222 223 224 225 226 227
}

/**
 * Implements hook_menu_delete().
 *
 * Removes settings for the removed menu.
 *
 * @param \Drupal\system\MenuInterface $menu
 */
function simple_sitemap_menu_delete(MenuInterface $menu) {
228

229
  /** @var \Drupal\simple_sitemap\Simplesitemap $generator */
230
  $generator = \Drupal::service('simple_sitemap.generator');
231
  $generator->setVariants(TRUE)->removeBundleSettings('menu_link_content', $menu->id());
232
}