simple_sitemap.module 8.59 KB
Newer Older
Pawel G's avatar
Pawel G committed
1
<?php
2 3 4 5
/**
 * @file
 * Main module file containing hooks.
 */
Pawel G's avatar
Pawel G committed
6

7
use Drupal\simple_sitemap\Form;
8

9 10 11 12 13 14 15 16
/**
 * Implements hook_help.
 */
function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
  return $route_name === 'help.page.simple_sitemap' ?
    check_markup(file_get_contents(dirname(__FILE__) . "/README.txt")) : NULL;
}

17 18
/**
 * Implements hook_form_alter.
19 20
 *
 * Adds sitemap settings to entity types that are supported via plugins.
21
 */
Pawel G's avatar
Pawel G committed
22
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
23 24

  $f = new Form($form, $form_state, $form_id);
Pawel G's avatar
Pawel G committed
25 26 27

  // Do not alter the form if it is irrelevant to sitemap generation.
  if (empty($f->entityType))
28
    return;
Pawel G's avatar
Pawel G committed
29

30
  $sitemap = \Drupal::service('simple_sitemap.generator');
31

32
  // Get current entity type sitemap settings.
33
  $entity_types = $sitemap->getConfig('entity_types');
34

Pawel G's avatar
Pawel G committed
35
  // Do not alter the form, if sitemap is disabled for entity type.
36
  if ($f->entityType == 'bundle_instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
37 38 39
    return;

  // Setting default form values.
40
  $index = 0;
Pawel G's avatar
Pawel G committed
41
  $priority = Form::PRIORITY_DEFAULT;
42 43

  // Overwriting defaults if settings found for bundle.
44 45 46
  if (isset($entity_types[$f->entityTypeId][$f->bundleName]['index'])) {
    $bundle_index = $index = $entity_types[$f->entityTypeId][$f->bundleName]['index'];
    $bundle_priority = $priority = $entity_types[$f->entityTypeId][$f->bundleName]['priority'];
47 48 49
  }

  // Overwriting defaults if settings found for entity.
50 51 52 53 54
  if ($f->entityType == 'bundle_instance') {
    if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'])) {
      $index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'];
      $priority = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'];
    }
55 56
  }

Pawel G's avatar
Pawel G committed
57
  $form['simple_sitemap'] = array(
58
    '#type' => 'details',
59
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
60
    '#title' => t('Simple XML sitemap'),
61
    '#description' => $f->entityType == 'bundle_instance' ? t('Settings for this specific entity can be overridden here.') : '',
62 63
  );

64 65
  // Attach some js magic to forms.
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
Pawel G's avatar
Pawel G committed
66
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
67
  if (isset($form['additional_settings'])) {
Pawel G's avatar
Pawel G committed
68
    $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
69 70
  }

Pawel G's avatar
Pawel G committed
71
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
72 73 74
    '#type' => 'radios',
    '#default_value' => $index,
    '#options' => [
75 76
      0 => $f->entityType == 'bundle_instance' ? t('Do not index this entity') : t('Do not index entities of this type'),
      1 => $f->entityType == 'bundle_instance' ? t('Index this entity') : t('Index entities of this type'),
77
    ]
78
  );
79 80 81 82
  if ($f->entityType == 'bundle_instance' && isset($bundle_index)) {
    $form['simple_sitemap']['simple_sitemap_index_content']['#options'][$bundle_index] .= ' <em>(' . t('Default') . ')</em>';
  }
  
Pawel G's avatar
Pawel G committed
83
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
84 85
    '#type' => 'select',
    '#title' => t('Priority'),
86
    '#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.'),
87
    '#default_value' => $priority,
Pawel G's avatar
Pawel G committed
88
    '#options' => Form::getPrioritySelectValues(),
89
  );
90 91 92
  if ($f->entityType == 'bundle_instance' && isset($bundle_priority)) {
    $form['simple_sitemap']['simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
  }
93

Pawel G's avatar
Pawel G committed
94
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
95
    '#type' => 'checkbox',
96
    '#title' => t('Regenerate sitemap after hitting <em>Save</em>'),
97
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
98 99
    '#default_value' => FALSE,
  );
100
  if ($sitemap->getSetting('cron_generate')) {
101
    $form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
102
  }
103

104
  $form['#simple_sitemap'] = $f;
105

106
  // Add submission handler.
107
  if (isset($form['actions']['submit']['#submit'])) {
108 109 110 111 112
    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';
      }
    }
113
  }
114
  else { // Fix for account page rendering other submit handlers not usable.
Pawel G's avatar
Pawel G committed
115
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
116
  }
Pawel G's avatar
Pawel G committed
117 118
}

119
/**
120
 * Form submission handler called in hook_form_alter.
121
 */
Pawel G's avatar
Pawel G committed
122
function simple_sitemap_entity_form_submit($form, &$form_state) {
123

124
  // Only make changes in DB if sitemap settings actually changed.
125
  if (Form::valuesChanged($form, $form_state)) {
126

127 128 129
    $sitemap = \Drupal::service('simple_sitemap.generator');
    $f = $form['#simple_sitemap'];
    $values = $form_state->getValues();
130 131

    // Get current entity type sitemap settings.
132
    $entity_types = $sitemap->getConfig('entity_types');
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    switch ($f->entityType) {

      case 'custom':
      case 'bundle':
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : Form::getNewEntityId($form_state);
        $entity_types[$f->entityTypeId][$f->bundleName]['index'] = $values['simple_sitemap_index_content'];
        $entity_types[$f->entityTypeId][$f->bundleName]['priority'] = $values['simple_sitemap_priority'];
        break;

      case 'bundle_instance':
        $f->entityId = !empty($f->entityId) ? $f->entityId : Form::getNewEntityId($form_state);

        // Delete overrides if they are identical to bundle settings.
        if ($values['simple_sitemap_index_content'] == $entity_types[$f->entityTypeId][$f->bundleName]['index']
          && $values['simple_sitemap_priority'] == $entity_types[$f->entityTypeId][$f->bundleName]['priority']) {
          unset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]);
        }
        // Else save overrides for this entity.
        else {
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'] = $values['simple_sitemap_index_content'];
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'] = $values['simple_sitemap_priority'];
        }
        break;
157
    }
158

159
    // Save new entity type settings.
160
    $sitemap->saveConfig('entity_types', $entity_types);
161

162 163
    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
164
      $sitemap->generateSitemap();
165
    }
166 167 168 169 170 171
  }
}

/**
 * Implements hook_cron.
 */
Pawel G's avatar
Pawel G committed
172
function simple_sitemap_cron() {
173
  $sitemap = \Drupal::service('simple_sitemap.generator');
174
  if ($sitemap->getSetting('cron_generate')) {
175
    // Regenerate sitemap for all languages.
176
    $sitemap->generateSitemap('backend');
177
  }
Pawel G's avatar
Pawel G committed
178
}
179

180 181
/**
 * Implements hook_entity_bundle_delete().
182 183
 *
 * Removes settings of the removed bundle.
184
 */
Pawel G's avatar
Pawel G committed
185
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
186
  $sitemap = \Drupal::service('simple_sitemap.generator');
187
  $entity_types = $sitemap->getConfig('entity_types');
188
  $bundle_entity_type_id = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getBundleEntityType();
189 190
  if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
    unset($entity_types[$bundle_entity_type_id][$bundle]);
191
    $sitemap->saveConfig('entity_types', $entity_types);
192
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
193
    if ($sitemap->getSetting('cron_generate')) {
194
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
195 196
    }
    drupal_set_message($message);
197 198
  }
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  $sitemap = \Drupal::service('simple_sitemap.generator');
  $entity_types = $sitemap->getConfig('entity_types');
  $entity_type = $entity->getEntityType();
  if (!empty($entity_type->getBundleEntityType())) {
    $bundle_entity_type = $entity_type->getBundleEntityType();
    $bundle = $entity->bundle();
    if (isset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()])) {
      unset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()]);
      $sitemap->saveConfig('entity_types', $entity_types);
    }
  }
}