simple_sitemap.module 5.59 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 25
  $sitemap_form_entity_data = Simplesitemap::get_sitemap_form_entity_data($form_state, $form_id);
  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'];
gbyte.co's avatar
gbyte.co committed
29

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

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

gbyte.co's avatar
gbyte.co 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.
gbyte.co's avatar
gbyte.co committed
43
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
44
  if (isset($form['additional_settings'])) {
gbyte.co's avatar
gbyte.co 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;
gbyte.co's avatar
gbyte.co 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;
gbyte.co's avatar
gbyte.co committed
56
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
57 58 59 60 61 62 63
    '#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
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 71 72
  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.');
  }
73

gbyte.co's avatar
gbyte.co 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'])) {
gbyte.co's avatar
gbyte.co committed
79
    $form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
80
  }
81
  else { // Fix for account page rendering other submit handlers not usable.
gbyte.co's avatar
gbyte.co committed
82
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
83
  }
gbyte.co's avatar
gbyte.co committed
84 85
}

86
/**
87
 * Form submission handler called in hook_form_alter.
88
 */
gbyte.co's avatar
gbyte.co 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.
gbyte.co's avatar
gbyte.co 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

gbyte.co's avatar
gbyte.co 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->get_config('entity_types');
103

gbyte.co's avatar
gbyte.co 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->save_config('entity_types', $entity_types);
109
  }
110

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

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

128 129
/**
 * Implements hook_entity_bundle_delete().
130 131
 *
 * Removes settings of the removed bundle.
132
 */
gbyte.co's avatar
gbyte.co committed
133
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
134
  $sitemap = \Drupal::service('simple_sitemap.generator');
135
  $entity_types = $sitemap->get_config('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->save_config('entity_types', $entity_types);
141 142 143 144 145
    $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);
146 147
  }
}