simple_sitemap.module 4.93 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1
<?php
gbyte.co's avatar
gbyte.co committed
2

3 4 5 6
/**
 * @file
 * Main module file containing hooks.
 */
gbyte.co's avatar
gbyte.co committed
7

gbyte.co's avatar
gbyte.co committed
8 9
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
10

11 12 13
/**
 * Implements hook_help.
 */
gbyte.co's avatar
gbyte.co committed
14
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
15
  return $route_name === 'help.page.simple_sitemap' ?
16
    check_markup(file_get_contents(dirname(__FILE__) . "/README.md")) : NULL;
17 18
}

19 20
/**
 * Implements hook_form_alter.
21 22
 *
 * Adds sitemap settings to entity types that are supported via plugins.
23
 */
gbyte.co's avatar
gbyte.co committed
24
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
25

26
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
gbyte.co's avatar
gbyte.co committed
27
  if (!$f->alteringForm) {
28
    return;
gbyte.co's avatar
gbyte.co committed
29
  }
gbyte.co's avatar
gbyte.co committed
30 31 32 33 34

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
35
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
gbyte.co's avatar
gbyte.co committed
36 37
  ];
  // Attach some js magic to forms.
gbyte.co's avatar
gbyte.co committed
38 39
  // todo: JS not working on comment entity form, hence disabling.
  if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') {
gbyte.co's avatar
gbyte.co committed
40
    $form['#attached']['library'][] = 'simple_sitemap/form';
gbyte.co's avatar
gbyte.co committed
41
  }
gbyte.co's avatar
gbyte.co committed
42 43 44
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
45
  }
46
  $f->displayEntitySettings($form['simple_sitemap'])
gbyte.co's avatar
gbyte.co committed
47 48
  // todo: do not show setting when creating new bundle.
    ->displayRegenerateNow($form['simple_sitemap']);
49

50
  // Add submission handler.
51
  if (isset($form['actions']['submit']['#submit'])) {
52 53 54 55 56
    foreach (array_keys($form['actions']) as $action) {
      if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
        $form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
      }
    }
57
  }
gbyte.co's avatar
gbyte.co committed
58 59
  // Fix for account page rendering other submit handlers not usable.
  else {
gbyte.co's avatar
gbyte.co committed
60
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
61
  }
gbyte.co's avatar
gbyte.co committed
62 63
}

64
/**
65
 * Form submission handler called in hook_form_alter.
66
 */
gbyte.co's avatar
gbyte.co committed
67
function simple_sitemap_entity_form_submit($form, &$form_state) {
68

69
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
70

71 72 73 74
  $values = $form_state->getValues();

  // Fix for values appearing in a sub array on a commerce product entity.
  $values = isset($values['simple_sitemap']) ? $values['simple_sitemap'] : $values;
75

76
  // Only make changes in DB if sitemap settings actually changed.
77
  if ($f->valuesChanged($form, $values)) {
gbyte.co's avatar
gbyte.co committed
78
    $generator = \Drupal::service('simple_sitemap.generator');
79

80
    switch ($f->entityCategory) {
81 82

      case 'bundle':
gbyte.co's avatar
gbyte.co committed
83
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
gbyte.co's avatar
gbyte.co committed
84
        $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
85 86
        break;

87
      case 'instance':
gbyte.co's avatar
gbyte.co committed
88
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
gbyte.co's avatar
gbyte.co committed
89
        $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
90
        break;
91
    }
92 93 94

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
gbyte.co's avatar
gbyte.co committed
95
      $generator->generateSitemap();
96
    }
97 98 99 100 101 102
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
103
function simple_sitemap_cron() {
gbyte.co's avatar
gbyte.co committed
104 105 106
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
107
  }
gbyte.co's avatar
gbyte.co committed
108
}
109

110 111
/**
 * Implements hook_entity_bundle_delete().
112 113
 *
 * Removes settings of the removed bundle.
114
 *
115
 * @todo Not working for menu bundles, as they are technically not bundles. Implement hook_menu_delete().
116
 * @todo delete entity overrides
117
 */
gbyte.co's avatar
gbyte.co committed
118
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
gbyte.co's avatar
gbyte.co committed
119 120
  $generator = \Drupal::service('simple_sitemap.generator');
  $entity_types = $generator->getConfig('entity_types');
121 122
  if (isset($entity_types[$entity_type_id][$bundle])) {
    unset($entity_types[$entity_type_id][$bundle]);
gbyte.co's avatar
gbyte.co committed
123
    $generator->saveConfig('entity_types', $entity_types);
gbyte.co's avatar
gbyte.co committed
124
    $message = "You may want to <a href='@url'>regenerate</a> your XML sitemap now.";
gbyte.co's avatar
gbyte.co committed
125
    if ($generator->getSetting('cron_generate')) {
gbyte.co's avatar
gbyte.co committed
126
      $message .= ' Otherwise the sitemap will be regenerated on the next cron run.';
127
    }
gbyte.co's avatar
gbyte.co committed
128
    drupal_set_message(t($message, ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']));
129 130
  }
}
131 132 133 134 135 136

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
gbyte.co's avatar
gbyte.co committed
137
function simple_sitemap_entity_delete(EntityInterface $entity) {
138 139 140 141
  \Drupal::database()->delete('simple_sitemap_entity_overrides')
    ->condition('entity_type', $entity->getEntityTypeId())
    ->condition('entity_id', $entity->id())
    ->execute();
142
}