simplesitemap.module 4.42 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

7
use Drupal\simplesitemap\Simplesitemap;
8
use Drupal\simplesitemap\SitemapGenerator;
9 10 11

/**
 * Implements hook_form_alter.
12 13
 *
 * Adds sitemap settings to entity types that are supported via plugins.
14
 */
15
function simplesitemap_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'];
gbyte.co's avatar
gbyte.co committed
21

22
  $sitemap = new Simplesitemap;
23

24
  // Get current entity type sitemap settings.
25
  $entity_types = $sitemap->get_config('entity_types');
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  $form['simplesitemap'] = array(
    '#group' => 'additional_settings',
    '#title' => t('Simple XML sitemap'),
    '#type' => 'details'
  );

  $index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
  $form['simplesitemap']['simplesitemap_index_content'] = array(
    '#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;
  $form['simplesitemap']['simplesitemap_priority'] = array(
    '#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,
  );

49
  $form['simplesitemap']['simplesitemap_regenerate_now'] = array(
50
    '#type' => 'checkbox',
51 52
    '#title' => t('Regenerate sitemap after hitting Save'),
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
53 54 55
    '#default_value' => FALSE,
  );

56 57 58
  $form['#simplesitemap']['entity_type_id'] = $entity_type_id;
  $form['#simplesitemap']['bundle_name'] = $bundle_name;

59 60
  // Add submission handler.
  $form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
gbyte.co's avatar
gbyte.co committed
61 62 63

}

64
/**
65
 * Form submission handler called in hook_form_alter.
66
 */
67
function simplesitemap_entity_form_submit($form, &$form_state) {
68

69
  $sitemap = new Simplesitemap;
70 71
  $values = $form_state->getValues();

72
  // Only make changes in DB if sitemap settings actually changed.
73 74
  if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
    || $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
75

76 77
    $entity_type_id = $form['#simplesitemap']['entity_type_id'];
    $bundle_name = $form['#simplesitemap']['bundle_name'];
78 79

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

82 83
    $entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
    $entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
84

85
    // Save new entity type settings.
86
    $sitemap->save_config('entity_types', $entity_types);
87
  }
88

89
  // Regenerate sitemaps according to user setting.
90
  if ($values['simplesitemap_regenerate_now']) {
91
    $sitemap->generate_sitemap();
92 93 94 95 96 97 98 99
  }
}

/**
 * Implements hook_cron.
 */
function simplesitemap_cron() {
  $sitemap = new Simplesitemap;
100 101 102 103
  if ($sitemap->get_setting('cron_generate')) {
    // Regenerate sitemap for all languages.
    $sitemap->generate_sitemap();
  }
gbyte.co's avatar
gbyte.co committed
104
}
105

106 107
/**
 * Implements hook_entity_bundle_delete().
108 109
 *
 * Removes settings of the removed bundle.
110 111 112
 */
function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
  $sitemap = new Simplesitemap;
113
  $entity_types = $sitemap->get_config('entity_types');
114 115 116 117
  $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]);
118
    $sitemap->save_config('entity_types', $entity_types);
119
    drupal_set_message(t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.
120 121
    Otherwise it will be rebuilt on the next cron run.",
      array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
122 123
  }
}