simple_sitemap.module 8.33 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 8
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
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
  $sitemap_form_entity_data = Simplesitemap::getSitemapFormEntityData($form_state, $form_id);
25
  if (empty($sitemap_form_entity_data))
26
    return;
27 28
  $entity_type_id = $sitemap_form_entity_data['entity_type_id'];
  $bundle_name = $sitemap_form_entity_data['bundle_name'];
29
  $entity_id = $sitemap_form_entity_data['entity_id'];
gbyte.co's avatar
gbyte.co committed
30

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

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

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  // Do not add sitemap settings to entity form, if sitemap is disabled for
  // entity type.
  if (!empty($entity_id) && empty($entity_types[$entity_type_id][$bundle_name]['index']))
    return;

  // Setting default form values.
  $index_content_checked = FALSE;
  $priority = SitemapGenerator::PRIORITY_DEFAULT;

  // Overwriting defaults if settings found for bundle.
  if (isset($entity_types[$entity_type_id][$bundle_name]['index'])) {
    $index_content_checked = $entity_types[$entity_type_id][$bundle_name]['index'];
    $priority = $entity_types[$entity_type_id][$bundle_name]['priority'];
  }

  // Overwriting defaults if settings found for entity.
  if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'])) {
    $index_content_checked = $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'];
    $priority = $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['priority'];
  }

gbyte.co's avatar
gbyte.co 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' => !empty($entity_id) ? 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.
gbyte.co's avatar
gbyte.co committed
66
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
67
  if (isset($form['additional_settings'])) {
gbyte.co's avatar
gbyte.co committed
68
    $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
69 70
  }

gbyte.co's avatar
gbyte.co committed
71
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
72
    '#type' => 'checkbox',
73
    '#title' => !empty($entity_id) ? t('Index this entity') : t('Index content of this type'),
74 75 76
    '#default_value' => $index_content_checked,
  );

gbyte.co's avatar
gbyte.co committed
77
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
78 79
    '#type' => 'select',
    '#title' => t('Priority'),
80
    '#description' => !empty($entity_id) ? 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.'),
81
    '#options' => SitemapGenerator::getPrioritySelectValues(),
82 83 84
    '#default_value' => $priority,
  );

gbyte.co's avatar
gbyte.co committed
85
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
86
    '#type' => 'checkbox',
87
    '#title' => t('Regenerate sitemap after hitting Save'),
88
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
89 90
    '#default_value' => FALSE,
  );
91
  if ($sitemap->getSetting('cron_generate')) {
92 93
    $form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
  }
94

gbyte.co's avatar
gbyte.co committed
95 96
  $form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
  $form['#simple_sitemap']['bundle_name'] = $bundle_name;
97
  $form['#simple_sitemap']['entity_id'] = $entity_id;
98

99
  // Add submission handler.
100
  if (isset($form['actions']['submit']['#submit'])) {
101 102 103 104 105
    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';
      }
    }
106
  }
107
  else { // Fix for account page rendering other submit handlers not usable.
gbyte.co's avatar
gbyte.co committed
108
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
109
  }
gbyte.co's avatar
gbyte.co committed
110 111
}

112
/**
113
 * Form submission handler called in hook_form_alter.
114
 */
gbyte.co's avatar
gbyte.co committed
115
function simple_sitemap_entity_form_submit($form, &$form_state) {
116
  $sitemap = \Drupal::service('simple_sitemap.generator');
117 118
  $values = $form_state->getValues();

119
  // Only make changes in DB if sitemap settings actually changed.
gbyte.co's avatar
gbyte.co committed
120 121
  if ($values['simple_sitemap_index_content'] != $form['simple_sitemap']['simple_sitemap_index_content']['#default_value']
    || $values['simple_sitemap_priority'] != $form['simple_sitemap']['simple_sitemap_priority']['#default_value']) {
122

gbyte.co's avatar
gbyte.co committed
123 124
    $entity_type_id = $form['#simple_sitemap']['entity_type_id'];
    $bundle_name = $form['#simple_sitemap']['bundle_name'];
125
    $entity_id = $form['#simple_sitemap']['entity_id'];
126 127

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

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    if (!empty($entity_id)) {
      // Delete overrides if they are identical to bundle settings.
      if ($values['simple_sitemap_index_content'] == $entity_types[$entity_type_id][$bundle_name]['index']
        && $values['simple_sitemap_priority'] == $entity_types[$entity_type_id][$bundle_name]['priority']) {
        unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]);
      }
      // Else save overrides for this entity.
      else {
        $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['index'] = $values['simple_sitemap_index_content'];
        $entity_types[$entity_type_id][$bundle_name]['entities'][$entity_id]['priority'] = $values['simple_sitemap_priority'];
      }
    }
    // Save bundle settings.
    else {
      $entity_types[$entity_type_id][$bundle_name]['index'] = $values['simple_sitemap_index_content'];
      $entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simple_sitemap_priority'];
    }
147
    // Save new entity type settings.
148
    $sitemap->saveConfig('entity_types', $entity_types);
149
  }
150

151
  // Regenerate sitemaps according to user setting.
gbyte.co's avatar
gbyte.co committed
152
  if ($values['simple_sitemap_regenerate_now']) {
153
    $sitemap->generateSitemap();
154 155 156 157 158 159
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
160
function simple_sitemap_cron() {
161
  $sitemap = \Drupal::service('simple_sitemap.generator');
162
  if ($sitemap->getSetting('cron_generate')) {
163
    // Regenerate sitemap for all languages.
164
    $sitemap->generateSitemap('backend');
165
  }
gbyte.co's avatar
gbyte.co committed
166
}
167

168 169
/**
 * Implements hook_entity_bundle_delete().
170 171
 *
 * Removes settings of the removed bundle.
172
 */
gbyte.co's avatar
gbyte.co committed
173
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
174
  $sitemap = \Drupal::service('simple_sitemap.generator');
175
  $entity_types = $sitemap->getConfig('entity_types');
176 177 178
  $bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();
  if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
    unset($entity_types[$bundle_entity_type_id][$bundle]);
179
    $sitemap->saveConfig('entity_types', $entity_types);
180
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
181
    if ($sitemap->getSetting('cron_generate')) {
182 183 184
      $message .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
    }
    drupal_set_message($message);
185 186
  }
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

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