simple_sitemap.module 5.84 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

8
use Drupal\Core\Form\FormStateInterface;
gbyte.co's avatar
gbyte.co committed
9 10
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
11
use Drupal\system\MenuInterface;
12

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

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

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

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

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

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

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

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

71
/**
72
 * Form submission handler called in hook_form_alter.
73
 */
gbyte.co's avatar
gbyte.co committed
74
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
75

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

78 79 80 81
  $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;
82

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

87
    switch ($f->entityCategory) {
88 89

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

97
      case 'instance':
gbyte.co's avatar
gbyte.co committed
98
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
99 100 101 102
        $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, [
          'index' => $values['simple_sitemap_index_content'],
          'priority' => $values['simple_sitemap_priority']
        ]);
103
        break;
104
    }
105 106 107

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
gbyte.co's avatar
gbyte.co committed
108
      $generator->generateSitemap();
109
    }
110 111 112 113 114 115
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
116
function simple_sitemap_cron() {
gbyte.co's avatar
gbyte.co committed
117 118 119
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
120
  }
gbyte.co's avatar
gbyte.co committed
121
}
122

123 124 125 126
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
127 128
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
129
 */
gbyte.co's avatar
gbyte.co committed
130
function simple_sitemap_entity_delete(EntityInterface $entity) {
131 132
  \Drupal::service('simple_sitemap.generator')
    ->removeEntityInstanceSettings($entity->getEntityTypeId(), $entity->id());
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 169 170 171 172 173 174 175 176 177

/**
 * 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']));
  }
}
178 179 180 181 182 183 184 185 186 187

/**
 * Implements hook_robotstxt().
 */
function simple_sitemap_robotstxt() {
  return [
    '# XML sitemap',
    'Sitemap: ' . $GLOBALS['base_url'] . '/sitemap.xml',
  ];
}