simple_sitemap.module 6.33 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 29 30
  /**
   * @var Drupal\simple_sitemap\Form\FormHelper $f
   */
31
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
32
  if (!$f->alteringForm()) {
33
    return;
gbyte.co's avatar
gbyte.co committed
34
  }
gbyte.co's avatar
gbyte.co committed
35 36 37 38 39

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

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

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

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

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

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

79 80 81
  /**
   * @var Drupal\simple_sitemap\Form\FormHelper $f
   */
82
  $f = \Drupal::service('simple_sitemap.form_helper')->processForm($form_state);
83

84 85 86 87
  $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;
88

89
  // Only make changes in DB if sitemap settings actually changed.
90
  if ($f->valuesChanged($form, $values)) {
91 92 93 94

    /**
     * @var \Drupal\simple_sitemap\Simplesitemap $generator
     */
gbyte.co's avatar
gbyte.co committed
95
    $generator = \Drupal::service('simple_sitemap.generator');
96

97
    switch ($f->getEntityCategory()) {
98 99

      case 'bundle':
100 101 102 103 104 105 106 107
        $generator->setBundleSettings(
          $f->getEntityTypeId(),
          !empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
          [
            'index' => $values['simple_sitemap_index_content'],
            'priority' => $values['simple_sitemap_priority']
          ]
        );
108 109
        break;

110
      case 'instance':
111 112 113 114 115 116 117 118
        $generator->setEntityInstanceSettings(
          $f->getEntityTypeId(),
          !empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
          [
            'index' => $values['simple_sitemap_index_content'],
            'priority' => $values['simple_sitemap_priority']
          ]
        );
119
        break;
120
    }
121 122 123

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
gbyte.co's avatar
gbyte.co committed
124
      $generator->generateSitemap();
125
    }
126 127 128 129 130 131
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
132
function simple_sitemap_cron() {
133 134 135 136

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
gbyte.co's avatar
gbyte.co committed
137 138 139
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
140
  }
gbyte.co's avatar
gbyte.co committed
141
}
142

143 144 145 146
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
147 148
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
149
 */
gbyte.co's avatar
gbyte.co committed
150
function simple_sitemap_entity_delete(EntityInterface $entity) {
151 152 153 154 155 156 157 158

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
  $generator = \Drupal::service('simple_sitemap.generator');
  $generator->removeEntityInstanceSettings(
    $entity->getEntityTypeId(), $entity->id()
  );
159
}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

/**
 * 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) {
190 191 192 193

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
194 195 196 197 198 199 200 201 202 203 204 205 206 207
  $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']));
  }
}
208 209 210 211 212 213 214 215 216 217

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