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

/**
 * Implements hook_form_alter.
12 13
 *
 * Adds sitemap settings to entity types that are supported via plugins.
14
 */
Pawel G's avatar
Pawel G committed
15
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
16 17
  $sitemap_form_entity_data = Simplesitemap::get_sitemap_form_entity_data($form_state, $form_id);
  if (empty($sitemap_form_entity_data))
18
    return;
19 20
  $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
21

22
  $sitemap = new Simplesitemap;
23

24
  // Get current entity type sitemap settings.
25
  $entity_types = $sitemap->get_config('entity_types');
26

Pawel G's avatar
Pawel G committed
27
  $form['simple_sitemap'] = array(
28
    '#type' => 'details',
29 30 31 32
    '#group' => 'additional_settings',
    '#title' => t('Simple XML sitemap'),
  );

33 34
  // Attach some js magic to forms.
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
Pawel G's avatar
Pawel G committed
35
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
36
  if (isset($form['additional_settings'])) {
Pawel G's avatar
Pawel G committed
37
    $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
38 39
  }

40
  $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
41
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
42 43 44 45 46 47
    '#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
48
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
49 50 51 52 53 54 55
    '#type' => 'select',
    '#title' => t('Priority'),
    '#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
    '#options' => SitemapGenerator::get_priority_select_values(),
    '#default_value' => $priority,
  );

Pawel G's avatar
Pawel G committed
56
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
57
    '#type' => 'checkbox',
58
    '#title' => t('Regenerate sitemap after hitting Save'),
59
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
60 61
    '#default_value' => FALSE,
  );
62 63 64
  if ($sitemap->get_setting('cron_generate')) {
    $form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
  }
65

Pawel G's avatar
Pawel G committed
66 67
  $form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
  $form['#simple_sitemap']['bundle_name'] = $bundle_name;
68

69
  // Add submission handler.
70
  if (isset($form['actions']['submit']['#submit'])) {
Pawel G's avatar
Pawel G committed
71
    $form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
72
  }
73
  else { // Fix for account page rendering other submit handlers not usable.
Pawel G's avatar
Pawel G committed
74
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
75
  }
Pawel G's avatar
Pawel G committed
76 77
}

78
/**
79
 * Form submission handler called in hook_form_alter.
80
 */
Pawel G's avatar
Pawel G committed
81
function simple_sitemap_entity_form_submit($form, &$form_state) {
82

83
  $sitemap = new Simplesitemap;
84 85
  $values = $form_state->getValues();

86
  // Only make changes in DB if sitemap settings actually changed.
Pawel G's avatar
Pawel G committed
87 88
  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']) {
89

Pawel G's avatar
Pawel G committed
90 91
    $entity_type_id = $form['#simple_sitemap']['entity_type_id'];
    $bundle_name = $form['#simple_sitemap']['bundle_name'];
92 93

    // Get current entity type sitemap settings.
94
    $entity_types = $sitemap->get_config('entity_types');
95

Pawel G's avatar
Pawel G committed
96 97
    $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'];
98

99
    // Save new entity type settings.
100
    $sitemap->save_config('entity_types', $entity_types);
101
  }
102

103
  // Regenerate sitemaps according to user setting.
Pawel G's avatar
Pawel G committed
104
  if ($values['simple_sitemap_regenerate_now']) {
105
    $sitemap->generate_sitemap();
106 107 108 109 110 111
  }
}

/**
 * Implements hook_cron.
 */
Pawel G's avatar
Pawel G committed
112
function simple_sitemap_cron() {
113
  $sitemap = new Simplesitemap;
114 115
  if ($sitemap->get_setting('cron_generate')) {
    // Regenerate sitemap for all languages.
Pawel G's avatar
Pawel G committed
116
    $sitemap->generate_sitemap('cron');
117
  }
Pawel G's avatar
Pawel G committed
118
}
119

120 121
/**
 * Implements hook_entity_bundle_delete().
122 123
 *
 * Removes settings of the removed bundle.
124
 */
Pawel G's avatar
Pawel G committed
125
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
126
  $sitemap = new Simplesitemap;
127
  $entity_types = $sitemap->get_config('entity_types');
128 129 130 131
  $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]);
132
    $sitemap->save_config('entity_types', $entity_types);
133 134 135 136 137
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
    if ($sitemap->get_setting('cron_generate')) {
      $message .= '</br>' . t('Otherwise the sitemap will be rebuilt on next cron run.');
    }
    drupal_set_message($message);
138 139
  }
}