simple_sitemap.module 5.59 KB
Newer Older
Pawel G's avatar
Pawel G committed
1
<?php
Pawel G's avatar
Pawel G committed
2

3 4 5 6
/**
 * @file
 * Main module file containing hooks.
 */
Pawel G's avatar
Pawel G committed
7

Pawel G's avatar
Pawel G committed
8
use \Drupal\Core\Form\FormStateInterface;
Pawel G's avatar
Pawel G 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.
 */
Pawel G's avatar
Pawel G 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
 */
Pawel G's avatar
Pawel G 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);
Pawel G's avatar
Pawel G committed
29
  if (!$f->alteringForm) {
30
    return;
Pawel G's avatar
Pawel G committed
31
  }
Pawel G's avatar
Pawel G 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.') : '',
Pawel G's avatar
Pawel G committed
38
  ];
39

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

Pawel G's avatar
Pawel G 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

Pawel G's avatar
Pawel G committed
51
  $f->displayEntitySettings($form['simple_sitemap'])
Pawel G's avatar
Pawel G 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 58 59 60 61
    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';
      }
    }
62
  }
Pawel G's avatar
Pawel G committed
63 64
  // Fix for account page rendering other submit handlers not usable.
  else {
Pawel G's avatar
Pawel G committed
65
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
66
  }
Pawel G's avatar
Pawel G committed
67 68
}

69
/**
70
 * Form submission handler called in hook_form_alter.
71
 */
Pawel G's avatar
Pawel G committed
72
function simple_sitemap_entity_form_submit($form, FormStateInterface &$form_state) {
73

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

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

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

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

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

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

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

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

115 116 117 118
/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
119 120
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
121
 */
Pawel G's avatar
Pawel G committed
122
function simple_sitemap_entity_delete(EntityInterface $entity) {
123 124
  \Drupal::service('simple_sitemap.generator')
    ->removeEntityInstanceSettings($entity->getEntityTypeId(), $entity->id());
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 169

/**
 * 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']));
  }
}