simple_sitemap.module 9.14 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1
<?php
2 3 4 5
/**
 * @file
 * Main module file containing hooks.
 */
gbyte.co's avatar
gbyte.co committed
6

7
use Drupal\simple_sitemap\Form;
8

9 10 11 12 13 14 15 16
/**
 * Implements hook_help.
 */
function simple_sitemap_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
  return $route_name === 'help.page.simple_sitemap' ?
    check_markup(file_get_contents(dirname(__FILE__) . "/README.txt")) : NULL;
}

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

24 25 26 27
  // Do not alter the form if user lacks certain permissions.
  if (!\Drupal::currentUser()->hasPermission('administer sitemap settings'))
    return;

28
  $f = new Form($form, $form_state, $form_id);
gbyte.co's avatar
gbyte.co committed
29 30

  // Do not alter the form if it is irrelevant to sitemap generation.
31
  if (empty($f->entityCategory))
32
    return;
gbyte.co's avatar
gbyte.co committed
33

34
  $sitemap = \Drupal::service('simple_sitemap.generator');
35

36
  // Get current entity type sitemap settings.
37
  $entity_types = $sitemap->getConfig('entity_types');
38

39 40 41 42 43
  // Do not alter the form if entity is not enabled in sitemap settings.
  if (!isset($entity_types[$f->entityTypeId]))
    return;

  // Do not alter the form, if sitemap is disabled for the entity type of this entity instance.
44
  if ($f->entityCategory == 'instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
45 46 47
    return;

  // Setting default form values.
48
  $index = 0;
gbyte.co's avatar
gbyte.co committed
49
  $priority = Form::PRIORITY_DEFAULT;
50 51

  // Overwriting defaults if settings found for bundle.
52 53 54
  if (isset($entity_types[$f->entityTypeId][$f->bundleName]['index'])) {
    $bundle_index = $index = $entity_types[$f->entityTypeId][$f->bundleName]['index'];
    $bundle_priority = $priority = $entity_types[$f->entityTypeId][$f->bundleName]['priority'];
55 56
  }

57
  // Overwriting defaults if settings found for this entity instance.
58 59 60 61
  if ($f->entityCategory == 'instance') {
    if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'])) {
      $index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'];
      $priority = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['priority'];
62
    }
63 64
  }

gbyte.co's avatar
gbyte.co committed
65
  $form['simple_sitemap'] = array(
66
    '#type' => 'details',
67
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
68
    '#title' => t('Simple XML sitemap'),
69
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this specific entity can be overridden here.') : '',
70 71
  );

72
  // Attach some js magic to forms.
gbyte.co's avatar
gbyte.co committed
73
  $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
74
  // Only attach fieldset summary js to 'additional settings' vertical tabs.
75
  if (isset($form['additional_settings'])) {
gbyte.co's avatar
gbyte.co committed
76
    $form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
77 78
  }

gbyte.co's avatar
gbyte.co committed
79
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
80 81 82
    '#type' => 'radios',
    '#default_value' => $index,
    '#options' => [
83 84
      0 => $f->entityCategory == 'instance' ? t('Do not index this entity') : t('Do not index entities of this type'),
      1 => $f->entityCategory == 'instance' ? t('Index this entity') : t('Index entities of this type'),
85
    ]
86
  );
87
  if ($f->entityCategory == 'instance' && isset($bundle_index)) {
88 89 90
    $form['simple_sitemap']['simple_sitemap_index_content']['#options'][$bundle_index] .= ' <em>(' . t('Default') . ')</em>';
  }
  
gbyte.co's avatar
gbyte.co committed
91
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
92 93
    '#type' => 'select',
    '#title' => t('Priority'),
94
    '#description' => $f->entityCategory == 'instance' ? t('The priority this entity will have in the eyes of search engine bots.') : t('The priority entities of this bundle will have in the eyes of search engine bots.'),
95
    '#default_value' => $priority,
gbyte.co's avatar
gbyte.co committed
96
    '#options' => Form::getPrioritySelectValues(),
97
  );
98
  if ($f->entityCategory == 'instance' && isset($bundle_priority)) {
99 100
    $form['simple_sitemap']['simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
  }
101

gbyte.co's avatar
gbyte.co committed
102
  $form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
103
    '#type' => 'checkbox',
104
    '#title' => t('Regenerate sitemap after hitting <em>Save</em>'),
105
    '#description' => t('This setting will regenerate the whole sitemap including the above changes.'),
106 107
    '#default_value' => FALSE,
  );
108
  if ($sitemap->getSetting('cron_generate')) {
109
    $form['simple_sitemap']['simple_sitemap_regenerate_now']['#description'] .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
110
  }
111

112
  $form['#simple_sitemap'] = $f;
113

114
  // Add submission handler.
115
  if (isset($form['actions']['submit']['#submit'])) {
116 117 118 119 120
    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';
      }
    }
121
  }
122
  else { // Fix for account page rendering other submit handlers not usable.
gbyte.co's avatar
gbyte.co committed
123
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
124
  }
gbyte.co's avatar
gbyte.co committed
125 126
}

127
/**
128
 * Form submission handler called in hook_form_alter.
129
 */
gbyte.co's avatar
gbyte.co committed
130
function simple_sitemap_entity_form_submit($form, &$form_state) {
131

132
  // Only make changes in DB if sitemap settings actually changed.
133
  if (Form::valuesChanged($form, $form_state)) {
134

135 136 137
    $sitemap = \Drupal::service('simple_sitemap.generator');
    $f = $form['#simple_sitemap'];
    $values = $form_state->getValues();
138 139 140 141
    // Fix for values appearing in a sub array on a commerce product entity.
    if (isset($values['simple_sitemap'])) {
      $values = $values['simple_sitemap'];
    }
142 143

    // Get current entity type sitemap settings.
144
    $entity_types = $sitemap->getConfig('entity_types');
145

146
    switch ($f->entityCategory) {
147 148 149 150 151 152 153 154

      case 'custom':
      case 'bundle':
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : Form::getNewEntityId($form_state);
        $entity_types[$f->entityTypeId][$f->bundleName]['index'] = $values['simple_sitemap_index_content'];
        $entity_types[$f->entityTypeId][$f->bundleName]['priority'] = $values['simple_sitemap_priority'];
        break;

155 156
      case 'instance':
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : Form::getNewEntityId($form_state);
157 158 159 160

        // Delete overrides if they are identical to bundle settings.
        if ($values['simple_sitemap_index_content'] == $entity_types[$f->entityTypeId][$f->bundleName]['index']
          && $values['simple_sitemap_priority'] == $entity_types[$f->entityTypeId][$f->bundleName]['priority']) {
161
          unset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]);
162 163 164
        }
        // Else save overrides for this entity.
        else {
165 166
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['index'] = $values['simple_sitemap_index_content'];
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]['priority'] = $values['simple_sitemap_priority'];
167 168
        }
        break;
169
    }
170

171
    // Save new entity type settings.
172
    $sitemap->saveConfig('entity_types', $entity_types);
173

174 175
    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
176
      $sitemap->generateSitemap();
177
    }
178 179 180 181 182 183
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
184
function simple_sitemap_cron() {
185
  $sitemap = \Drupal::service('simple_sitemap.generator');
186
  if ($sitemap->getSetting('cron_generate')) {
187
    // Regenerate sitemap for all languages.
188
    $sitemap->generateSitemap('backend');
189
  }
gbyte.co's avatar
gbyte.co committed
190
}
191

192 193
/**
 * Implements hook_entity_bundle_delete().
194 195
 *
 * Removes settings of the removed bundle.
196 197
 *
 * @todo Not working for menu bundles, as they are technically not bundles.
198
 */
gbyte.co's avatar
gbyte.co committed
199
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
200
  $sitemap = \Drupal::service('simple_sitemap.generator');
201
  $entity_types = $sitemap->getConfig('entity_types');
202 203
  if (isset($entity_types[$entity_type_id][$bundle])) {
    unset($entity_types[$entity_type_id][$bundle]);
204
    $sitemap->saveConfig('entity_types', $entity_types);
205
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
206
    if ($sitemap->getSetting('cron_generate')) {
207
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
208
    }
209
//    drupal_set_message($message); // Commented out, as html code is visible.
210 211
  }
}
212 213 214 215 216 217 218 219 220

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  $sitemap = \Drupal::service('simple_sitemap.generator');
  $entity_types = $sitemap->getConfig('entity_types');
221 222 223 224 225 226
  $entity_type_id = $entity->getEntityTypeId();
  $bundle_name = !empty($entity->bundle()) ? $entity->bundle() : $entity_type_id;
  $bundle_name = $bundle_name == 'menu_link_content'&& method_exists($entity, 'getMenuName') ? $entity->getMenuName() : $bundle_name; // Menu fix.
  if (isset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()])) {
    unset($entity_types[$entity_type_id][$bundle_name]['entities'][$entity->id()]);
    $sitemap->saveConfig('entity_types', $entity_types);
227 228
  }
}