simple_sitemap.module 5.36 KB
Newer Older
Pawel G's avatar
Pawel G committed
1
<?php
2 3 4 5
/**
 * @file
 * Main module file containing hooks.
 */
Pawel G's avatar
Pawel G 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
 */
Pawel G's avatar
Pawel G committed
22
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
23
  $f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
Pawel G's avatar
Pawel G committed
24
  if (!$f->alteringForm)
25
    return;
Pawel G's avatar
Pawel G committed
26 27 28 29 30

  $form['simple_sitemap'] = [
    '#type' => 'details',
    '#group' => isset($form['additional_settings']) ? 'additional_settings' : 'advanced',
    '#title' => t('Simple XML sitemap'),
31
    '#description' => $f->entityCategory == 'instance' ? t('Settings for this entity can be overridden here.') : '',
Pawel G's avatar
Pawel G committed
32 33 34 35 36 37 38
  ];
  // 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';
39
  }
Pawel G's avatar
Pawel G committed
40
  $f->displayEntitySettings($form['simple_sitemap']);
Pawel G's avatar
Pawel G committed
41
  $f->displayRegenerateNow($form['simple_sitemap']); // todo: do not show setting when creating new bundle
42

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

56
/**
57
 * Form submission handler called in hook_form_alter.
58
 */
Pawel G's avatar
Pawel G committed
59
function simple_sitemap_entity_form_submit($form, &$form_state) {
60

61 62 63 64
  $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;
65

66 67
  // Only make changes in DB if sitemap settings actually changed.
  if (Form::valuesChanged($form, $values)) {
Pawel G's avatar
Pawel G committed
68
    $generator = \Drupal::service('simple_sitemap.generator');
69
    $f = \Drupal::service('simple_sitemap.form')->processForm($form_state);
70

71
    switch ($f->entityCategory) {
72 73

      case 'bundle':
Pawel G's avatar
Pawel G committed
74
        $f->bundleName = !empty($f->bundleName) ? $f->bundleName : $f->getFormEntityId();
Pawel G's avatar
Pawel G committed
75
        $generator->setBundleSettings($f->entityTypeId, $f->bundleName, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
76 77
        break;

78
      case 'instance':
Pawel G's avatar
Pawel G committed
79
        $f->instanceId = !empty($f->instanceId) ? $f->instanceId : $f->getFormEntityId();
Pawel G's avatar
Pawel G committed
80
        $generator->setEntityInstanceSettings($f->entityTypeId, $f->instanceId, ['index' => $values['simple_sitemap_index_content'], 'priority' => $values['simple_sitemap_priority']]);
81
        break;
82
    }
83 84 85

    // Regenerate sitemaps according to user setting.
    if ($values['simple_sitemap_regenerate_now']) {
Pawel G's avatar
Pawel G committed
86
      $generator->generateSitemap();
87
    }
88 89 90 91 92 93
  }
}

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

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

/**
 * Implements hook_entity_delete().
 *
 * Removes settings of the removed entity.
 */
function simple_sitemap_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
Pawel G's avatar
Pawel G committed
128 129
  $generator = \Drupal::service('simple_sitemap.generator');
  $entity_types = $generator->getConfig('entity_types');
130 131 132 133 134
  $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()]);
Pawel G's avatar
Pawel G committed
135
    $generator->saveConfig('entity_types', $entity_types);
136 137
  }
}
138