simple_sitemap.module 5.34 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
/**
 * Implements hook_help.
 */
gbyte.co's avatar
gbyte.co committed
13
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
14 15 16 17
  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
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
gbyte.co's avatar
gbyte.co committed
25
  if (!$f->alteringForm) {
26
    return;
gbyte.co's avatar
gbyte.co committed
27
  }
gbyte.co's avatar
gbyte.co committed
28 29 30 31 32

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

48
  // Add submission handler.
49
  if (isset($form['actions']['submit']['#submit'])) {
50 51 52 53 54
    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';
      }
    }
55
  }
gbyte.co's avatar
gbyte.co committed
56 57
  // Fix for account page rendering other submit handlers not usable.
  else {
gbyte.co's avatar
gbyte.co committed
58
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
59
  }
gbyte.co's avatar
gbyte.co committed
60 61
}

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

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

69 70 71 72
  $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;
73

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

78
    switch ($f->entityCategory) {
79 80

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

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

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

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

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

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
gbyte.co's avatar
gbyte.co committed
134
function simple_sitemap_entity_delete(EntityInterface $entity) {
gbyte.co's avatar
gbyte.co committed
135 136
  $generator = \Drupal::service('simple_sitemap.generator');
  $entity_types = $generator->getConfig('entity_types');
137 138
  $entity_type_id = $entity->getEntityTypeId();
  $bundle_name = !empty($entity->bundle()) ? $entity->bundle() : $entity_type_id;
gbyte.co's avatar
gbyte.co committed
139 140
  // Menu fix.
  $bundle_name = $bundle_name == 'menu_link_content'&& method_exists($entity, 'getMenuName') ? $entity->getMenuName() : $bundle_name;
141 142
  if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()])) {
    unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()]);
gbyte.co's avatar
gbyte.co committed
143
    $generator->saveConfig('entity_types', $entity_types);
144 145
  }
}