simple_sitemap.module 5.58 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

Pawel G's avatar
Pawel G 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
 */
Pawel G's avatar
Pawel G 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'];
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
  $form['simple_sitemap'] = array(
36
    '#type' => 'details',
37 38 39 40
    '#group' => 'additional_settings',
    '#title' => t('Simple XML sitemap'),
  );

41 42
  // Attach some js magic to forms.
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
Pawel G's avatar
Pawel G committed
43
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
44
  if (isset($form['additional_settings'])) {
Pawel G's avatar
Pawel G committed
45
    $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
46 47
  }

48
  $index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
Pawel G's avatar
Pawel G committed
49
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
50 51 52 53 54 55
    '#type' => 'checkbox',
    '#title' => t('Index content of this type'),
    '#default_value' => $index_content_checked,
  );

  $priority = isset($entity_types[$entity_type_id][$bundle_name]['priority']) ? $entity_types[$entity_type_id][$bundle_name]['priority'] : SitemapGenerator::PRIORITY_DEFAULT;
Pawel G's avatar
Pawel G committed
56
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
57 58 59
    '#type' => 'select',
    '#title' => t('Priority'),
    '#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
60
    '#options' => SitemapGenerator::getPrioritySelectValues(),
61 62 63
    '#default_value' => $priority,
  );

Pawel G's avatar
Pawel G committed
64
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
65
    '#type' => 'checkbox',
66
    '#title' => t('Regenerate sitemap after hitting Save'),
67
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
68 69
    '#default_value' => FALSE,
  );
70
  if ($sitemap->getSetting('cron_generate')) {
71 72
    $form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
  }
73

Pawel G's avatar
Pawel G committed
74 75
  $form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
  $form['#simple_sitemap']['bundle_name'] = $bundle_name;
76

77
  // Add submission handler.
78
  if (isset($form['actions']['submit']['#submit'])) {
Pawel G's avatar
Pawel G committed
79
    $form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
80
  }
81
  else { // Fix for account page rendering other submit handlers not usable.
Pawel G's avatar
Pawel G committed
82
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
83
  }
Pawel G's avatar
Pawel G committed
84 85
}

86
/**
87
 * Form submission handler called in hook_form_alter.
88
 */
Pawel G's avatar
Pawel G committed
89
function simple_sitemap_entity_form_submit($form, &$form_state) {
90

91
  $sitemap = \Drupal::service('simple_sitemap.generator');
92 93
  $values = $form_state->getValues();

94
  // Only make changes in DB if sitemap settings actually changed.
Pawel G's avatar
Pawel G committed
95 96
  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']) {
97

Pawel G's avatar
Pawel G committed
98 99
    $entity_type_id = $form['#simple_sitemap']['entity_type_id'];
    $bundle_name = $form['#simple_sitemap']['bundle_name'];
100 101

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

Pawel G's avatar
Pawel G committed
104 105
    $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'];
106

107
    // Save new entity type settings.
108
    $sitemap->saveConfig('entity_types', $entity_types);
109
  }
110

111
  // Regenerate sitemaps according to user setting.
Pawel G's avatar
Pawel G committed
112
  if ($values['simple_sitemap_regenerate_now']) {
113
    $sitemap->generateSitemap();
114 115 116 117 118 119
  }
}

/**
 * Implements hook_cron.
 */
Pawel G's avatar
Pawel G committed
120
function simple_sitemap_cron() {
121
  $sitemap = \Drupal::service('simple_sitemap.generator');
122
  if ($sitemap->getSetting('cron_generate')) {
123
    // Regenerate sitemap for all languages.
124
    $sitemap->generateSitemap('backend');
125
  }
Pawel G's avatar
Pawel G committed
126
}
127

128 129
/**
 * Implements hook_entity_bundle_delete().
130 131
 *
 * Removes settings of the removed bundle.
132
 */
Pawel G's avatar
Pawel G committed
133
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
134
  $sitemap = \Drupal::service('simple_sitemap.generator');
135
  $entity_types = $sitemap->getConfig('entity_types');
136 137 138 139
  $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]);
140
    $sitemap->saveConfig('entity_types', $entity_types);
141
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
142
    if ($sitemap->getSetting('cron_generate')) {
143 144 145
      $message .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
    }
    drupal_set_message($message);
146 147
  }
}