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

gbyte.co's avatar
gbyte.co committed
7
use Drupal\simple_sitemap\SitemapGenerator;
8
use Drupal\simple_sitemap\Form;
9

10 11 12 13 14 15 16 17
/**
 * 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;
}

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

  $f = new Form($form, $form_state, $form_id);
  if (empty($f->entityType)) {
27
    return;
28
  }
gbyte.co's avatar
gbyte.co 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

35 36
  // Do not add sitemap settings to entity form, if sitemap is disabled for
  // entity type.
37
  if ($f->entityType == 'bundle_instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
38 39 40
    return;

  // Setting default form values.
41
  $index = 0;
42 43 44
  $priority = SitemapGenerator::PRIORITY_DEFAULT;

  // Overwriting defaults if settings found for bundle.
45 46 47
  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'];
48 49 50
  }

  // Overwriting defaults if settings found for entity.
51 52 53 54 55
  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'];
    }
56 57
  }

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

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

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

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

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

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

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

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

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

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

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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;
158
    }
159

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

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

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

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

/**
 * 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);
    }
  }
}