simplesitemap.module 4.94 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
  $form['simplesitemap'] = array(
30
    '#type' => 'details',
31 32 33 34
    '#group' => 'additional_settings',
    '#title' => t('Simple XML sitemap'),
  );

35 36 37 38 39 40 41
  // Attach some js magic to forms.
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  $form['simplesitemap']['#attached']['library'][] = 'simplesitemap/form';
  if (isset($form['additional_settings'])) {
    $form['simplesitemap']['#attached']['library'][] = 'simplesitemap/fieldsetSummaries';
  }

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  $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,
  );

58
  $form['simplesitemap']['simplesitemap_regenerate_now'] = array(
59
    '#type' => 'checkbox',
60 61
    '#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.'),
62 63 64
    '#default_value' => FALSE,
  );

65 66 67
  $form['#simplesitemap']['entity_type_id'] = $entity_type_id;
  $form['#simplesitemap']['bundle_name'] = $bundle_name;

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

77
/**
78
 * Form submission handler called in hook_form_alter.
79
 */
80
function simplesitemap_entity_form_submit($form, &$form_state) {
81

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

85
  // Only make changes in DB if sitemap settings actually changed.
86 87
  if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
    || $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
88

89 90
    $entity_type_id = $form['#simplesitemap']['entity_type_id'];
    $bundle_name = $form['#simplesitemap']['bundle_name'];
91 92

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

95 96
    $entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
    $entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
97

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

102
  // Regenerate sitemaps according to user setting.
103
  if ($values['simplesitemap_regenerate_now']) {
104
    $sitemap->generate_sitemap();
105 106 107 108 109 110 111 112
  }
}

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

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