simple_sitemap.module 5.29 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
    $f->displayEntitySitemapSettings($form['simple_sitemap']);
    $f->displaySitemapRegenerationSetting($form['simple_sitemap']);
40
  }
41 42
  else {
    return;
43
  }
44

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

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

63 64 65 66
  $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;
67

68 69
  // Only make changes in DB if sitemap settings actually changed.
  if (Form::valuesChanged($form, $values)) {
70
    $sitemap = \Drupal::service('simple_sitemap.generator');
71
    $f = new Form($form_state);
72

73
    switch ($f->entityCategory) {
74 75 76

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

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

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

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

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

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