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

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

gbyte.co's avatar
gbyte.co 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.
gbyte.co's avatar
gbyte.co committed
35
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
36
  if (isset($form['additional_settings'])) {
gbyte.co's avatar
gbyte.co 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;
gbyte.co's avatar
gbyte.co 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;
gbyte.co's avatar
gbyte.co 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,
  );

gbyte.co's avatar
gbyte.co committed
56
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
57
    '#type' => 'checkbox',
58 59
    '#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.'),
60 61 62
    '#default_value' => FALSE,
  );

gbyte.co's avatar
gbyte.co committed
63 64
  $form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
  $form['#simple_sitemap']['bundle_name'] = $bundle_name;
65

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

75
/**
76
 * Form submission handler called in hook_form_alter.
77
 */
gbyte.co's avatar
gbyte.co committed
78
function simple_sitemap_entity_form_submit($form, &$form_state) {
79

80
  $sitemap = new Simplesitemap;
81 82
  $values = $form_state->getValues();

83
  // Only make changes in DB if sitemap settings actually changed.
gbyte.co's avatar
gbyte.co committed
84 85
  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']) {
86

gbyte.co's avatar
gbyte.co committed
87 88
    $entity_type_id = $form['#simple_sitemap']['entity_type_id'];
    $bundle_name = $form['#simple_sitemap']['bundle_name'];
89 90

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

gbyte.co's avatar
gbyte.co committed
93 94
    $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'];
95

96
    // Save new entity type settings.
97
    $sitemap->save_config('entity_types', $entity_types);
98
  }
99

100
  // Regenerate sitemaps according to user setting.
gbyte.co's avatar
gbyte.co committed
101
  if ($values['simple_sitemap_regenerate_now']) {
102
    $sitemap->generate_sitemap();
103 104 105 106 107 108
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
109
function simple_sitemap_cron() {
110
  $sitemap = new Simplesitemap;
111 112
  if ($sitemap->get_setting('cron_generate')) {
    // Regenerate sitemap for all languages.
gbyte.co's avatar
gbyte.co committed
113
    $sitemap->generate_sitemap('cron');
114
  }
gbyte.co's avatar
gbyte.co committed
115
}
116

117 118
/**
 * Implements hook_entity_bundle_delete().
119 120
 *
 * Removes settings of the removed bundle.
121
 */
gbyte.co's avatar
gbyte.co committed
122
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
123
  $sitemap = new Simplesitemap;
124
  $entity_types = $sitemap->get_config('entity_types');
125 126 127 128
  $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]);
129
    $sitemap->save_config('entity_types', $entity_types);
130
    drupal_set_message(t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.
131
    Otherwise it will be rebuilt on the next cron run.", //todo: First check if cron generation is enabled.
gbyte.co's avatar
gbyte.co committed
132
      array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
133 134
  }
}