simple_sitemap.module 5.52 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
use Drupal\system\MenuInterface;
11

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

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

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

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
36
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
gbyte.co's avatar
gbyte.co committed
37
  ];
38

gbyte.co's avatar
gbyte.co committed
39
  // Attach some js magic to forms.
gbyte.co's avatar
gbyte.co committed
40 41
  // todo: JS not working on comment entity form, hence disabling.
  if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') {
gbyte.co's avatar
gbyte.co committed
42
    $form['#attached']['library'][] = 'simple_sitemap/form';
gbyte.co's avatar
gbyte.co committed
43
  }
44

gbyte.co's avatar
gbyte.co committed
45 46 47
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
48
  }
49

50
  $f->displayEntitySettings($form['simple_sitemap'])
gbyte.co's avatar
gbyte.co committed
51 52
  // todo: do not show setting when creating new bundle.
    ->displayRegenerateNow($form['simple_sitemap']);
53

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

68
/**
69
 * Form submission handler called in hook_form_alter.
70
 */
gbyte.co's avatar
gbyte.co committed
71
function simple_sitemap_entity_form_submit($form, &$form_state) {
72

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

75 76 77 78
  $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;
79

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

84
    switch ($f->entityCategory) {
85 86

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

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

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

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

114 115 116 117
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
118 119
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
120
 */
gbyte.co's avatar
gbyte.co committed
121
function simple_sitemap_entity_delete(EntityInterface $entity) {
122 123
  \Drupal::service('simple_sitemap.generator')
    ->removeEntityInstanceSettings($entity->getEntityTypeId(), $entity->id());
124
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

/**
 * Implements hook_entity_bundle_delete().
 *
 * Removes settings of the removed bundle.
 */
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
  simple_sitemap_delete_bundle_config($entity_type_id, $bundle);
}

/**
 * Implements hook_menu_delete().
 *
 * Removes settings for the removed menu.
 *
 * @param \Drupal\system\MenuInterface $menu
 */
function simple_sitemap_menu_delete(MenuInterface $menu) {
  simple_sitemap_delete_bundle_config('menu_link_content', $menu->id());
}

/**
 * Helper function used by simple_sitemap_entity_bundle_delete() and
 * simple_sitemap_menu_delete() hooks. This is needed, as menus are technically
 * not bundles.
 *
 * @param string $entity_type_id
 * @param string $bundle
 */
function simple_sitemap_delete_bundle_config($entity_type_id, $bundle) {
  $generator = \Drupal::service('simple_sitemap.generator');
  $deleted_bundle_settings = $generator->getBundleSettings($entity_type_id, $bundle);
  if ($deleted_bundle_settings !== FALSE) {

    // Delete bundle settings.
    \Drupal::service('config.factory')->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle")->delete();

    $message = "You may want to <a href='@url'>regenerate</a> your XML sitemap now.";
    if ($generator->getSetting('cron_generate')) {
      $message .= ' Otherwise the sitemap will be regenerated on the next cron run.';
    }
    drupal_set_message(t($message, ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']));
  }
}