simple_sitemap.module 6.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 28 29 30 31 32 33 34 35 36
  $f = new Form($form_state);
  if ($f->alteringForm) {
    $form['simple_sitemap'] = [
      '#type' => 'details',
      '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
      '#title' => t('Simple XML sitemap'),
      '#description' => $f->entityCategory == 'instance' ? t('Settings for this specific entity can be overridden here.') : '',
    ];
    // Attach some js magic to forms.
    if ($f->entityTypeId != 'comment' || $f->entityCategory != 'instance') // todo: JS not working on comment entity form, hence disabling.
      $form['#attached']['library'][] = 'simple_sitemap/form';
    // Only attach fieldset summary js to 'additional settings' vertical tabs.
    if (isset($form['additional_settings'])) {
      $form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
37
    }
38

39 40
    $f->displayEntitySitemapSettings($form['simple_sitemap']);
    $f->displaySitemapRegenerationSetting($form['simple_sitemap']);
41
  }
42 43
  else {
    return;
44
  }
45
  $form['#simple_sitemap'] = $f;
46

47
  // Add submission handler.
48
  if (isset($form['actions']['submit']['#submit'])) {
49 50 51 52 53
    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';
      }
    }
54
  }
55
  else { // Fix for account page rendering other submit handlers not usable.
gbyte.co's avatar
gbyte.co committed
56
    $form['#submit'][] = 'simple_sitemap_entity_form_submit';
57
  }
gbyte.co's avatar
gbyte.co committed
58 59
}

60
/**
61
 * Form submission handler called in hook_form_alter.
62
 */
gbyte.co's avatar
gbyte.co committed
63
function simple_sitemap_entity_form_submit($form, &$form_state) {
64

65
  // Only make changes in DB if sitemap settings actually changed.
66
  if (Form::valuesChanged($form, $form_state)) {
67

68 69 70
    $sitemap = \Drupal::service('simple_sitemap.generator');
    $f = $form['#simple_sitemap'];
    $values = $form_state->getValues();
71 72 73 74
    // Fix for values appearing in a sub array on a commerce product entity.
    if (isset($values['simple_sitemap'])) {
      $values = $values['simple_sitemap'];
    }
75 76

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

79
    switch ($f->entityCategory) {
80 81 82 83 84 85 86 87

      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;

88 89
      case 'instance':
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : Form::getNewEntityId($form_state);
90 91 92 93

        // 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']) {
94
          unset($entity_types[$f->entityTypeId][$f->bundleName]['entities'][$f->instanceId]);
95
        }
96
        else { // Else save overrides for this entity.
97 98
          $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'];
99 100
        }
        break;
101
    }
102

103
    // Save new entity type settings.
104
    $sitemap->saveConfig('entity_types', $entity_types);
105

106 107
    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
108
      $sitemap->generateSitemap();
109
    }
110 111 112 113 114 115
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
116
function simple_sitemap_cron() {
117
  $sitemap = \Drupal::service('simple_sitemap.generator');
118 119
  if ($sitemap->getSetting('cron_generate')) {
    $sitemap->generateSitemap('backend');
120
  }
gbyte.co's avatar
gbyte.co committed
121
}
122

123 124
/**
 * Implements hook_entity_bundle_delete().
125 126
 *
 * Removes settings of the removed bundle.
127
 *
128
 * @todo Not working for menu bundles, as they are technically not bundles. Implement hook_menu_delete().
129
 */
gbyte.co's avatar
gbyte.co committed
130
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
131
  $sitemap = \Drupal::service('simple_sitemap.generator');
132
  $entity_types = $sitemap->getConfig('entity_types');
133 134
  if (isset($entity_types[$entity_type_id][$bundle])) {
    unset($entity_types[$entity_type_id][$bundle]);
135
    $sitemap->saveConfig('entity_types', $entity_types);
136
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
137
    if ($sitemap->getSetting('cron_generate')) {
138
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
139
    }
140
//    drupal_set_message($message); // todo: Commented out, as html code is visible.
141 142
  }
}
143 144 145 146 147 148 149 150 151

/**
 * 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');
152 153 154 155 156 157
  $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);
158 159
  }
}