simple_sitemap.module 5.36 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']['#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 66 67 68
  $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;
69

70 71
  // Only make changes in DB if sitemap settings actually changed.
  if (Form::valuesChanged($form, $values)) {
72
    $sitemap = \Drupal::service('simple_sitemap.generator');
73
    $f = $form['simple_sitemap']['#simple_sitemap'];
74

75
    switch ($f->entityCategory) {
76 77 78

      case 'bundle':
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : Form::getNewEntityId($form_state);
79
        $sitemap->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
80 81
        break;

82 83
      case 'instance':
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : Form::getNewEntityId($form_state);
84
        $sitemap->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
85
        break;
86
    }
87 88 89

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
90
      $sitemap->generateSitemap();
91
    }
92 93 94 95 96 97
  }
}

/**
 * Implements hook_cron.
 */
gbyte.co's avatar
gbyte.co committed
98
function simple_sitemap_cron() {
99
  $sitemap = \Drupal::service('simple_sitemap.generator');
100 101
  if ($sitemap->getSetting('cron_generate')) {
    $sitemap->generateSitemap('backend');
102
  }
gbyte.co's avatar
gbyte.co committed
103
}
104

105 106
/**
 * Implements hook_entity_bundle_delete().
107 108
 *
 * Removes settings of the removed bundle.
109
 *
110
 * @todo Not working for menu bundles, as they are technically not bundles. Implement hook_menu_delete().
111
 */
gbyte.co's avatar
gbyte.co committed
112
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
113
  $sitemap = \Drupal::service('simple_sitemap.generator');
114
  $entity_types = $sitemap->getConfig('entity_types');
115 116
  if (isset($entity_types[$entity_type_id][$bundle])) {
    unset($entity_types[$entity_type_id][$bundle]);
117
    $sitemap->saveConfig('entity_types', $entity_types);
118
    $message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
119
    if ($sitemap->getSetting('cron_generate')) {
120
      $message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
121
    }
122
//    drupal_set_message($message); // todo: Commented out, as html code is visible.
123 124
  }
}
125 126 127 128 129 130 131 132 133

/**
 * 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');
134 135 136 137 138 139
  $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);
140 141
  }
}