simple_sitemap.module 6.72 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
/**
gbyte.co's avatar
gbyte.co committed
14 15 16 17 18
 *Implements hook_help.
 *
 * @param $route_name
 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
 * @return \Drupal\Component\Render\MarkupInterface|null
19
 */
gbyte.co's avatar
gbyte.co committed
20
function simple_sitemap_help($route_name, RouteMatchInterface $route_match) {
21
  return $route_name === 'help.page.simple_sitemap' ?
22
    check_markup(file_get_contents(dirname(__FILE__) . "/README.md")) : NULL;
23 24
}

25 26
/**
 * Implements hook_form_alter.
27 28
 *
 * Adds sitemap settings to entity types that are supported via plugins.
gbyte.co's avatar
gbyte.co committed
29 30 31 32
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 * @param $form_id
33
 */
gbyte.co's avatar
gbyte.co committed
34
function simple_sitemap_form_alter(&$form, FormStateInterface $form_state, $form_id) {
35

36 37 38
  /**
   * @var Drupal\simple_sitemap\Form\FormHelper $f
   */
gbyte.co's avatar
gbyte.co committed
39 40
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f->processForm($form_state)) {
41
    return;
gbyte.co's avatar
gbyte.co committed
42
  }
gbyte.co's avatar
gbyte.co committed
43 44 45 46 47

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

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

gbyte.co's avatar
gbyte.co committed
57 58 59
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
  if (isset($form['additional_settings'])) {
    $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
60
  }
61

62
  $f->displayEntitySettings($form['simple_sitemap'])
gbyte.co's avatar
gbyte.co committed
63 64
  // todo: do not show setting when creating new bundle.
    ->displayRegenerateNow($form['simple_sitemap']);
65

66
  // Add submission handler.
67
  if (isset($form['actions']['submit']['#submit'])) {
68
    foreach (array_keys($form['actions']) as $action) {
69 70 71
      if ($action != 'preview'
        && isset($form['actions'][$action]['#type'])
        && $form['actions'][$action]['#type'] === 'submit') {
72 73 74
        $form['actions'][$action]['#submit'][] = 'simple_sitemap_entity_form_submit';
      }
    }
75
  }
gbyte.co's avatar
gbyte.co committed
76 77
  // Fix for account page rendering other submit handlers not usable.
  else {
gbyte.co's avatar
gbyte.co committed
78
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
79
  }
gbyte.co's avatar
gbyte.co committed
80 81
}

82
/**
83
 * Form submission handler called in hook_form_alter.
gbyte.co's avatar
gbyte.co committed
84 85 86
 *
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
87
 */
gbyte.co's avatar
gbyte.co committed
88
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
89

90 91 92
  /**
   * @var Drupal\simple_sitemap\Form\FormHelper $f
   */
gbyte.co's avatar
gbyte.co committed
93 94 95 96
  $f = \Drupal::service('simple_sitemap.form_helper');
  if (!$f->processForm($form_state)) {
    return;
  }
97

98 99 100 101
  $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;
102

103
  // Only make changes in DB if sitemap settings actually changed.
104
  if ($f->valuesChanged($form, $values)) {
105 106 107 108

    /**
     * @var \Drupal\simple_sitemap\Simplesitemap $generator
     */
109
    $generator = \Drupal::service('simple_sitemap.generator');
110

111
    switch ($f->getEntityCategory()) {
112 113

      case 'bundle':
114 115 116 117 118 119 120 121
        $generator->setBundleSettings(
          $f->getEntityTypeId(),
          !empty($f->getBundleName()) ? $f->getBundleName() : $f->getFormEntityId(),
          [
            'index' => $values['simple_sitemap_index_content'],
            'priority' => $values['simple_sitemap_priority']
          ]
        );
122 123
        break;

124
      case 'instance':
125 126 127 128 129 130 131 132
        $generator->setEntityInstanceSettings(
          $f->getEntityTypeId(),
          !empty($f->getInstanceId()) ? $f->getInstanceId() : $f->getFormEntityId(),
          [
            'index' => $values['simple_sitemap_index_content'],
            'priority' => $values['simple_sitemap_priority']
          ]
        );
133
        break;
134
    }
135 136 137

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
138
      $generator->generateSitemap();
139
    }
140 141 142 143 144 145
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
146
function simple_sitemap_cron() {
147 148 149 150

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
151 152 153
  $generator = \Drupal::service('simple_sitemap.generator');
  if ($generator->getSetting('cron_generate')) {
    $generator->generateSitemap('backend');
154
  }
gbyte.co's avatar
gbyte.co committed
155
}
156

157 158 159 160
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
161 162
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
163
 */
gbyte.co's avatar
gbyte.co committed
164
function simple_sitemap_entity_delete(EntityInterface $entity) {
165 166 167 168 169 170 171 172

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
  $generator = \Drupal::service('simple_sitemap.generator');
  $generator->removeEntityInstanceSettings(
    $entity->getEntityTypeId(), $entity->id()
  );
173
}
174 175 176 177 178

/**
 * Implements hook_entity_bundle_delete().
 *
 * Removes settings of the removed bundle.
gbyte.co's avatar
gbyte.co committed
179 180 181
 *
 * @param string $entity_type_id
 * @param string $bundle
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
 */
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) {
207 208 209 210

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap $generator
   */
211 212 213 214 215 216 217 218 219 220 221 222 223 224
  $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']));
  }
}
225 226 227 228 229 230 231 232 233 234

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