simple_sitemap.module 8.74 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 31

  // Do not alter the form if it is irrelevant to sitemap generation.
  if (empty($f->entityType))
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

gbyte.co's avatar
gbyte.co committed
39
  // Do not alter the form, if sitemap is disabled for entity type.
40
  if ($f->entityType == 'bundle_instance' && empty($entity_types[$f->entityTypeId][$f->bundleName]['index']))
41 42 43
    return;

  // Setting default form values.
44
  $index = 0;
gbyte.co's avatar
gbyte.co committed
45
  $priority = Form::PRIORITY_DEFAULT;
46 47

  // Overwriting defaults if settings found for bundle.
48 49 50
  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'];
51 52 53
  }

  // Overwriting defaults if settings found for entity.
54 55 56 57 58
  if ($f->entityType == 'bundle_instance') {
    if (isset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'])) {
      $index = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'];
      $priority = $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'];
    }
59 60
  }

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

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

gbyte.co's avatar
gbyte.co committed
75
  $form['simple_sitemap']['simple_sitemap_index_content'] = array(
76 77 78
    '#type' => 'radios',
    '#default_value' => $index,
    '#options' => [
79 80
      0 => $f->entityType == 'bundle_instance' ? t('Do not index this entity') : t('Do not index entities of this type'),
      1 => $f->entityType == 'bundle_instance' ? t('Index this entity') : t('Index entities of this type'),
81
    ]
82
  );
83 84 85 86
  if ($f->entityType == 'bundle_instance' && isset($bundle_index)) {
    $form['simple_sitemap']['simple_sitemap_index_content']['#options'][$bundle_index] .= ' <em>(' . t('Default') . ')</em>';
  }
  
gbyte.co's avatar
gbyte.co committed
87
  $form['simple_sitemap']['simple_sitemap_priority'] = array(
88 89
    '#type' => 'select',
    '#title' => t('Priority'),
90
    '#description' => $f->entityType == 'bundle_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.'),
91
    '#default_value' => $priority,
gbyte.co's avatar
gbyte.co committed
92
    '#options' => Form::getPrioritySelectValues(),
93
  );
94 95 96
  if ($f->entityType == 'bundle_instance' && isset($bundle_priority)) {
    $form['simple_sitemap']['simple_sitemap_priority']['#options'][(string)$bundle_priority] .= ' (' . t('Default') . ')';
  }
97

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

108
  $form['#simple_sitemap'] = $f;
109

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

123
/**
124
 * Form submission handler called in hook_form_alter.
125
 */
gbyte.co's avatar
gbyte.co committed
126
function simple_sitemap_entity_form_submit($form, &$form_state) {
127

128
  // Only make changes in DB if sitemap settings actually changed.
129
  if (Form::valuesChanged($form, $form_state)) {
130

131 132 133
    $sitemap = \Drupal::service('simple_sitemap.generator');
    $f = $form['#simple_sitemap'];
    $values = $form_state->getValues();
134 135

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    switch ($f->entityType) {

      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;

      case 'bundle_instance':
        $f->entityId = !empty($f->entityId) ? $f->entityId : Form::getNewEntityId($form_state);

        // 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']) {
          unset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]);
        }
        // Else save overrides for this entity.
        else {
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['index'] = $values['simple_sitemap_index_content'];
          $entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->entityId]['priority'] = $values['simple_sitemap_priority'];
        }
        break;
161
    }
162

163
    // Save new entity type settings.
164
    $sitemap->saveConfig('entity_types', $entity_types);
165

166 167
    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
168
      $sitemap->generateSitemap();
169
    }
170 171 172 173 174 175
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
176
function simple_sitemap_cron() {
177
  $sitemap = \Drupal::service('simple_sitemap.generator');
178
  if ($sitemap->getSetting('cron_generate')) {
179
    // Regenerate sitemap for all languages.
180
    $sitemap->generateSitemap('backend');
181
  }
gbyte.co's avatar
gbyte.co committed
182
}
183

184 185
/**
 * Implements hook_entity_bundle_delete().
186 187
 *
 * Removes settings of the removed bundle.
188
 */
gbyte.co's avatar
gbyte.co committed
189
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
190
  $sitemap = \Drupal::service('simple_sitemap.generator');
191
  $entity_types = $sitemap->getConfig('entity_types');
192
  $bundle_entity_type_id = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getBundleEntityType();
193 194
  if (isset($entity_types[$bundle_entity_type_id][$bundle])) {
    unset($entity_types[$bundle_entity_type_id][$bundle]);
195
    $sitemap->saveConfig('entity_types', $entity_types);
196
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
197
    if ($sitemap->getSetting('cron_generate')) {
198
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
199 200
    }
    drupal_set_message($message);
201 202
  }
}
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

/**
 * 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');
  $entity_type = $entity->getEntityType();
  if (!empty($entity_type->getBundleEntityType())) {
    $bundle_entity_type = $entity_type->getBundleEntityType();
    $bundle = $entity->bundle();
    if (isset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()])) {
      unset($entity_types[$bundle_entity_type][$bundle]['entities'][$entity->id()]);
      $sitemap->saveConfig('entity_types', $entity_types);
    }
  }
}